/
chip-option.spec.ts
331 lines (261 loc) · 10.9 KB
/
chip-option.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import {Directionality} from '@angular/cdk/bidi';
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing/private';
import {Component, DebugElement, ViewChild} from '@angular/core';
import {waitForAsync, ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
import {By} from '@angular/platform-browser';
import {Subject} from 'rxjs';
import {
MatChipEvent,
MatChipListbox,
MatChipOption,
MatChipSelectionChange,
MatChipsModule,
} from './index';
import {SPACE} from '@angular/cdk/keycodes';
describe('MDC-based Option Chips', () => {
let fixture: ComponentFixture<any>;
let chipDebugElement: DebugElement;
let chipNativeElement: HTMLElement;
let primaryAction: HTMLElement;
let chipInstance: MatChipOption;
let globalRippleOptions: RippleGlobalOptions;
let dir = 'ltr';
beforeEach(waitForAsync(() => {
globalRippleOptions = {};
TestBed.configureTestingModule({
imports: [MatChipsModule],
declarations: [SingleChip],
providers: [
{provide: MAT_RIPPLE_GLOBAL_OPTIONS, useFactory: () => globalRippleOptions},
{
provide: Directionality,
useFactory: () => ({
value: dir,
change: new Subject(),
}),
},
],
});
TestBed.compileComponents();
}));
describe('MatChipOption', () => {
let testComponent: SingleChip;
beforeEach(() => {
fixture = TestBed.createComponent(SingleChip);
fixture.detectChanges();
chipDebugElement = fixture.debugElement.query(By.directive(MatChipOption))!;
chipNativeElement = chipDebugElement.nativeElement;
chipInstance = chipDebugElement.injector.get<MatChipOption>(MatChipOption);
primaryAction = chipNativeElement.querySelector('.mdc-evolution-chip__action--primary')!;
testComponent = fixture.debugElement.componentInstance;
});
describe('basic behaviors', () => {
it('adds the `mat-chip` class', () => {
expect(chipNativeElement.classList).toContain('mat-mdc-chip');
});
it('emits focus only once for multiple clicks', () => {
let counter = 0;
chipInstance._onFocus.subscribe(() => {
counter++;
});
primaryAction.focus();
primaryAction.focus();
fixture.detectChanges();
expect(counter).toBe(1);
});
it('emits destroy on destruction', () => {
spyOn(testComponent, 'chipDestroy').and.callThrough();
// Force a destroy callback
testComponent.shouldShow = false;
fixture.detectChanges();
expect(testComponent.chipDestroy).toHaveBeenCalledTimes(1);
});
it('allows color customization', () => {
expect(chipNativeElement.classList).toContain('mat-primary');
testComponent.color = 'warn';
fixture.detectChanges();
expect(chipNativeElement.classList).not.toContain('mat-primary');
expect(chipNativeElement.classList).toContain('mat-warn');
});
it('allows selection', () => {
spyOn(testComponent, 'chipSelectionChange');
expect(chipNativeElement.classList).not.toContain('mat-mdc-chip-selected');
testComponent.selected = true;
fixture.detectChanges();
expect(chipNativeElement.classList).toContain('mat-mdc-chip-selected');
expect(testComponent.chipSelectionChange).toHaveBeenCalledWith({
source: chipInstance,
isUserInput: false,
selected: true,
});
});
it('should not prevent the default click action', () => {
const event = dispatchFakeEvent(chipNativeElement, 'click');
fixture.detectChanges();
expect(event.defaultPrevented).toBe(false);
});
it('should not dispatch `selectionChange` event when deselecting a non-selected chip', () => {
chipInstance.deselect();
const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);
chipInstance.deselect();
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it('should not dispatch `selectionChange` event when selecting a selected chip', () => {
chipInstance.select();
const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);
chipInstance.select();
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it(
'should not dispatch `selectionChange` event when selecting a selected chip via ' +
'user interaction',
() => {
chipInstance.select();
const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);
chipInstance.selectViaInteraction();
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
},
);
it('should not dispatch `selectionChange` through setter if the value did not change', () => {
chipInstance.selected = false;
const spy = jasmine.createSpy('selectionChange spy');
const subscription = chipInstance.selectionChange.subscribe(spy);
chipInstance.selected = false;
expect(spy).not.toHaveBeenCalled();
subscription.unsubscribe();
});
it('should be able to disable ripples through ripple global options at runtime', () => {
expect(chipInstance._isRippleDisabled())
.withContext('Expected chip ripples to be enabled.')
.toBe(false);
globalRippleOptions.disabled = true;
expect(chipInstance._isRippleDisabled())
.withContext('Expected chip ripples to be disabled.')
.toBe(true);
});
it('should have the correct role', () => {
expect(chipNativeElement.getAttribute('role')).toBe('presentation');
});
it('should be able to set a custom role', () => {
chipInstance.role = 'button';
fixture.detectChanges();
expect(chipNativeElement.getAttribute('role')).toBe('button');
});
});
describe('keyboard behavior', () => {
describe('when selectable is true', () => {
beforeEach(() => {
testComponent.selectable = true;
fixture.detectChanges();
});
it('should selects/deselects the currently focused chip on SPACE', () => {
const CHIP_SELECTED_EVENT: MatChipSelectionChange = {
source: chipInstance,
isUserInput: true,
selected: true,
};
const CHIP_DESELECTED_EVENT: MatChipSelectionChange = {
source: chipInstance,
isUserInput: true,
selected: false,
};
spyOn(testComponent, 'chipSelectionChange');
// Use the spacebar to select the chip
dispatchKeyboardEvent(primaryAction, 'keydown', SPACE);
fixture.detectChanges();
expect(chipInstance.selected).toBeTruthy();
expect(testComponent.chipSelectionChange).toHaveBeenCalledTimes(1);
expect(testComponent.chipSelectionChange).toHaveBeenCalledWith(CHIP_SELECTED_EVENT);
// Use the spacebar to deselect the chip
dispatchKeyboardEvent(primaryAction, 'keydown', SPACE);
fixture.detectChanges();
expect(chipInstance.selected).toBeFalsy();
expect(testComponent.chipSelectionChange).toHaveBeenCalledTimes(2);
expect(testComponent.chipSelectionChange).toHaveBeenCalledWith(CHIP_DESELECTED_EVENT);
});
it('should have correct aria-selected in single selection mode', () => {
expect(primaryAction.getAttribute('aria-selected')).toBe('false');
testComponent.selected = true;
fixture.detectChanges();
expect(primaryAction.getAttribute('aria-selected')).toBe('true');
});
it('should have the correct aria-selected in multi-selection mode', fakeAsync(() => {
testComponent.chipList.multiple = true;
flush();
fixture.detectChanges();
expect(primaryAction.getAttribute('aria-selected')).toBe('false');
testComponent.selected = true;
fixture.detectChanges();
expect(primaryAction.getAttribute('aria-selected')).toBe('true');
}));
it('should disable focus on the checkmark', fakeAsync(() => {
// The checkmark is only shown in multi selection mode.
testComponent.chipList.multiple = true;
flush();
fixture.detectChanges();
const checkmark = chipNativeElement.querySelector('.mdc-evolution-chip__checkmark-svg')!;
expect(checkmark.getAttribute('focusable')).toBe('false');
}));
});
describe('when selectable is false', () => {
beforeEach(() => {
testComponent.selectable = false;
fixture.detectChanges();
});
it('SPACE ignores selection', () => {
spyOn(testComponent, 'chipSelectionChange');
// Use the spacebar to attempt to select the chip
dispatchKeyboardEvent(primaryAction, 'keydown', SPACE);
fixture.detectChanges();
expect(chipInstance.selected).toBe(false);
expect(testComponent.chipSelectionChange).not.toHaveBeenCalled();
});
it('should not have the aria-selected attribute', () => {
expect(primaryAction.hasAttribute('aria-selected')).toBe(false);
});
});
it('should update the aria-disabled for disabled chips', () => {
expect(primaryAction.getAttribute('aria-disabled')).toBe('false');
testComponent.disabled = true;
fixture.detectChanges();
expect(primaryAction.getAttribute('aria-disabled')).toBe('true');
});
});
it('should contain a focus indicator inside the text label', () => {
const label = chipNativeElement.querySelector('.mdc-evolution-chip__text-label');
expect(label?.querySelector('.mat-mdc-focus-indicator')).toBeTruthy();
});
});
});
@Component({
template: `
<mat-chip-listbox>
<div *ngIf="shouldShow">
<mat-chip-option [selectable]="selectable"
[color]="color" [selected]="selected" [disabled]="disabled"
(destroyed)="chipDestroy($event)"
(selectionChange)="chipSelectionChange($event)">
<span class="avatar" matChipAvatar></span>
{{name}}
</mat-chip-option>
</div>
</mat-chip-listbox>`,
})
class SingleChip {
@ViewChild(MatChipListbox) chipList: MatChipListbox;
disabled: boolean = false;
name: string = 'Test';
color: string = 'primary';
selected: boolean = false;
selectable: boolean = true;
shouldShow: boolean = true;
chipDestroy: (event?: MatChipEvent) => void = () => {};
chipSelectionChange: (event?: MatChipSelectionChange) => void = () => {};
}