Pradeep
Pradeep

Reputation: 419

Angular - Material <mat-option> not visible in tests

I am working on Angular 9 with Material components. I want to show some values with in mat-select component.

Below is app.component.html

<h2> Angular - Material </h2>

<mat-form-field>
  <mat-label>Type</mat-label>
  <mat-select >
    <mat-option value="string">Small text</mat-option>
    <mat-option value="number">Number</mat-option>
    <mat-option value="date">Date</mat-option>
  </mat-select>
</mat-form-field>

<hr>

My app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'angular-playground';
}

When I run app with ng serve then I can see select box with all options.

<mat-select> showing options

But when I try to test this component with karma ng test then I am not getting any options in the select box. I tried inspecting HTML DOM and I am not seeing any option tags with in select and there are no errors in the console.

enter image description here

app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { MatFormFieldModule } from '@angular/material/form-field'; 
import {MatSelectModule} from '@angular/material/select'; 

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule, MatFormFieldModule, MatSelectModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
});

This is a so basic angular application and I have a simple component with only select box. Not sure why it is not showing options in test.

Below is my app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatFormFieldModule } from '@angular/material/form-field'; 
import {MatSelectModule} from '@angular/material/select'; 


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatFormFieldModule,
    MatSelectModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Please help me on this.

Upvotes: 3

Views: 3708

Answers (2)

CSSBurner
CSSBurner

Reputation: 1991

In my experience, the By.css() or By.directive() methid cannot be used to access the MatOption elements. Instead, the options can be accessed a bit differently:

const select = fixture.debugElement.queryAll(By.css('mat-select'))[0].componentInstance;
const options: MatOption[] = select.options;

... and then, if tests need to be run on the options themselves, you can test methods on the options such as:

options[i].focus(); // i = index, triggerEventHandler('focus')
options[i].select(); // triggerEventHandler('select')
options[i].deselect(); // triggerEventHandler('deselect')
options[i].getLabel(); // triggerEventHandler('getLabel')
options[i].toggle(); // triggerEventHandler('toggle')
options[i].disabled;
options[i].selected;
options.length;

All of these methods can be found within the source located at @angular/material/core. (right click on your MatOption in your import statement).

Upvotes: 0

Nicooss54
Nicooss54

Reputation: 11

you must trigger the toggle of the MatSelectComponent to display your list of options like that:

const matSelectComponent = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
fixture.detectChanges();

Now you can access to your options with:

const matOptionComponent = fixture.debugElement.queryAll(By.directive(MatOption));

Upvotes: 1

Related Questions