条件渲染信息的角度单元测试

问题描述 投票:0回答:1
angular unit-testing
1个回答
0
投票

你可以尝试两种方法吗?

  1. 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();

  }));
  1. 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();

  });
© www.soinside.com 2019 - 2024. All rights reserved.