Skip to content

Commit 514d6f0

Browse files
committed
test(cdk-experimental/selection): switch tests away from fakeAsync (#33490)
Reworks the tests in the CDK selection module not to depend on `fakeAsync` anymore. (cherry picked from commit 909fb31)
1 parent 918090a commit 514d6f0

1 file changed

Lines changed: 54 additions & 57 deletions

File tree

src/cdk-experimental/selection/selection.spec.ts

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
inject,
99
ChangeDetectionStrategy,
1010
} from '@angular/core';
11-
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
11+
import {ComponentFixture, TestBed} from '@angular/core/testing';
1212

1313
import {CdkSelection} from './selection';
1414
import {CdkSelectionModule} from './selection-module';
@@ -64,44 +64,44 @@ describe('CdkSelection', () => {
6464
});
6565

6666
describe('cdkSelectAll', () => {
67-
it('should select all items when not all selected', fakeAsync(() => {
67+
it('should select all items when not all selected', async () => {
6868
expect(component.cdkSelection.isAllSelected()).toBeFalsy();
6969
expect(component.getSelectAll().textContent.trim()).toBe('unchecked');
7070

71-
component.clickSelectAll();
71+
await component.clickSelectAll();
7272

7373
expect(component.cdkSelection.isAllSelected()).toBeTruthy();
7474
expect(component.getSelectAll().textContent.trim()).toBe('checked');
75-
}));
75+
});
7676

77-
it('should de-select all items when all selected', fakeAsync(() => {
77+
it('should de-select all items when all selected', async () => {
7878
// Select all items.
79-
component.clickSelectAll();
79+
await component.clickSelectAll();
8080

8181
expect(component.cdkSelection.isAllSelected()).toBeTruthy();
8282
expect(component.getSelectAll().textContent.trim()).toBe('checked');
8383

84-
component.clickSelectAll();
84+
await component.clickSelectAll();
8585

8686
expect(component.cdkSelection.isAllSelected()).toBeFalsy();
8787
expect(component.getSelectAll().textContent.trim()).toBe('unchecked');
88-
}));
88+
});
8989

90-
it('should de-select all items when partially selected', fakeAsync(() => {
90+
it('should de-select all items when partially selected', async () => {
9191
// make the 1st item selected.
9292
component.clickSelectionToggle(0);
9393

9494
expect(component.cdkSelection.isPartialSelected()).toBeTruthy();
9595
expect(component.getSelectAll().textContent.trim()).toBe('indeterminate');
9696

97-
component.clickSelectAll();
97+
await component.clickSelectAll();
9898

9999
expect(component.cdkSelection.isAllSelected()).toBeFalsy();
100100
expect(component.cdkSelection.isPartialSelected()).toBeFalsy();
101101
expect(component.getSelectAll().textContent.trim()).toBe('unchecked');
102-
}));
102+
});
103103

104-
it('should respond to selection toggle clicks', fakeAsync(() => {
104+
it('should respond to selection toggle clicks', () => {
105105
// Start with no selection.
106106
expect(component.cdkSelection.isAllSelected()).toBeFalsy();
107107
expect(component.getSelectAll().textContent.trim()).toBe('unchecked');
@@ -123,10 +123,10 @@ describe('CdkSelection', () => {
123123
expect(component.cdkSelection.isAllSelected()).toBeTruthy();
124124
expect(component.cdkSelection.isPartialSelected()).toBeFalsy();
125125
expect(component.getSelectAll().textContent.trim()).toBe('checked');
126-
}));
126+
});
127127

128-
it('should emit the correct selection change events', fakeAsync(() => {
129-
component.clickSelectAll();
128+
it('should emit the correct selection change events', async () => {
129+
await component.clickSelectAll();
130130

131131
expect(component.selectionChange!.before).toEqual([]);
132132
expect(component.selectionChange!.after).toEqual([
@@ -136,7 +136,7 @@ describe('CdkSelection', () => {
136136
{value: 'durian', index: 3},
137137
]);
138138

139-
component.clickSelectAll();
139+
await component.clickSelectAll();
140140

141141
expect(component.selectionChange!.before).toEqual([
142142
{value: 'apple', index: 0},
@@ -145,35 +145,35 @@ describe('CdkSelection', () => {
145145
{value: 'durian', index: 3},
146146
]);
147147
expect(component.selectionChange!.after).toEqual([]);
148-
}));
148+
});
149149
});
150150

151151
describe('cdkSelectionToggle', () => {
152-
it('should respond to select-all toggle click', fakeAsync(() => {
152+
it('should respond to select-all toggle click', async () => {
153153
// All items not unchecked.
154154
expect(component.getSelectionToggle(0).textContent.trim()).toBe('unchecked');
155155
expect(component.getSelectionToggle(1).textContent.trim()).toBe('unchecked');
156156
expect(component.getSelectionToggle(2).textContent.trim()).toBe('unchecked');
157157
expect(component.getSelectionToggle(3).textContent.trim()).toBe('unchecked');
158158

159-
component.clickSelectAll();
159+
await component.clickSelectAll();
160160

161161
// Everything selected.
162162
expect(component.getSelectionToggle(0).textContent.trim()).toBe('checked');
163163
expect(component.getSelectionToggle(1).textContent.trim()).toBe('checked');
164164
expect(component.getSelectionToggle(2).textContent.trim()).toBe('checked');
165165
expect(component.getSelectionToggle(3).textContent.trim()).toBe('checked');
166166

167-
component.clickSelectAll();
167+
await component.clickSelectAll();
168168

169169
// Everything unselected.
170170
expect(component.getSelectionToggle(0).textContent.trim()).toBe('unchecked');
171171
expect(component.getSelectionToggle(1).textContent.trim()).toBe('unchecked');
172172
expect(component.getSelectionToggle(2).textContent.trim()).toBe('unchecked');
173173
expect(component.getSelectionToggle(3).textContent.trim()).toBe('unchecked');
174-
}));
174+
});
175175

176-
it('should select unselected item when clicked', fakeAsync(() => {
176+
it('should select unselected item when clicked', () => {
177177
expect(component.cdkSelection.isSelected('apple', 0)).toBeFalsy();
178178
expect(component.getSelectionToggle(0).textContent.trim()).toBe('unchecked');
179179

@@ -189,11 +189,11 @@ describe('CdkSelection', () => {
189189
expect(component.getSelectionToggle(1).textContent.trim()).toBe('unchecked');
190190
expect(component.getSelectionToggle(2).textContent.trim()).toBe('unchecked');
191191
expect(component.getSelectionToggle(3).textContent.trim()).toBe('unchecked');
192-
}));
192+
});
193193

194-
it('should de-selected selected item when clicked', fakeAsync(() => {
194+
it('should de-selected selected item when clicked', async () => {
195195
// Make all items selected.
196-
component.clickSelectAll();
196+
await component.clickSelectAll();
197197

198198
component.clickSelectionToggle(1);
199199

@@ -207,9 +207,9 @@ describe('CdkSelection', () => {
207207
expect(component.getSelectionToggle(0).textContent.trim()).toBe('checked');
208208
expect(component.getSelectionToggle(2).textContent.trim()).toBe('checked');
209209
expect(component.getSelectionToggle(3).textContent.trim()).toBe('checked');
210-
}));
210+
});
211211

212-
it('should emit the correct selection change events', fakeAsync(() => {
212+
it('should emit the correct selection change events', () => {
213213
component.clickSelectionToggle(1);
214214

215215
expect(component.selectionChange!.before).toEqual([]);
@@ -230,7 +230,7 @@ describe('CdkSelection', () => {
230230
{value: 'cherry', index: 2},
231231
]);
232232
expect(component.selectionChange!.after).toEqual([{value: 'banana', index: 1}]);
233-
}));
233+
});
234234
});
235235
});
236236

@@ -244,7 +244,7 @@ describe('CdkSelection with multiple = false', () => {
244244
fixture.detectChanges();
245245
});
246246

247-
it('should uncheck the previous selection when selecting new item', fakeAsync(() => {
247+
it('should uncheck the previous selection when selecting new item', () => {
248248
// Everything start as unchecked.
249249
expect(component.getSelectionToggle(0).textContent.trim()).toBe('unchecked');
250250
expect(component.getSelectionToggle(1).textContent.trim()).toBe('unchecked');
@@ -273,9 +273,9 @@ describe('CdkSelection with multiple = false', () => {
273273
expect(component.getSelectionToggle(1).textContent.trim()).toBe('unchecked');
274274
expect(component.getSelectionToggle(2).textContent.trim()).toBe('unchecked');
275275
expect(component.getSelectionToggle(3).textContent.trim()).toBe('unchecked');
276-
}));
276+
});
277277

278-
it('should emit the correct selection change events', fakeAsync(() => {
278+
it('should emit the correct selection change events', () => {
279279
component.clickSelectionToggle(1);
280280

281281
expect(component.selectionChange!.before).toEqual([]);
@@ -290,19 +290,19 @@ describe('CdkSelection with multiple = false', () => {
290290

291291
expect(component.selectionChange!.before).toEqual([{value: 'cherry', index: 2}]);
292292
expect(component.selectionChange!.after).toEqual([]);
293-
}));
293+
});
294294
});
295295

296296
describe('cdkSelectionColumn', () => {
297297
let fixture: ComponentFixture<MultiSelectTableWithSelectionColumn>;
298298
let component: MultiSelectTableWithSelectionColumn;
299299

300-
beforeEach(fakeAsync(() => {
300+
beforeEach(async () => {
301301
fixture = TestBed.createComponent(MultiSelectTableWithSelectionColumn);
302302
component = fixture.componentInstance;
303303
fixture.detectChanges();
304-
flush();
305-
}));
304+
await fixture.whenStable();
305+
});
306306

307307
it('should show check boxes', () => {
308308
const checkboxes =
@@ -311,23 +311,23 @@ describe('cdkSelectionColumn', () => {
311311
expect(checkboxes.length).toBe(5);
312312
});
313313

314-
it('should allow select all', fakeAsync(() => {
314+
it('should allow select all', async () => {
315315
expect(component.getSelectAll().checked).toBe(false);
316316
expect(component.getSelectionToggle(0).checked).toBe(false);
317317
expect(component.getSelectionToggle(1).checked).toBe(false);
318318
expect(component.getSelectionToggle(2).checked).toBe(false);
319319
expect(component.getSelectionToggle(3).checked).toBe(false);
320320

321-
component.clickSelectAll();
321+
await component.clickSelectAll();
322322

323323
expect(component.getSelectAll().checked).toBe(true);
324324
expect(component.getSelectionToggle(0).checked).toBe(true);
325325
expect(component.getSelectionToggle(1).checked).toBe(true);
326326
expect(component.getSelectionToggle(2).checked).toBe(true);
327327
expect(component.getSelectionToggle(3).checked).toBe(true);
328-
}));
328+
});
329329

330-
it('should allow toggle rows', fakeAsync(() => {
330+
it('should allow toggle rows', () => {
331331
expect(component.getSelectAll().checked).toBe(false);
332332
expect(component.getSelectAll().indeterminate).toBe(false);
333333
expect(component.getSelectionToggle(0).checked).toBe(false);
@@ -347,10 +347,10 @@ describe('cdkSelectionColumn', () => {
347347
expect(component.getSelectionToggle(1).checked).toBe(true);
348348
expect(component.getSelectionToggle(2).checked).toBe(true);
349349
expect(component.getSelectionToggle(3).checked).toBe(true);
350-
}));
350+
});
351351

352352
describe('cdkRowSelection', () => {
353-
it('should set .cdk-selected on selected rows', fakeAsync(() => {
353+
it('should set .cdk-selected on selected rows', () => {
354354
expect(component.getRow(0).classList.contains('cdk-selected')).toBeFalsy();
355355
expect(component.getRow(1).classList.contains('cdk-selected')).toBeFalsy();
356356
expect(component.getRow(2).classList.contains('cdk-selected')).toBeFalsy();
@@ -363,9 +363,9 @@ describe('cdkSelectionColumn', () => {
363363
component.clickSelectionToggle(0);
364364

365365
expect(component.getRow(0).classList.contains('cdk-selected')).toBeFalsy();
366-
}));
366+
});
367367

368-
it('should set aria-selected on selected rows', fakeAsync(() => {
368+
it('should set aria-selected on selected rows', () => {
369369
expect(component.getRow(0).getAttribute('aria-selected')).toBe('false');
370370
expect(component.getRow(1).getAttribute('aria-selected')).toBe('false');
371371
expect(component.getRow(2).getAttribute('aria-selected')).toBe('false');
@@ -378,7 +378,7 @@ describe('cdkSelectionColumn', () => {
378378
component.clickSelectionToggle(0);
379379

380380
expect(component.getRow(0).getAttribute('aria-selected')).toBe('false');
381-
}));
381+
});
382382
});
383383
});
384384

@@ -396,7 +396,7 @@ describe('cdkSelectionColumn with multiple = false', () => {
396396
expect(component.elementRef.nativeElement.querySelector('input[cdkselectall]')).toBe(null);
397397
});
398398

399-
it('should allow selecting one single row', fakeAsync(() => {
399+
it('should allow selecting one single row', () => {
400400
expect(component.getSelectionToggle(0).checked).toBe(false);
401401
expect(component.getSelectionToggle(1).checked).toBe(false);
402402
expect(component.getSelectionToggle(2).checked).toBe(false);
@@ -413,9 +413,13 @@ describe('cdkSelectionColumn with multiple = false', () => {
413413

414414
component.clickSelectionToggle(1);
415415
expect(component.getSelectionToggle(1).checked).toBe(false);
416-
}));
416+
});
417417
});
418418

419+
function wait(milliseconds: number) {
420+
return new Promise(resolve => setTimeout(resolve, milliseconds));
421+
}
422+
419423
@Component({
420424
template: `
421425
<ul cdkSelection [dataSource]="data" [cdkSelectionMultiple]="true"
@@ -458,9 +462,9 @@ class ListWithMultiSelection {
458462
}
459463
}
460464

461-
clickSelectAll() {
465+
async clickSelectAll() {
462466
this.getSelectAll().click();
463-
flush();
467+
await wait(0);
464468
this._cdr.detectChanges();
465469
}
466470

@@ -471,7 +475,6 @@ class ListWithMultiSelection {
471475
}
472476

473477
toggle.click();
474-
flush();
475478
this._cdr.detectChanges();
476479
}
477480

@@ -519,7 +522,6 @@ class ListWithSingleSelection {
519522
}
520523

521524
toggle.click();
522-
flush();
523525
this._cdr.detectChanges();
524526
}
525527

@@ -564,11 +566,10 @@ class MultiSelectTableWithSelectionColumn {
564566
}
565567
}
566568

567-
clickSelectAll() {
569+
async clickSelectAll() {
568570
this.getSelectAll().click();
569-
flush();
571+
await wait(0);
570572
this._cdr.detectChanges();
571-
flush();
572573
}
573574

574575
clickSelectionToggle(index: number) {
@@ -578,9 +579,7 @@ class MultiSelectTableWithSelectionColumn {
578579
}
579580

580581
toggle.click();
581-
flush();
582582
this._cdr.detectChanges();
583-
flush();
584583
}
585584

586585
getSelectAll(): HTMLInputElement {
@@ -629,9 +628,7 @@ class SingleSelectTableWithSelectionColumn {
629628
}
630629

631630
toggle.click();
632-
flush();
633631
this._cdr.detectChanges();
634-
flush();
635632
}
636633

637634
getSelectionToggle(index: number): HTMLInputElement {

0 commit comments

Comments
 (0)