我有一个LoginView
组件,其中有“角度材质”选项卡。在一个选项卡中显示一个LoginForm
组件,在第二个选项卡中显示一个RegistrationForm
组件。
我尝试在LoginView
中进行测试的是,当我单击第二个选项卡时,将显示一个RegistrationForm
。但是,我不知道如何单击选项卡。我尝试将name
或id
添加到mat-tab,但它不是在DOM中生成的,querySelectorAll()
也返回null
。
来源:
<mat-tab-group dynamicHeight class="py-5">
<mat-tab label="{{'form.letsLogIn' | translate}}">
<app-login-form></app-login-form>
</mat-tab>
<mat-tab label="{{'form.letsRegister' | translate}}">
<app-registration-form></app-registration-form>
</mat-tab>
</mat-tab-group>
规范文件:
import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginViewComponent } from './login-view.component';
import { TranslateModule } from '@ngx-translate/core';
import { MatTabsModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@Component({selector: 'app-login-form', template: ''})
class LoginFormStubComponent {}
@Component({selector: 'app-registration-form', template: ''})
class RegistrationFormStubComponent {}
describe('LoginViewComponent', () => {
let component: LoginViewComponent;
let fixture: ComponentFixture<LoginViewComponent>;
let compiled: any;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
LoginViewComponent,
LoginFormStubComponent,
RegistrationFormStubComponent ],
imports: [
TranslateModule.forRoot(),
MatTabsModule,
BrowserAnimationsModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginViewComponent);
component = fixture.componentInstance;
compiled = fixture.nativeElement;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have a title', () => {
expect(compiled.querySelector('h1')).toBeTruthy();
});
it('should display login form at start', () => {
expect(compiled.querySelector('app-login-form')).toBeTruthy();
});
it('should display registration form after clicking second tab', () => {
compiled = fixture.nativeElement;
compiled.querySelectorAll('mat-tab')[1].click();
fixture.detectChanges();
expect(compiled.querySelector('app-registration-form')).toBeTruthy();
});
});
答案 0 :(得分:1)
mat-tab
元素只是容器。处理点击的元素使用类mat-tab-label
。试试:
fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1].nativeElement.click();
编辑:
或者,在您的组件中包含对MatTabGroup
组件的引用,然后直接或通过组件函数设置MatTabGroup.selectedIndex
:
组件HTML:
<mat-tab-group dynamicHeight class="py-5" #tabGroup=="matTabGroup">
...
组件TS:
@ViewChild('tabGroup') tabGroup: MatTabGroup;
setTab(index: number) {
// maybe add some bounds and null checking
this.tabGroup.selectedIndex = index;
}
单元测试用法:
component.setTab(1):
fixture.detectChanges();
答案 1 :(得分:0)
我只是解决了同样的问题。 您的测试很好。只需添加async()并使用whenStable()即可。
it('should display registration form after clicking second tab', async(() => {
compiled = fixture.nativeElement;
compiled.querySelectorAll('mat-tab')[1].click();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(compiled.querySelector('app-registration-form')).toBeTruthy();
});
}));