你可以尝试两种方法吗?
fakeAsync
和 flush
代码:
it('should be visible when visible is true and invisible when false', fakeAsync(()=> {
component.visible = true;
fixture.detectChanges();
flush();
expect(fixture.debugElement.nativeElement.querySelector('.dialog')).toBeTruthy();
component.visible = false;
fixture.detectChanges();
flush();
expect(fixture.debugElement.nativeElement.querySelector('.dialog')).toBeFalsy();
}));
await fixture.whenStable();
代码:
it('should be visible when visible is true and invisible when false', async ()=> {
component.visible = true;
fixture.detectChanges();
await fixture.whenStable();
expect(fixture.debugElement.nativeElement.querySelector('.dialog')).toBeTruthy();
component.visible = false;
fixture.detectChanges();
await fixture.whenStable();
expect(fixture.debugElement.nativeElement.querySelector('.dialog')).toBeFalsy();
});