|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatAutocompleteModule} from '@angular/material/autocomplete'; |
| 6 | +import {MatAutocompleteModule as MatMdcAutocompleteModule} from '../index'; |
| 7 | +import {MatAutocompleteHarness} from './autocomplete-harness'; |
| 8 | +import {MatAutocompleteHarness as MatMdcAutocompleteHarness} from './mdc-autocomplete-harness'; |
| 9 | + |
| 10 | +let fixture: ComponentFixture<AutocompleteHarnessTest>; |
| 11 | +let loader: HarnessLoader; |
| 12 | +let harness: typeof MatAutocompleteHarness; |
| 13 | + |
| 14 | +describe('MatAutocompleteHarness', () => { |
| 15 | + describe('non-MDC-based', () => { |
| 16 | + beforeEach(async () => { |
| 17 | + await TestBed.configureTestingModule({ |
| 18 | + imports: [MatAutocompleteModule], |
| 19 | + declarations: [AutocompleteHarnessTest], |
| 20 | + }).compileComponents(); |
| 21 | + |
| 22 | + fixture = TestBed.createComponent(AutocompleteHarnessTest); |
| 23 | + fixture.detectChanges(); |
| 24 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 25 | + harness = MatAutocompleteHarness; |
| 26 | + }); |
| 27 | + |
| 28 | + runTests(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('MDC-based', () => { |
| 32 | + beforeEach(async () => { |
| 33 | + await TestBed.configureTestingModule({ |
| 34 | + imports: [MatMdcAutocompleteModule], |
| 35 | + declarations: [AutocompleteHarnessTest], |
| 36 | + }).compileComponents(); |
| 37 | + |
| 38 | + fixture = TestBed.createComponent(AutocompleteHarnessTest); |
| 39 | + fixture.detectChanges(); |
| 40 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 41 | + // Public APIs are the same as MatAutocompleteHarness, but cast |
| 42 | + // is necessary because of different private fields. |
| 43 | + harness = MatMdcAutocompleteHarness as any; |
| 44 | + }); |
| 45 | + |
| 46 | + // TODO: enable after MDC autocomplete is implemented |
| 47 | + // runTests(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +/** Shared tests to run on both the original and MDC-based autocomplete. */ |
| 52 | +function runTests() { |
| 53 | + it('should load all autocomplete harnesses', async () => { |
| 54 | + const inputs = await loader.getAllHarnesses(harness); |
| 55 | + expect(inputs.length).toBe(5); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should be able to get text inside the input', async () => { |
| 59 | + const input = await loader.getHarness(harness.with({id: 'prefilled'})); |
| 60 | + expect(await input.getText()).toBe('Prefilled value'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should get disabled state', async () => { |
| 64 | + const enabled = await loader.getHarness(harness.with({id: 'plain'})); |
| 65 | + const disabled = await loader.getHarness(harness.with({id: 'disabled'})); |
| 66 | + |
| 67 | + expect(await enabled.isDisabled()).toBe(false); |
| 68 | + expect(await disabled.isDisabled()).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should focus and blur an input', async () => { |
| 72 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 73 | + expect(getActiveElementId()).not.toBe('plain'); |
| 74 | + await input.focus(); |
| 75 | + expect(getActiveElementId()).toBe('plain'); |
| 76 | + await input.blur(); |
| 77 | + expect(getActiveElementId()).not.toBe('plain'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should be able to type in an input', async () => { |
| 81 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 82 | + await input.enterText('Hello there'); |
| 83 | + expect(await input.getText()).toBe('Hello there'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should be able to get the autocomplete panel', async () => { |
| 87 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 88 | + await input.focus(); |
| 89 | + expect(await input.getPanel()).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should be able to get the autocomplete panel options', async () => { |
| 93 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 94 | + await input.focus(); |
| 95 | + const options = await input.getOptions(); |
| 96 | + |
| 97 | + expect(options.length).toBe(11); |
| 98 | + expect(await options[5].text()).toBe('New York'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should be able to get the autocomplete panel groups', async () => { |
| 102 | + const input = await loader.getHarness(harness.with({id: 'grouped'})); |
| 103 | + await input.focus(); |
| 104 | + const groups = await input.getOptionGroups(); |
| 105 | + const options = await input.getOptions(); |
| 106 | + |
| 107 | + expect(groups.length).toBe(3); |
| 108 | + expect(options.length).toBe(11); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should be able to get the autocomplete panel', async () => { |
| 112 | + // Focusing without any options will render the panel, but it'll be invisible. |
| 113 | + fixture.componentInstance.states = []; |
| 114 | + fixture.detectChanges(); |
| 115 | + |
| 116 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 117 | + await input.focus(); |
| 118 | + expect(await input.panelIsVisible()).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should be able to get whether the autocomplete is open', async () => { |
| 122 | + const input = await loader.getHarness(harness.with({id: 'plain'})); |
| 123 | + |
| 124 | + expect(await input.isOpen()).toBe(false); |
| 125 | + await input.focus(); |
| 126 | + expect(await input.isOpen()).toBe(true); |
| 127 | + }); |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +function getActiveElementId() { |
| 132 | + return document.activeElement ? document.activeElement.id : ''; |
| 133 | +} |
| 134 | + |
| 135 | +@Component({ |
| 136 | + template: ` |
| 137 | + <mat-autocomplete #autocomplete="matAutocomplete"> |
| 138 | + <mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option> |
| 139 | + </mat-autocomplete> |
| 140 | +
|
| 141 | + <mat-autocomplete #groupedAutocomplete="matAutocomplete"> |
| 142 | + <mat-optgroup *ngFor="let group of stateGroups" [label]="group.name"> |
| 143 | + <mat-option |
| 144 | + *ngFor="let state of group.states" |
| 145 | + [value]="state.code">{{ state.name }}</mat-option> |
| 146 | + </mat-optgroup> |
| 147 | + </mat-autocomplete> |
| 148 | +
|
| 149 | + <input id="plain" [matAutocomplete]="autocomplete"> |
| 150 | + <input id="disabled" disabled [matAutocomplete]="autocomplete"> |
| 151 | + <textarea id="textarea" [matAutocomplete]="autocomplete"></textarea> |
| 152 | + <input id="prefilled" [matAutocomplete]="autocomplete" value="Prefilled value"> |
| 153 | + <input id="grouped" [matAutocomplete]="groupedAutocomplete"> |
| 154 | + ` |
| 155 | +}) |
| 156 | +class AutocompleteHarnessTest { |
| 157 | + states = [ |
| 158 | + {code: 'AL', name: 'Alabama'}, |
| 159 | + {code: 'CA', name: 'California'}, |
| 160 | + {code: 'FL', name: 'Florida'}, |
| 161 | + {code: 'KS', name: 'Kansas'}, |
| 162 | + {code: 'MA', name: 'Massachusetts'}, |
| 163 | + {code: 'NY', name: 'New York'}, |
| 164 | + {code: 'OR', name: 'Oregon'}, |
| 165 | + {code: 'PA', name: 'Pennsylvania'}, |
| 166 | + {code: 'TN', name: 'Tennessee'}, |
| 167 | + {code: 'VA', name: 'Virginia'}, |
| 168 | + {code: 'WY', name: 'Wyoming'}, |
| 169 | + ]; |
| 170 | + |
| 171 | + stateGroups = [ |
| 172 | + { |
| 173 | + name: 'One', |
| 174 | + states: this.states.slice(0, 3) |
| 175 | + }, |
| 176 | + { |
| 177 | + name: 'Two', |
| 178 | + states: this.states.slice(3, 7) |
| 179 | + }, |
| 180 | + { |
| 181 | + name: 'Three', |
| 182 | + states: this.states.slice(7) |
| 183 | + } |
| 184 | + ]; |
| 185 | +} |
| 186 | + |
0 commit comments