-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathoption.spec.ts
More file actions
308 lines (246 loc) · 10.5 KB
/
option.spec.ts
File metadata and controls
308 lines (246 loc) · 10.5 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component, DebugElement, signal, ChangeDetectionStrategy} from '@angular/core';
import {By} from '@angular/platform-browser';
import {
dispatchFakeEvent,
dispatchKeyboardEvent,
createKeyboardEvent,
dispatchEvent,
} from '@angular/cdk/testing/private';
import {SPACE, ENTER} from '@angular/cdk/keycodes';
import {
MatOption,
MatOptionModule,
MAT_OPTION_PARENT_COMPONENT,
MatOptionParentComponent,
} from './index';
describe('MatOption component', () => {
it('should complete the `stateChanges` stream on destroy', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionInstance: MatOption = fixture.debugElement.query(
By.directive(MatOption),
)!.componentInstance;
const completeSpy = jasmine.createSpy('complete spy');
const subscription = optionInstance._stateChanges.subscribe({complete: completeSpy});
fixture.destroy();
expect(completeSpy).toHaveBeenCalled();
subscription.unsubscribe();
});
it('should not emit to `onSelectionChange` if selecting an already-selected option', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionInstance: MatOption = fixture.debugElement.query(
By.directive(MatOption),
)!.componentInstance;
optionInstance.select();
expect(optionInstance.selected).toBe(true);
const spy = jasmine.createSpy('selection change spy');
const subscription = optionInstance.onSelectionChange.subscribe(spy);
optionInstance.select();
fixture.detectChanges();
expect(optionInstance.selected).toBe(true);
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it('should not emit to `onSelectionChange` if deselecting an unselected option', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionInstance: MatOption = fixture.debugElement.query(
By.directive(MatOption),
)!.componentInstance;
optionInstance.deselect();
expect(optionInstance.selected).toBe(false);
const spy = jasmine.createSpy('selection change spy');
const subscription = optionInstance.onSelectionChange.subscribe(spy);
optionInstance.deselect();
fixture.detectChanges();
expect(optionInstance.selected).toBe(false);
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it('should be able to set a custom id', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.componentInstance.id.set('custom-option');
fixture.detectChanges();
const optionInstance = fixture.debugElement.query(By.directive(MatOption))!.componentInstance;
expect(optionInstance.id).toBe('custom-option');
});
it('should select the option when pressing space', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionDebugElement = fixture.debugElement.query(By.directive(MatOption))!;
const optionNativeElement: HTMLElement = optionDebugElement.nativeElement;
const optionInstance: MatOption = optionDebugElement.componentInstance;
const spy = jasmine.createSpy('selection change spy');
const subscription = optionInstance.onSelectionChange.subscribe(spy);
const event = dispatchKeyboardEvent(optionNativeElement, 'keydown', SPACE);
fixture.detectChanges();
expect(spy).toHaveBeenCalled();
expect(event.defaultPrevented).toBe(true);
subscription.unsubscribe();
});
it('should select the option when pressing enter', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionDebugElement = fixture.debugElement.query(By.directive(MatOption))!;
const optionNativeElement: HTMLElement = optionDebugElement.nativeElement;
const optionInstance: MatOption = optionDebugElement.componentInstance;
const spy = jasmine.createSpy('selection change spy');
const subscription = optionInstance.onSelectionChange.subscribe(spy);
const event = dispatchKeyboardEvent(optionNativeElement, 'keydown', ENTER);
fixture.detectChanges();
expect(spy).toHaveBeenCalled();
expect(event.defaultPrevented).toBe(true);
subscription.unsubscribe();
});
it('should not do anything when pressing the selection keys with a modifier', () => {
const fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
const optionDebugElement = fixture.debugElement.query(By.directive(MatOption))!;
const optionNativeElement: HTMLElement = optionDebugElement.nativeElement;
const optionInstance: MatOption = optionDebugElement.componentInstance;
const spy = jasmine.createSpy('selection change spy');
const subscription = optionInstance.onSelectionChange.subscribe(spy);
[ENTER, SPACE].forEach(key => {
const event = createKeyboardEvent('keydown', key);
Object.defineProperty(event, 'shiftKey', {get: () => true});
dispatchEvent(optionNativeElement, event);
fixture.detectChanges();
expect(event.defaultPrevented).toBe(false);
});
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
describe('ripples', () => {
let fixture: ComponentFixture<BasicOption>;
let optionDebugElement: DebugElement;
let optionNativeElement: HTMLElement;
let optionInstance: MatOption;
beforeEach(() => {
fixture = TestBed.createComponent(BasicOption);
fixture.detectChanges();
optionDebugElement = fixture.debugElement.query(By.directive(MatOption))!;
optionNativeElement = optionDebugElement.nativeElement;
optionInstance = optionDebugElement.componentInstance;
});
it('should show ripples by default', () => {
expect(optionInstance.disableRipple)
.withContext('Expected ripples to be enabled by default')
.toBeFalsy();
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripples to show up initially')
.toBe(0);
dispatchFakeEvent(optionNativeElement, 'mousedown');
dispatchFakeEvent(optionNativeElement, 'mouseup');
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected one ripple to show up after a fake click.')
.toBe(1);
});
it('should not show ripples if the option is disabled', () => {
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripples to show up initially')
.toBe(0);
fixture.componentInstance.disabled.set(true);
fixture.detectChanges();
dispatchFakeEvent(optionNativeElement, 'mousedown');
dispatchFakeEvent(optionNativeElement, 'mouseup');
expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length)
.withContext('Expected no ripples to show up after click on a disabled option.')
.toBe(0);
});
});
describe('multi-select selected state label color token', () => {
it('should apply option-multiple-selected-state-label-text-color to selected multi-select option labels', () => {
const fixture = TestBed.createComponent(MultipleOption);
fixture.detectChanges();
const host: HTMLElement = fixture.nativeElement;
host.style.setProperty(
'--mat-option-multiple-selected-state-label-text-color',
'rgb(255, 0, 0)',
);
const optionEl: HTMLElement = host.querySelector('mat-option')!;
optionEl.click();
fixture.detectChanges();
const primaryText: HTMLElement = optionEl.querySelector('.mdc-list-item__primary-text')!;
expect(getComputedStyle(primaryText).color).toBe('rgb(255, 0, 0)');
});
it('should not change label color of selected multi-select options when the token is not set', () => {
const fixture = TestBed.createComponent(MultipleOption);
fixture.detectChanges();
const optionEl: HTMLElement = fixture.nativeElement.querySelector('mat-option')!;
const primaryText: HTMLElement = optionEl.querySelector('.mdc-list-item__primary-text')!;
const colorBefore = getComputedStyle(primaryText).color;
optionEl.click();
fixture.detectChanges();
expect(getComputedStyle(primaryText).color).toBe(colorBefore);
});
});
it('should have a focus indicator', () => {
const fixture = TestBed.createComponent(BasicOption);
const optionNativeElement = fixture.debugElement.query(By.directive(MatOption))!.nativeElement;
expect(optionNativeElement.parentElement.querySelector('.mat-focus-indicator'))
.withContext(
'expected to find a focus indicator on ' +
"either the mat-option element or one of it's children",
)
.not.toBeNull();
});
describe('inside inert group', () => {
let fixture: ComponentFixture<InsideGroup>;
beforeEach(waitForAsync(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
{
provide: MAT_OPTION_PARENT_COMPONENT,
useValue: {inertGroups: true},
},
],
});
fixture = TestBed.createComponent(InsideGroup);
fixture.detectChanges();
}));
it('should remove all accessibility-related attributes from the group', () => {
const group: HTMLElement = fixture.nativeElement.querySelector('mat-optgroup');
expect(group.hasAttribute('role')).toBe(false);
expect(group.hasAttribute('aria-disabled')).toBe(false);
expect(group.hasAttribute('aria-labelledby')).toBe(false);
});
it('should mirror the group label inside the option', () => {
const option: HTMLElement = fixture.nativeElement.querySelector('mat-option');
expect(option.textContent?.trim()).toBe('Option(Group)');
});
});
});
@Component({
template: `<mat-option [id]="id()" [disabled]="disabled()"></mat-option>`,
imports: [MatOptionModule],
changeDetection: ChangeDetectionStrategy.Eager,
})
class BasicOption {
disabled = signal(false);
id = signal('');
}
@Component({
template: `
<mat-optgroup label="Group">
<mat-option>Option</mat-option>
</mat-optgroup>
`,
imports: [MatOptionModule],
changeDetection: ChangeDetectionStrategy.Eager,
})
class InsideGroup {}
@Component({
template: `<mat-option>Option</mat-option>`,
imports: [MatOptionModule],
changeDetection: ChangeDetectionStrategy.Eager,
providers: [
{
provide: MAT_OPTION_PARENT_COMPONENT,
useValue: {multiple: true} as MatOptionParentComponent,
},
],
})
class MultipleOption {}