forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimeoutSpec.js
202 lines (139 loc) · 6.23 KB
/
timeoutSpec.js
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
'use strict';
describe('$timeout', function() {
beforeEach(module(provideLog));
it('should delegate functions to $browser.defer', inject(function($timeout, $browser) {
var counter = 0;
$timeout(function() { counter++; });
expect(counter).toBe(0);
$browser.defer.flush();
expect(counter).toBe(1);
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed!');
expect(counter).toBe(1);
}));
it('should call $apply after each callback is executed', inject(function($timeout, $rootScope) {
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
$timeout(function() {});
expect(applySpy).not.toHaveBeenCalled();
$timeout.flush();
expect(applySpy).toHaveBeenCalledOnce();
applySpy.reset();
$timeout(function() {});
$timeout(function() {});
$timeout.flush();
expect(applySpy.callCount).toBe(2);
}));
it('should NOT call $apply if skipApply is set to true', inject(function($timeout, $rootScope) {
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
$timeout(function() {}, 12, false);
expect(applySpy).not.toHaveBeenCalled();
$timeout.flush();
expect(applySpy).not.toHaveBeenCalled();
}));
it('should allow you to specify the delay time', inject(function($timeout, $browser) {
var defer = spyOn($browser, 'defer');
$timeout(noop, 123);
expect(defer.callCount).toEqual(1);
expect(defer.mostRecentCall.args[1]).toEqual(123);
}));
it('should return a promise which will be resolved with return value of the timeout callback',
inject(function($timeout, log) {
var promise = $timeout(function() { log('timeout'); return 'buba'; });
promise.then(function(value) { log('promise success: ' + value); }, log.fn('promise error'));
expect(log).toEqual([]);
$timeout.flush();
expect(log).toEqual(['timeout', 'promise success: buba']);
}));
it('should forget references to deferreds when callback called even if skipApply is true',
inject(function($timeout, $browser) {
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
var cancelSpy = spyOn($browser.defer, 'cancel').andCallThrough();
var promise1 = $timeout(function() {}, 0, false);
var promise2 = $timeout(function() {}, 100, false);
expect(cancelSpy).not.toHaveBeenCalled();
$timeout.flushNext(0);
// Promise1 deferred object should already be removed from the list and not cancellable
$timeout.cancel(promise1);
expect(cancelSpy).not.toHaveBeenCalled();
// Promise2 deferred object should not have been called and should be cancellable
$timeout.cancel(promise2);
expect(cancelSpy).toHaveBeenCalled();
}));
describe('exception handling', function() {
beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));
it('should delegate exception to the $exceptionHandler service', inject(
function($timeout, $exceptionHandler) {
$timeout(function() {throw "Test Error";});
expect($exceptionHandler.errors).toEqual([]);
$timeout.flush();
expect($exceptionHandler.errors).toEqual(["Test Error"]);
}));
it('should call $apply even if an exception is thrown in callback', inject(
function($timeout, $rootScope) {
var applySpy = spyOn($rootScope, '$apply').andCallThrough();
$timeout(function() {throw "Test Error";});
expect(applySpy).not.toHaveBeenCalled();
$timeout.flush();
expect(applySpy).toHaveBeenCalled();
}));
it('should reject the timeout promise when an exception is thrown in the timeout callback',
inject(function($timeout, log) {
var promise = $timeout(function() { throw "Some Error"; });
promise.then(log.fn('success'), function(reason) { log('error: ' + reason); });
$timeout.flush();
expect(log).toEqual('error: Some Error');
}));
it('should forget references to relevant deferred even when exception is thrown',
inject(function($timeout, $browser) {
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
var cancelSpy = spyOn($browser.defer, 'cancel').andCallThrough();
var promise = $timeout(function() { throw "Test Error"; }, 0, false);
$timeout.flush();
expect(cancelSpy).not.toHaveBeenCalled();
$timeout.cancel(promise);
expect(cancelSpy).not.toHaveBeenCalled();
}));
});
describe('cancel', function() {
it('should cancel tasks', inject(function($timeout) {
var task1 = jasmine.createSpy('task1'),
task2 = jasmine.createSpy('task2'),
task3 = jasmine.createSpy('task3'),
promise1, promise3;
promise1 = $timeout(task1);
$timeout(task2);
promise3 = $timeout(task3, 333);
$timeout.cancel(promise3);
$timeout.cancel(promise1);
$timeout.flush();
expect(task1).not.toHaveBeenCalled();
expect(task2).toHaveBeenCalledOnce();
expect(task3).not.toHaveBeenCalled();
}));
it('should return true if a task was successfully canceled', inject(function($timeout) {
var task1 = jasmine.createSpy('task1'),
task2 = jasmine.createSpy('task2'),
promise1, promise2;
promise1 = $timeout(task1);
$timeout.flush();
promise2 = $timeout(task2);
expect($timeout.cancel(promise1)).toBe(false);
expect($timeout.cancel(promise2)).toBe(true);
}));
it('should not throw a runtime exception when given an undefined promise', inject(function($timeout) {
expect($timeout.cancel()).toBe(false);
}));
it('should forget references to relevant deferred', inject(function($timeout, $browser) {
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced
var cancelSpy = spyOn($browser.defer, 'cancel').andCallThrough();
var promise = $timeout(function() {}, 0, false);
expect(cancelSpy).not.toHaveBeenCalled();
$timeout.cancel(promise);
expect(cancelSpy).toHaveBeenCalledOnce();
// Promise deferred object should already be removed from the list and not cancellable again
$timeout.cancel(promise);
expect(cancelSpy).toHaveBeenCalledOnce();
}));
});
});