4
votes

I have a LoginView component in which I have Angular Material Tabs. In one tab there's a LoginForm component displayed and in a second tab there's a RegistrationForm component.

What I try to test in LoginView is that when I click on a second tab, a RegistrationForm would be displayed. However, I have no idea how to click a tab. I've tried adding name or id to mat-tab but it isn't being generated in DOM, querySelectorAll() also returns null.

Source:

<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>

Spec file:

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();
  });
});
2
Please add all the spec file. The BeforeEach is essential to help you.JFPicard
@JFPicard I've edited my code.Dandy

2 Answers

4
votes

I just worked the same problem. Your test is fine. Just add async() and use 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();
  });
}));
1
votes

The mat-tab element is just the container. The element that handles the click uses the class mat-tab-label. Try:

fixture.debugElement.queryAll(By.css('.mat-tab-label'))[1].nativeElement.click();

EDIT:

Alternatively, include inside your component a reference to the MatTabGroup component, and then set MatTabGroup.selectedIndex directly or from a component function:

Component HTML:

<mat-tab-group dynamicHeight class="py-5" #tabGroup=="matTabGroup">
...

Component TS:

@ViewChild('tabGroup') tabGroup: MatTabGroup;

setTab(index: number) {
    // maybe add some bounds and null checking
    this.tabGroup.selectedIndex = index;
}

Unit test usage:

component.setTab(1):
fixture.detectChanges();