首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试带有*ngIf的有条件呈现<div>的角分量?

如何测试带有*ngIf的有条件呈现<div>的角分量?
EN

Stack Overflow用户
提问于 2022-10-14 17:49:08
回答 1查看 35关注 0票数 0

我有一个组件有条件地呈现一个div。

代码语言:javascript
复制
<div *ngIf="shouldRender" id="info-banner">
 <p>The info is shown</p>
</div>

测试看起来是这样的

代码语言:javascript
复制
it('should display the info') , () => {
   spectator = createComponent()
   spectator.setInput( { shouldRender:true})
   spectator.detectChanges()
   const infoBanner = spectator.query("#info-banner")
   expect(infoBanner).toBeTruthy()
})

考试总是失败,为什么?

EN

回答 1

Stack Overflow用户

发布于 2022-10-14 18:41:02

您能否在MyComponent发生时调整以下代码,并给出测试失败消息到底在说什么?

代码语言:javascript
复制
import {
    byTestId, createComponentFactory, Spectator
} from '@ngneat/spectator';

describe('MyComponent with spectator', () => {
    let spectator: Spectator<MyComponent>; // adjust MyComponent with your component

    const createComponent = createComponentFactory({
        component: MyComponent, // adjust MyComponent with your component
        shallow: true,
    });

    beforeEach(() => {
        spectator = createComponent({ props: {shouldRender: true}});
    });

    it('should display the info if shouldRender is true', () => {
        expect(
            spectator.query(byTestId('info-banner'))
        ).toBeTruthy;
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74073066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档