|
1 |
| -import {MatOptionModule, MatOption} from '@angular/material/core'; |
2 |
| -import {runHarnessTests} from './option-shared.spec'; |
| 1 | +import {Component, ViewChildren, QueryList} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 5 | +import { |
| 6 | + MatOption, |
| 7 | + MatOptionModule, |
| 8 | + MAT_OPTION_PARENT_COMPONENT, |
| 9 | + MatOptionParentComponent, |
| 10 | +} from '@angular/material/core'; |
3 | 11 | import {MatOptionHarness} from './option-harness';
|
4 | 12 |
|
5 |
| -describe('MDC-based MatOptionHarness', () => { |
6 |
| - runHarnessTests(MatOptionModule, MatOptionHarness, MatOption); |
| 13 | +describe('MatOptionHarness', () => { |
| 14 | + let fixture: ComponentFixture<OptionHarnessTest>; |
| 15 | + let loader: HarnessLoader; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + await TestBed.configureTestingModule({ |
| 19 | + imports: [MatOptionModule], |
| 20 | + declarations: [OptionHarnessTest], |
| 21 | + }).compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(OptionHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should load all option harnesses', async () => { |
| 29 | + const options = await loader.getAllHarnesses(MatOptionHarness); |
| 30 | + expect(options.length).toBe(2); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should filter options by text', async () => { |
| 34 | + const options = await loader.getAllHarnesses(MatOptionHarness.with({text: 'Disabled option'})); |
| 35 | + expect(options.length).toBe(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should filter options text by a pattern', async () => { |
| 39 | + const options = await loader.getAllHarnesses(MatOptionHarness.with({text: /option/})); |
| 40 | + expect(options.length).toBe(2); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should filter options text by its selected state', async () => { |
| 44 | + const options = await loader.getAllHarnesses(MatOptionHarness); |
| 45 | + let selectedOptions = await loader.getAllHarnesses(MatOptionHarness.with({isSelected: true})); |
| 46 | + |
| 47 | + expect(options.length).toBe(2); |
| 48 | + expect(selectedOptions.length).toBe(0); |
| 49 | + |
| 50 | + await options[0].click(); |
| 51 | + |
| 52 | + selectedOptions = await loader.getAllHarnesses(MatOptionHarness.with({isSelected: true})); |
| 53 | + |
| 54 | + expect(selectedOptions.length).toBe(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should get the text of options', async () => { |
| 58 | + const options = await loader.getAllHarnesses(MatOptionHarness); |
| 59 | + const texts = await parallel(() => options.map(option => option.getText())); |
| 60 | + expect(texts).toEqual(['Plain option', 'Disabled option']); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should get whether an option is disabled', async () => { |
| 64 | + const options = await loader.getAllHarnesses(MatOptionHarness); |
| 65 | + const disabledStates = await parallel(() => options.map(option => option.isDisabled())); |
| 66 | + expect(disabledStates).toEqual([false, true]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should get whether an option is selected', async () => { |
| 70 | + const option = await loader.getHarness(MatOptionHarness); |
| 71 | + |
| 72 | + expect(await option.isSelected()).toBe(false); |
| 73 | + |
| 74 | + await option.click(); |
| 75 | + |
| 76 | + expect(await option.isSelected()).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should get whether an option is active', async () => { |
| 80 | + const option = await loader.getHarness(MatOptionHarness); |
| 81 | + |
| 82 | + expect(await option.isActive()).toBe(false); |
| 83 | + |
| 84 | + // Set the option as active programmatically since |
| 85 | + // it's usually the parent component that does it. |
| 86 | + fixture.componentInstance.options.first.setActiveStyles(); |
| 87 | + fixture.detectChanges(); |
| 88 | + |
| 89 | + expect(await option.isActive()).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should get whether an option is in multi-selection mode', async () => { |
| 93 | + const option = await loader.getHarness(MatOptionHarness); |
| 94 | + |
| 95 | + expect(await option.isMultiple()).toBe(false); |
| 96 | + |
| 97 | + // Options take their `multiple` state from the parent component. |
| 98 | + fixture.componentInstance.multiple = true; |
| 99 | + fixture.detectChanges(); |
| 100 | + |
| 101 | + expect(await option.isMultiple()).toBe(true); |
| 102 | + }); |
7 | 103 | });
|
| 104 | + |
| 105 | +@Component({ |
| 106 | + providers: [ |
| 107 | + { |
| 108 | + provide: MAT_OPTION_PARENT_COMPONENT, |
| 109 | + useExisting: OptionHarnessTest, |
| 110 | + }, |
| 111 | + ], |
| 112 | + template: ` |
| 113 | + <mat-option>Plain option</mat-option> |
| 114 | + <mat-option disabled>Disabled option</mat-option> |
| 115 | + `, |
| 116 | +}) |
| 117 | +class OptionHarnessTest implements MatOptionParentComponent { |
| 118 | + @ViewChildren(MatOption) options: QueryList<{setActiveStyles(): void}>; |
| 119 | + multiple = false; |
| 120 | +} |
0 commit comments