1
- import { Component , EventEmitter , Input , Output } from '@angular/core' ;
1
+ import { Component } from '@angular/core' ;
2
2
import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
3
3
4
4
import { Clipboard } from './clipboard' ;
5
5
import { ClipboardModule } from './clipboard-module' ;
6
+ import { PendingCopy } from './pending-copy' ;
6
7
7
8
const COPY_CONTENT = 'copy content' ;
8
9
@@ -11,17 +12,18 @@ const COPY_CONTENT = 'copy content';
11
12
template : `
12
13
<button
13
14
[cdkCopyToClipboard]="content"
14
- (cdkCopyToClipboardCopied)="copied.emit($event)"></button>` ,
15
+ [cdkCopyToClipboardAttempts]="attempts"
16
+ (cdkCopyToClipboardCopied)="copied($event)"></button>` ,
15
17
} )
16
18
class CopyToClipboardHost {
17
- @Input ( ) content = '' ;
18
- @Output ( ) copied = new EventEmitter < boolean > ( ) ;
19
+ content = '' ;
20
+ attempts = 1 ;
21
+ copied = jasmine . createSpy ( 'copied spy' ) ;
19
22
}
20
23
21
24
describe ( 'CdkCopyToClipboard' , ( ) => {
22
25
let fixture : ComponentFixture < CopyToClipboardHost > ;
23
- let mockCopy : jasmine . Spy ;
24
- let copiedOutput : jasmine . Spy ;
26
+ let clipboard : Clipboard ;
25
27
26
28
beforeEach ( fakeAsync ( ( ) => {
27
29
TestBed . configureTestingModule ( {
@@ -37,31 +39,69 @@ describe('CdkCopyToClipboard', () => {
37
39
38
40
const host = fixture . componentInstance ;
39
41
host . content = COPY_CONTENT ;
40
- copiedOutput = jasmine . createSpy ( 'copied' ) ;
41
- host . copied . subscribe ( copiedOutput ) ;
42
- mockCopy = spyOn ( TestBed . get ( Clipboard ) , 'copy' ) ;
43
-
42
+ clipboard = TestBed . get ( Clipboard ) ;
44
43
fixture . detectChanges ( ) ;
45
44
} ) ;
46
45
47
46
it ( 'copies content to clipboard upon click' , ( ) => {
47
+ spyOn ( clipboard , 'copy' ) ;
48
48
fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
49
-
50
- expect ( mockCopy ) . toHaveBeenCalledWith ( COPY_CONTENT ) ;
49
+ expect ( clipboard . copy ) . toHaveBeenCalledWith ( COPY_CONTENT ) ;
51
50
} ) ;
52
51
53
52
it ( 'emits copied event true when copy succeeds' , fakeAsync ( ( ) => {
54
- mockCopy . and . returnValue ( true ) ;
55
- fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
53
+ spyOn ( clipboard , 'copy' ) . and . returnValue ( true ) ;
54
+ fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
56
55
57
- expect ( copiedOutput ) . toHaveBeenCalledWith ( true ) ;
58
- } ) ) ;
56
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledWith ( true ) ;
57
+ } ) ) ;
59
58
60
59
it ( 'emits copied event false when copy fails' , fakeAsync ( ( ) => {
61
- mockCopy . and . returnValue ( false ) ;
62
- fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
63
- tick ( ) ;
60
+ spyOn ( clipboard , 'copy' ) . and . returnValue ( false ) ;
61
+ fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
62
+ tick ( ) ;
63
+
64
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledWith ( false ) ;
65
+ } ) ) ;
66
+
67
+ it ( 'should be able to attempt multiple times before succeeding' , fakeAsync ( ( ) => {
68
+ const maxAttempts = 3 ;
69
+ let attempts = 0 ;
70
+ spyOn ( clipboard , 'beginCopy' ) . and . returnValue ( {
71
+ copy : ( ) => ++ attempts >= maxAttempts ,
72
+ destroy : ( ) => { }
73
+ } as PendingCopy ) ;
74
+ fixture . componentInstance . attempts = maxAttempts ;
75
+ fixture . detectChanges ( ) ;
76
+
77
+ fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
78
+ fixture . detectChanges ( ) ;
79
+ tick ( ) ;
80
+
81
+ expect ( attempts ) . toBe ( maxAttempts ) ;
82
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledTimes ( 1 ) ;
83
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledWith ( true ) ;
84
+ } ) ) ;
85
+
86
+ it ( 'should be able to attempt multiple times before failing' , fakeAsync ( ( ) => {
87
+ const maxAttempts = 3 ;
88
+ let attempts = 0 ;
89
+ spyOn ( clipboard , 'beginCopy' ) . and . returnValue ( {
90
+ copy : ( ) => {
91
+ attempts ++ ;
92
+ return false ;
93
+ } ,
94
+ destroy : ( ) => { }
95
+ } as PendingCopy ) ;
96
+ fixture . componentInstance . attempts = maxAttempts ;
97
+ fixture . detectChanges ( ) ;
64
98
65
- expect ( copiedOutput ) . toHaveBeenCalledWith ( false ) ;
66
- } ) ) ;
99
+ fixture . nativeElement . querySelector ( 'button' ) ! . click ( ) ;
100
+ fixture . detectChanges ( ) ;
101
+ tick ( ) ;
102
+
103
+ expect ( attempts ) . toBe ( maxAttempts ) ;
104
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledTimes ( 1 ) ;
105
+ expect ( fixture . componentInstance . copied ) . toHaveBeenCalledWith ( false ) ;
106
+ } ) ) ;
67
107
} ) ;
0 commit comments