Skip to content

Commit d26a0be

Browse files
authored
refactor(cdk/clipboard): switch tests away from fakeAsync (#33585)
Reworks the clipboard tests not to use `fakeAsync`.
1 parent e93f48e commit d26a0be

1 file changed

Lines changed: 44 additions & 40 deletions

File tree

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
import {Component, ChangeDetectionStrategy} from '@angular/core';
2-
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
33

44
import {Clipboard} from './clipboard';
55
import {ClipboardModule} from './clipboard-module';
66
import {PendingCopy} from './pending-copy';
77

8-
const COPY_CONTENT = 'copy content';
9-
10-
@Component({
11-
selector: 'copy-to-clipboard-host',
12-
template: `
13-
<button
14-
[cdkCopyToClipboard]="content"
15-
[cdkCopyToClipboardAttempts]="attempts"
16-
(cdkCopyToClipboardCopied)="copied($event)"></button>`,
17-
imports: [ClipboardModule],
18-
changeDetection: ChangeDetectionStrategy.Eager,
19-
})
20-
class CopyToClipboardHost {
21-
content = '';
22-
attempts = 1;
23-
copied = jasmine.createSpy('copied spy');
24-
}
25-
268
describe('CdkCopyToClipboard', () => {
9+
const COPY_CONTENT = 'copy content';
2710
let fixture: ComponentFixture<CopyToClipboardHost>;
2811
let clipboard: Clipboard;
2912

@@ -43,22 +26,22 @@ describe('CdkCopyToClipboard', () => {
4326
expect(clipboard.copy).toHaveBeenCalledWith(COPY_CONTENT);
4427
});
4528

46-
it('emits copied event true when copy succeeds', fakeAsync(() => {
29+
it('emits copied event true when copy succeeds', () => {
4730
spyOn(clipboard, 'copy').and.returnValue(true);
4831
fixture.nativeElement.querySelector('button')!.click();
4932

5033
expect(fixture.componentInstance.copied).toHaveBeenCalledWith(true);
51-
}));
34+
});
5235

53-
it('emits copied event false when copy fails', fakeAsync(() => {
36+
it('emits copied event false when copy fails', async () => {
5437
spyOn(clipboard, 'copy').and.returnValue(false);
5538
fixture.nativeElement.querySelector('button')!.click();
56-
tick(1);
39+
await fixture.whenStable();
5740

5841
expect(fixture.componentInstance.copied).toHaveBeenCalledWith(false);
59-
}));
42+
});
6043

61-
it('should be able to attempt multiple times before succeeding', fakeAsync(() => {
44+
it('should be able to attempt multiple times before succeeding', async () => {
6245
const maxAttempts = 3;
6346
let attempts = 0;
6447
spyOn(clipboard, 'beginCopy').and.returnValue({
@@ -71,14 +54,14 @@ describe('CdkCopyToClipboard', () => {
7154

7255
fixture.nativeElement.querySelector('button')!.click();
7356
fixture.detectChanges();
74-
tick(3);
57+
await wait(100);
7558

7659
expect(attempts).toBe(maxAttempts);
7760
expect(fixture.componentInstance.copied).toHaveBeenCalledTimes(1);
7861
expect(fixture.componentInstance.copied).toHaveBeenCalledWith(true);
79-
}));
62+
});
8063

81-
it('should be able to attempt multiple times before failing', fakeAsync(() => {
64+
it('should be able to attempt multiple times before failing', async () => {
8265
const maxAttempts = 3;
8366
let attempts = 0;
8467
spyOn(clipboard, 'beginCopy').and.returnValue({
@@ -94,37 +77,58 @@ describe('CdkCopyToClipboard', () => {
9477

9578
fixture.nativeElement.querySelector('button')!.click();
9679
fixture.detectChanges();
97-
tick(3);
80+
await wait(100);
9881

9982
expect(attempts).toBe(maxAttempts);
10083
expect(fixture.componentInstance.copied).toHaveBeenCalledTimes(1);
10184
expect(fixture.componentInstance.copied).toHaveBeenCalledWith(false);
102-
}));
85+
});
10386

104-
it('should destroy any pending copies when the directive is destroyed', fakeAsync(() => {
87+
it('should destroy any pending copies when the directive is destroyed', async () => {
10588
const fakeCopy = {
106-
copy: jasmine.createSpy('copy spy').and.returnValue(false) as () => boolean,
107-
destroy: jasmine.createSpy('destroy spy') as () => void,
108-
} as PendingCopy;
89+
copy: jasmine.createSpy('copy spy').and.returnValue(false),
90+
destroy: jasmine.createSpy('destroy spy'),
91+
};
10992

11093
fixture.componentInstance.attempts = 10;
11194
fixture.changeDetectorRef.markForCheck();
11295
fixture.detectChanges();
11396

114-
spyOn(clipboard, 'beginCopy').and.returnValue(fakeCopy);
97+
spyOn(clipboard, 'beginCopy').and.returnValue(fakeCopy as unknown as PendingCopy);
11598
fixture.detectChanges();
11699

117100
fixture.nativeElement.querySelector('button')!.click();
118101
fixture.detectChanges();
119-
tick(1);
102+
await wait(5);
120103

121-
expect(fakeCopy.copy).toHaveBeenCalledTimes(2);
104+
const initialCallCount = fakeCopy.copy.calls.count();
105+
expect(initialCallCount).toBeGreaterThan(0);
122106
expect(fakeCopy.destroy).toHaveBeenCalledTimes(0);
123107

124108
fixture.destroy();
125-
tick(1);
109+
await wait(50);
126110

127-
expect(fakeCopy.copy).toHaveBeenCalledTimes(2);
111+
expect(fakeCopy.copy.calls.count()).toBe(initialCallCount);
128112
expect(fakeCopy.destroy).toHaveBeenCalledTimes(1);
129-
}));
113+
});
130114
});
115+
116+
function wait(milliseconds: number) {
117+
return new Promise(resolve => setTimeout(resolve, milliseconds));
118+
}
119+
120+
@Component({
121+
selector: 'copy-to-clipboard-host',
122+
template: `
123+
<button
124+
[cdkCopyToClipboard]="content"
125+
[cdkCopyToClipboardAttempts]="attempts"
126+
(cdkCopyToClipboardCopied)="copied($event)"></button>`,
127+
imports: [ClipboardModule],
128+
changeDetection: ChangeDetectionStrategy.Eager,
129+
})
130+
class CopyToClipboardHost {
131+
content = '';
132+
attempts = 1;
133+
copied = jasmine.createSpy('copied spy');
134+
}

0 commit comments

Comments
 (0)