The FormsModule was not added to the component in the stackblitz.
import { FormsModule } from '@angular/forms'; /** @title Select with 2-way value binding */ @Component({ selector: 'select-value-binding-example', templateUrl: 'select-value-binding-example.html', standalone: true, imports: [MatFormFieldModule, MatSelectModule, FormsModule], }) export class SelectValueBindingExample { selected = 'option2'; }
Please ensure the click the correct option that matches the test case:
it('dummy test', async () => { const issueTypeFilter = await loader.getHarness<MatSelectHarness>( MatSelectHarness.with({ selector: '#issuetype-filter', }) ); await issueTypeFilter.open(); expect((await issueTypeFilter.getOptions()).length).toBe(4); // correct const options = await issueTypeFilter.getOptions(); await options[1].click(); await fixture.detectChanges(); expect(await issueTypeFilter.getValueText()).toBe('Option 1'); expect(component.selected).toBe('option1'); // binding value should change });
Full Code:
Test:
import { HarnessLoader } from '@angular/cdk/testing'; import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; import { MatSelectHarness } from '@angular/material/select/testing'; import { MatTableHarness } from '@angular/material/table/testing'; import { SelectValueBindingExample } from './select-value-binding-example'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatSelectModule } from '@angular/material/select'; let loader: HarnessLoader; describe('test', () => { let component: SelectValueBindingExample; let fixture: ComponentFixture<SelectValueBindingExample>; beforeEach(async () => { await TestBed.configureTestingModule({ imports: [BrowserAnimationsModule, MatSelectModule], }).compileComponents(); fixture = TestBed.createComponent(SelectValueBindingExample); loader = TestbedHarnessEnvironment.loader(fixture); component = fixture.componentInstance; fixture.detectChanges(); }); it('dummy test', async () => { const issueTypeFilter = await loader.getHarness<MatSelectHarness>( MatSelectHarness.with({ selector: '#issuetype-filter', }) ); await issueTypeFilter.open(); expect((await issueTypeFilter.getOptions()).length).toBe(4); // correct const options = await issueTypeFilter.getOptions(); await options[1].click(); await fixture.detectChanges(); expect(await issueTypeFilter.getValueText()).toBe('Option 1'); expect(component.selected).toBe('option1'); // binding value should change }); });
HTML:
<mat-form-field> <mat-label>Select an option</mat-label> <mat-select [(ngModel)]="selected" id="issuetype-filter"> <mat-option>None</mat-option> <mat-option value="option1">Option 1</mat-option> <mat-option value="option2">Option 2</mat-option> <mat-option value="option3">Option 3</mat-option> </mat-select> </mat-form-field> <p>You selected: {{selected}}</p>
TS:
import { Component } from '@angular/core'; import { MatSelectModule } from '@angular/material/select'; import { MatFormFieldModule } from '@angular/material/form-field'; import { FormsModule } from '@angular/forms'; /** @title Select with 2-way value binding */ @Component({ selector: 'select-value-binding-example', templateUrl: 'select-value-binding-example.html', standalone: true, imports: [MatFormFieldModule, MatSelectModule, FormsModule], }) export class SelectValueBindingExample { selected = 'option2'; }