|
| 1 | +import {TestBed, ComponentFixture, waitForAsync, inject} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatAutocompleteHarness} from '@angular/material/autocomplete/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatAutocompleteModule} from '@angular/material/autocomplete'; |
| 8 | +import {AutocompleteHarnessExample} from './autocomplete-harness-example'; |
| 9 | +import {OverlayContainer} from '@angular/cdk/overlay'; |
| 10 | + |
| 11 | +describe('AutocompleteHarnessExample', () => { |
| 12 | + let fixture: ComponentFixture<AutocompleteHarnessExample>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + let overlayContainer: OverlayContainer; |
| 15 | + |
| 16 | + beforeAll(() => { |
| 17 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 18 | + }); |
| 19 | + |
| 20 | + beforeEach( |
| 21 | + waitForAsync(() => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [MatAutocompleteModule], |
| 24 | + declarations: [AutocompleteHarnessExample] |
| 25 | + }).compileComponents(); |
| 26 | + })); |
| 27 | + |
| 28 | + beforeEach( () => { |
| 29 | + fixture = TestBed.createComponent(AutocompleteHarnessExample); |
| 30 | + fixture.detectChanges(); |
| 31 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 32 | + inject([OverlayContainer], (oc: OverlayContainer) => { |
| 33 | + overlayContainer = oc; |
| 34 | + })(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + // Angular won't call this for us so we need to do it ourselves to avoid leaks. |
| 39 | + overlayContainer.ngOnDestroy(); |
| 40 | + overlayContainer = null!; |
| 41 | + }); |
| 42 | + |
| 43 | + it('should load all autocomplete harnesses', async () => { |
| 44 | + const autocompletes = await loader.getAllHarnesses(MatAutocompleteHarness); |
| 45 | + expect(autocompletes.length).toBe(2); |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + it('should get disabled state', async () => { |
| 50 | + const enabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'})); |
| 51 | + const disabled = await loader.getHarness(MatAutocompleteHarness.with({selector: '#disabled'})); |
| 52 | + |
| 53 | + expect(await enabled.isDisabled()).toBe(false); |
| 54 | + expect(await disabled.isDisabled()).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should focus and blur an input', async () => { |
| 58 | + const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'})); |
| 59 | + expect(await input.isFocused()).toBe(false); |
| 60 | + await input.focus(); |
| 61 | + expect(await input.isFocused()).toBe(true); |
| 62 | + await input.blur(); |
| 63 | + expect(await input.isFocused()).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should be able to type in an input', async () => { |
| 67 | + const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'})); |
| 68 | + await input.enterText('Hello there'); |
| 69 | + expect(await input.getValue()).toBe('Hello there'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should be able to get filtered options', async () => { |
| 73 | + const input = await loader.getHarness(MatAutocompleteHarness.with({selector: '#plain'})); |
| 74 | + await input.focus(); |
| 75 | + const options = await input.getOptions({text: /New/}); |
| 76 | + |
| 77 | + expect(options.length).toBe(1); |
| 78 | + expect(await options[0].getText()).toBe('New York'); |
| 79 | + }); |
| 80 | +}); |
0 commit comments