У меня есть мат-меню, в котором содержание может отличаться в зависимости от пользователя. Я пытаюсь написать модульный тест, но, судя по тому, что я вижу, jasmine не видит CDK div
, поэтому я не могу получить записи меню.
Мой шаблон:
<button id="account" mat-icon-button [matMenuTriggerFor]="menu" aria-label="Profile">
<mat-icon>person</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item *ngxPermissionsOnly="PERMISSION.USER_LIST" id="user-list" (click)="usersList()">
<mat-icon>recent_actors</mat-icon>
</button>
<button mat-menu-item *ngxPermissionsOnly="PERMISSION.INFORMATIONS" id="informations" (click)="infoList()">
<mat-icon>info</mat-icon>
</button>
<button mat-menu-item id="logout" (click)="logout()">
<mat-icon>exit_to_app</mat-icon>
</button>
</mat-menu>
Модульный тест:
let component: HeaderComponent;
let fixture: ComponentFixture < HeaderComponent > ;
const providers: any[] = headerProviders;
beforeEach(async (() => {
TestBed.configureTestingModule({
declarations: [
HeaderComponent,
NgxPermissionsRestrictStubDirective
],
providers: providers,
imports: [
BrowserAnimationsModule,
BrowserModule,
CommonModule,
CommonSogetrelModule,
FlexLayoutModule,
SharedMaterialModule,
RouterTestingModule.withRoutes([])
]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
});
}));
it('should not display elements which needs permissions', () => {
fixture.nativeElement.querySelector('#account').click();
fixture.detectChanges();
expect(logoutBtn).toBeTruthy('Le bouton Déconnexion doit être affiché');
expect(fixture.debugElement.nativeElement.querySelector('#user-list')).toBeFalsy();
});
Я пробовал с
console.info(fixture.nativeElement.parentNode);
const menu = fixture.nativeElement.parentNode.querySelector('.mat-menu-panel');
expect(menu).toBeTruthy();
Что я вижу с console.info
, так это то, что на странице нет CDK div
, поэтому очевидно, что .mat-menu-panel
не найден.
Есть идеи, как проверить содержимое мат-меню?
imports
естьMatMenuModule
иMatIconModule
. Скорее всего, он у вас есть, так как вы не получаете ошибок при компиляции. - person AliF50   schedule 28.04.2020fixture.debugElement.query(By.css('#account')).nativeElement.click()
иexpect(fixture.debugElement.query(By.css('#logout'))).toBeTruthy()
,expect(fixture.debugElement.query(By.css('#user-list'))).toBeFalsy()
?, тем не менее, я думаю, вам следует добавить любой вывод консоли или результат теста, фактически и то, и другое. - person Bargros   schedule 07.02.2021