/
shared.spec.ts
292 lines (235 loc) · 9.63 KB
/
shared.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {OverlayContainer} from '@angular/cdk/overlay';
import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {FormControl, ReactiveFormsModule, Validators} from '@angular/forms';
import {MatFormFieldModule} from '@angular/material/form-field';
import {MatSelectModule} from '@angular/material/select';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MatSelectHarness} from './select-harness';
/** Shared tests to run on both the original and MDC-based select. */
export function runHarnessTests(
selectModule: typeof MatSelectModule, selectHarness: typeof MatSelectHarness) {
let fixture: ComponentFixture<SelectHarnessTest>;
let loader: HarnessLoader;
let overlayContainer: OverlayContainer;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
selectModule,
MatFormFieldModule,
NoopAnimationsModule,
ReactiveFormsModule,
],
declarations: [SelectHarnessTest],
}).compileComponents();
fixture = TestBed.createComponent(SelectHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
})();
});
afterEach(() => {
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
overlayContainer.ngOnDestroy();
overlayContainer = null!;
});
it('should load all select harnesses', async () => {
const selects = await loader.getAllHarnesses(selectHarness);
expect(selects.length).toBe(4);
});
it('should be able to check whether a select is in multi-selection mode', async () => {
const singleSelection = await loader.getHarness(selectHarness.with({
selector: '#single-selection'
}));
const multipleSelection =
await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
expect(await singleSelection.isMultiple()).toBe(false);
expect(await multipleSelection.isMultiple()).toBe(true);
});
it('should get disabled state', async () => {
const singleSelection = await loader.getHarness(selectHarness.with({
selector: '#single-selection'
}));
const multipleSelection =
await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
expect(await singleSelection.isDisabled()).toBe(false);
expect(await multipleSelection.isDisabled()).toBe(false);
fixture.componentInstance.isDisabled = true;
fixture.detectChanges();
expect(await singleSelection.isDisabled()).toBe(true);
expect(await multipleSelection.isDisabled()).toBe(false);
});
it('should get required state', async () => {
const singleSelection = await loader.getHarness(selectHarness.with({
selector: '#single-selection'
}));
const multipleSelection =
await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
expect(await singleSelection.isRequired()).toBe(false);
expect(await multipleSelection.isRequired()).toBe(false);
fixture.componentInstance.isRequired = true;
fixture.detectChanges();
expect(await singleSelection.isRequired()).toBe(true);
expect(await multipleSelection.isRequired()).toBe(false);
});
it('should get valid state', async () => {
const singleSelection = await loader.getHarness(selectHarness.with({
selector: '#single-selection'
}));
const withFormControl = await loader.getHarness(selectHarness.with({
selector: '#with-form-control'
}));
expect(await singleSelection.isValid()).toBe(true);
expect(await withFormControl.isValid()).toBe(false);
});
it('should focus and blur a select', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
expect(getActiveElementId()).not.toBe('single-selection');
await select.focus();
expect(getActiveElementId()).toBe('single-selection');
await select.blur();
expect(getActiveElementId()).not.toBe('single-selection');
});
it('should be able to open and close a single-selection select', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
expect(await select.isOpen()).toBe(false);
await select.open();
expect(await select.isOpen()).toBe(true);
await select.close();
expect(await select.isOpen()).toBe(false);
});
it('should be able to open and close a multi-selection select', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
expect(await select.isOpen()).toBe(false);
await select.open();
expect(await select.isOpen()).toBe(true);
await select.close();
expect(await select.isOpen()).toBe(false);
});
it('should be able to get the select panel', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
await select.open();
expect(await select.getPanel()).toBeTruthy();
});
it('should be able to get the select options', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
await select.open();
const options = await select.getOptions();
expect(options.length).toBe(11);
expect(await options[5].getText()).toBe('New York');
});
it('should be able to get the select panel groups', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#grouped'}));
await select.open();
const groups = await select.getOptionGroups();
const options = await select.getOptions();
expect(groups.length).toBe(3);
expect(options.length).toBe(11);
});
it('should be able to get the value text from a single-selection select', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
await select.open();
const options = await select.getOptions();
await options[3].click();
expect(await select.getValueText()).toBe('Kansas');
});
it('should be able to get the value text from a multi-selection select', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
await select.open();
const options = await select.getOptions();
await options[3].click();
await options[5].click();
expect(await select.getValueText()).toBe('Kansas, New York');
});
it('should be able to get whether a single-selection select is empty', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#single-selection'}));
expect(await select.isEmpty()).toBe(true);
await select.open();
const options = await select.getOptions();
await options[3].click();
expect(await select.isEmpty()).toBe(false);
});
it('should be able to get whether a multi-selection select is empty', async () => {
const select = await loader.getHarness(selectHarness.with({selector: '#multiple-selection'}));
expect(await select.isEmpty()).toBe(true);
await select.open();
const options = await select.getOptions();
await options[3].click();
await options[5].click();
expect(await select.isEmpty()).toBe(false);
});
it('should be able to click an option', async () => {
const control = fixture.componentInstance.formControl;
const select = await loader.getHarness(selectHarness.with({selector: '#with-form-control'}));
expect(control.value).toBeFalsy();
await select.open();
await (await select.getOptions())[1].click();
expect(control.value).toBe('CA');
});
}
function getActiveElementId() {
return document.activeElement ? document.activeElement.id : '';
}
@Component({
template: `
<mat-form-field>
<mat-select [disabled]="isDisabled" [required]="isRequired" id="single-selection">
<mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select multiple id="multiple-selection">
<mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select id="grouped">
<mat-optgroup *ngFor="let group of stateGroups" [label]="group.name">
<mat-option
*ngFor="let state of group.states"
[value]="state.code">{{ state.name }}</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select [formControl]="formControl" id="with-form-control">
<mat-option *ngFor="let state of states" [value]="state.code">{{ state.name }}</mat-option>
</mat-select>
</mat-form-field>
`
})
class SelectHarnessTest {
formControl = new FormControl(undefined, [Validators.required]);
isDisabled = false;
isRequired = false;
states = [
{code: 'AL', name: 'Alabama'},
{code: 'CA', name: 'California'},
{code: 'FL', name: 'Florida'},
{code: 'KS', name: 'Kansas'},
{code: 'MA', name: 'Massachusetts'},
{code: 'NY', name: 'New York'},
{code: 'OR', name: 'Oregon'},
{code: 'PA', name: 'Pennsylvania'},
{code: 'TN', name: 'Tennessee'},
{code: 'VA', name: 'Virginia'},
{code: 'WY', name: 'Wyoming'},
];
stateGroups = [
{
name: 'One',
states: this.states.slice(0, 3)
},
{
name: 'Two',
states: this.states.slice(3, 7)
},
{
name: 'Three',
states: this.states.slice(7)
}
];
}