Repository navigation
Expand file tree
/
Copy pathangular-clipboard.spec.js
More file actions
161 lines (119 loc) · 4.91 KB
/
Copy pathangular-clipboard.spec.js
File metadata and controls
161 lines (119 loc) · 4.91 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
describe('angular-clipboard', function () {
var elm, scope;
beforeEach(angular.mock.module('angular-clipboard'));
beforeEach(angular.mock.inject(function ($rootScope, $compile) {
scope = $rootScope;
elm = $compile('<button clipboard supported="supported" text="textToCopy" on-copied="success(text)" on-error="fail(err)">Copy</button>')(scope);
scope.supported = undefined;
scope.textToCopy = 'Copy me!';
scope.copied = false;
scope.success = function (text) {
//console.log("scope.success called: ", text);
scope.copied = true;
};
scope.fail = function (err) {
//console.log(err);
};
scope.$digest();
spyOn(scope, 'success').and.callThrough();
spyOn(scope, 'fail');
}));
it('should invoke success callback after successful execCommand', function () {
spyOn(document, 'execCommand').and.returnValue(true);
elm.triggerHandler('click');
expect(scope.success).toHaveBeenCalled();
});
it('should invoke fail callback on error in execCommand', function () {
spyOn(document, 'execCommand').and.returnValue(false);
elm.triggerHandler('click');
expect(scope.fail).toHaveBeenCalledWith('failure copy');
});
it('should invoke fail callback on invalid child element', function () {
spyOn(document.body, 'appendChild').and.throwError('fake');
elm.triggerHandler('click');
expect(scope.fail).toHaveBeenCalled();
});
it('should be caught by angular\'s digest cycle', function () {
spyOn(document, 'execCommand').and.returnValue(true);
elm.triggerHandler('click');
expect(scope.copied).toEqual(true);
});
it('should export/return angular module', function () {
expect(window.angularClipboard).toBeDefined();
expect(window.angularClipboard.name).toEqual('angular-clipboard');
});
it('should feature detect and set supported', function () {
expect(scope.supported).toEqual(true);
});
});
describe('with provider', function() {
var $compile, $rootScope, element, flag;
/*
this.options = {
onCopiedDefaultCallback: false,
onErrorDefaultCallback: false
};
this.configure = function (options) {
angular.extend(this.options, options);
};
*/
describe('set with default callbacks', function() {
beforeEach(module('angular-clipboard', function (angularClipboardProvider) {
flag = false;
function onCopied(text) {
flag = text;
//console.log("default onCopied called: ", text);
}
function onError(err) {
flag = err;
}
angularClipboardProvider.configure({
onCopiedDefaultCallback: onCopied,
onErrorDefaultCallback: onError
});
}));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$rootScope.textToCopy = 'Copy me!';
$rootScope.success = function() {
flag = 'override copied';
}
$rootScope.fail = function(err) {
flag = 'override fail';
}
}));
describe('and no callbacks in attributes', function() {
beforeEach(function() {
element = $compile('<button clipboard text="textToCopy">Copy</button>')($rootScope);
$rootScope.$digest();
});
it('should call the default on-copied', function () {
spyOn(document, 'execCommand').and.returnValue(true);
element.triggerHandler('click');
expect(flag).toBe($rootScope.textToCopy);
});
it('should call the default on-error', function () {
spyOn(document, 'execCommand').and.returnValue(false);
element.triggerHandler('click');
expect(flag).toBe('failure copy');
});
});
describe('but with callbacks in attributes', function() {
beforeEach(function() {
element = $compile('<button clipboard text="textToCopy" on-copied="success()" on-error="fail(err)">Copy</button>')($rootScope);
$rootScope.$digest();
});
it('should override the default success callback', function() {
spyOn(document, 'execCommand').and.returnValue(true);
element.triggerHandler('click');
expect(flag).toBe('override copied');
})
it('should override the default error callback', function() {
spyOn(document, 'execCommand').and.returnValue(false);
element.triggerHandler('click');
expect(flag).toBe('override fail');
})
});
});
});