|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { HarnessLoader } from '@angular/cdk/testing'; |
| 3 | +import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; |
| 4 | +import { MatSelectHarness } from '@angular/material/select/testing'; |
| 5 | +import { LocationSelectMenuComponent } from './location-select-menu.component'; |
| 6 | +import { Location } from '../../library/Location'; |
| 7 | + |
| 8 | +let component: LocationSelectMenuComponent; |
| 9 | +let fixture: ComponentFixture<LocationSelectMenuComponent>; |
| 10 | +describe('LocationSelectMenuComponent', () => { |
| 11 | + let loader: HarnessLoader; |
| 12 | + beforeEach(async () => { |
| 13 | + await TestBed.configureTestingModule({ |
| 14 | + imports: [LocationSelectMenuComponent] |
| 15 | + }).compileComponents(); |
| 16 | + |
| 17 | + fixture = TestBed.createComponent(LocationSelectMenuComponent); |
| 18 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 19 | + component = fixture.componentInstance; |
| 20 | + component['viewValueProp'] = 'name'; |
| 21 | + component.options = [ |
| 22 | + Object.assign(new Location(), { |
| 23 | + level1: 'USA', |
| 24 | + level2: 'California' |
| 25 | + }), |
| 26 | + Object.assign(new Location(), { |
| 27 | + level1: 'USA', |
| 28 | + level2: 'New York' |
| 29 | + }), |
| 30 | + Object.assign(new Location(), { |
| 31 | + level1: 'Canada', |
| 32 | + level2: 'Ontario' |
| 33 | + }) |
| 34 | + ]; |
| 35 | + fixture.detectChanges(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should show groups and options', async () => { |
| 39 | + const select = await loader.getHarness(MatSelectHarness); |
| 40 | + await select.open(); |
| 41 | + const optionGroups = await select.getOptionGroups(); |
| 42 | + expect( |
| 43 | + await Promise.all(optionGroups.map(async (option) => await option.getLabelText())) |
| 44 | + ).toEqual(['State', 'Country']); |
| 45 | + const options = await select.getOptions(); |
| 46 | + expect(await Promise.all(options.map(async (option) => await option.getText()))).toEqual([ |
| 47 | + 'California, USA', |
| 48 | + 'New York, USA', |
| 49 | + 'Ontario, Canada', |
| 50 | + 'Canada', |
| 51 | + 'USA' |
| 52 | + ]); |
| 53 | + }); |
| 54 | +}); |
0 commit comments