forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake_async_spec.js
More file actions
290 lines (227 loc) · 7.91 KB
/
fake_async_spec.js
File metadata and controls
290 lines (227 loc) · 7.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
fakeAsync,
flushMicrotasks,
iit,
inject,
IS_DARTIUM,
it,
Log,
tick,
xit
} from 'angular2/test_lib';
import {TimerWrapper, PromiseWrapper} from 'angular2/src/facade/async';
import {BaseException, global} from 'angular2/src/facade/lang';
import {Parser} from 'angular2/change_detection';
export function main() {
describe('fake async', () => {
it('should run synchronous code', () => {
var ran = false;
fakeAsync(() => {
ran = true;
})();
expect(ran).toEqual(true);
});
it('should pass arguments to the wrapped function', () => {
fakeAsync((foo, bar) => {
expect(foo).toEqual('foo');
expect(bar).toEqual('bar');
})('foo', 'bar');
});
it('should work with inject()', inject([Parser], fakeAsync((parser) => {
expect(parser).toBeAnInstanceOf(Parser);
})));
if (!IS_DARTIUM) {
it('should throw on nested calls', () => {
// TODO(vicb): re-enable once the jasmine patch from zone.js is applied
if (!IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
fakeAsync(() => null)();
})();
}).toThrowError('fakeAsync() calls can not be nested');
});
}
describe('Promise', () => {
it('should run asynchronous code', fakeAsync(() => {
var thenRan = false;
PromiseWrapper.resolve(null).then((_) => {
thenRan = true;
});
expect(thenRan).toEqual(false);
flushMicrotasks();
expect(thenRan).toEqual(true);
}));
it('should run chained thens', fakeAsync(() => {
var log = new Log();
PromiseWrapper
.resolve(null)
.then((_) => log.add(1))
.then((_) => log.add(2));
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
it('should run Promise created in Promise', fakeAsync(() => {
var log = new Log();
PromiseWrapper
.resolve(null)
.then((_) => {
log.add(1);
PromiseWrapper.resolve(null).then((_) => log.add(2));
});
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
// TODO(vicb): check why this doesn't work in JS - linked to open issues on GH ?
xit('should complain if the test throws an exception during async calls', () => {
expect(() => {
fakeAsync(() => {
PromiseWrapper.resolve(null).then((_) => {
throw new BaseException('async');
});
flushMicrotasks();
})();
}).toThrowError('async');
});
it('should complain if a test throws an exception', () => {
expect(() => {
fakeAsync(() => {
throw new BaseException('sync');
})();
}).toThrowError('sync');
});
});
describe('timers', () => {
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => { ran = true }, 0);
expect(ran).toEqual(false);
tick();
expect(ran).toEqual(true);
}));
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
var ran = false;
TimerWrapper.setTimeout(() => { ran = true; }, 10);
tick(6);
expect(ran).toEqual(false);
tick(6);
expect(ran).toEqual(true);
}));
it('should run queued timer only once', fakeAsync(() => {
var cycles = 0;
TimerWrapper.setTimeout(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should not run cancelled timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
TimerWrapper.clearTimeout(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should throw an error on dangling timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
TimerWrapper.setTimeout(() => { }, 10);
})();
}).toThrowError('1 timer(s) still in the queue.');
});
it('should throw an error on dangling periodic timers', () => {
// TODO(vicb): https://github.com/google/quiver-dart/issues/248
if (IS_DARTIUM) return;
expect(() => {
fakeAsync(() => {
TimerWrapper.setInterval(() => { }, 10);
})();
}).toThrowError('1 periodic timer(s) still in the queue.');
});
it('should run periodic timers', fakeAsync(() => {
var cycles = 0;
var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(2);
tick(10);
expect(cycles).toEqual(3);
TimerWrapper.clearInterval(id);
}));
it('should not run cancelled periodic timer', fakeAsync(() => {
var ran = false;
var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
TimerWrapper.clearInterval(id);
tick(10);
expect(ran).toEqual(false);
}));
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
if (global != null && global.jasmine) {
// TODO(vicb): remove this when we switch to jasmine 2.3.3+
// see https://github.com/jasmine/jasmine/commit/51462f369b376615bc9d761dcaa5d822ea1ff8ee
return;
}
var cycles = 0;
var id;
id = TimerWrapper.setInterval(() => {
cycles++;
TimerWrapper.clearInterval(id);
}, 10);
tick(10);
expect(cycles).toEqual(1);
tick(10);
expect(cycles).toEqual(1);
}));
it('should process microtasks before timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => log.add('timer'), 9);
var id = TimerWrapper.setInterval(() => log.add('periodic timer'), 10);
expect(log.result()).toEqual('');
tick(10);
expect(log.result()).toEqual('microtask; timer; periodic timer');
TimerWrapper.clearInterval(id);
}));
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
var log = new Log();
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
TimerWrapper.setTimeout(() => {
log.add('timer');
PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
}, 9);
var id = TimerWrapper.setInterval(() => {
log.add('periodic timer');
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
}, 10);
tick(10);
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
tick(10);
expect(log.result()).toEqual('microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
TimerWrapper.clearInterval(id);
}));
});
describe('outside of the fakeAsync zone', () => {
it('calling flushMicrotasks should throw', () => {
expect(() => {
flushMicrotasks();
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
});
it('calling tick should throw', () => {
expect(() => {
tick();
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
});
});
});
}