Dan*_*ndy 9 unit-testing karma-jasmine angular-material angular
我有一个LoginView组件,其中有角度材质选项卡。在一个选项卡中显示一个LoginForm组件,在第二个选项卡中显示一个RegistrationForm组件。
我尝试测试的LoginView是,当我单击第二个选项卡时,RegistrationForm将显示 a 。但是,我不知道如何单击选项卡。我尝试添加nameorid到 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();
  });
});
小智 9
我刚刚解决了同样的问题。你的测试没问题。只需添加 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();
  });
}));
元素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();