How do I unit test if an element is visible when the *ngIf directive is used using Jasmine in Angular

前端 未结 4 1652
礼貌的吻别
礼貌的吻别 2020-12-28 15:28

I have an Angular 6 app and writing some unit tests trying to determine if an element is visible or not based solely on the boolean result of an *ngIf directive

4条回答
  •  梦毁少年i
    2020-12-28 16:21

    If the element is hidden, then it wont be rendered inside the dom.

    You can check

    expect(fixture.debugElement.query(By.css('.header'))).toBeUndefined();
    

    EDIT : toBeNull() works better in the above case

    expect(fixture.debugElement.query(By.css('.header'))).toBeNull();
    

    And also you have a syntax error while fetching the button element. nativeElement is not a function.

    Change it this way :

    const button = fixture.debugElement.query(By.css('button')).nativeElement;
    

提交回复
热议问题