This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
setTimeout.spec.ts
60 lines (55 loc) · 2.02 KB
/
setTimeout.spec.ts
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
import {zoneSymbol} from '../../lib/browser/utils';
describe('setTimeout', function () {
it('should intercept setTimeout', function (done) {
var cancelId: any;
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
testZone.run(() => {
var timeoutFn = function () {
expect(Zone.current.name).toEqual(('TestZone'));
global[zoneSymbol('setTimeout')](function () {
expect(wtfMock.log).toEqual([
'# Zone:fork("<root>::WTF", "TestZone")',
'> Zone:invoke:unit-test("<root>::WTF::TestZone")',
'# Zone:schedule:macroTask:setTimeout("<root>::WTF::TestZone", ' + id + ')',
'< Zone:invoke:unit-test',
'> Zone:invokeTask:setTimeout("<root>::WTF::TestZone")',
'< Zone:invokeTask:setTimeout'
]);
done();
});
};
expect(Zone.current.name).toEqual(('TestZone'));
cancelId = setTimeout(timeoutFn, 3);
var id = JSON.stringify((<MacroTask>cancelId).data);
expect(wtfMock.log).toEqual([
'# Zone:fork("<root>::WTF", "TestZone")',
'> Zone:invoke:unit-test("<root>::WTF::TestZone")',
'# Zone:schedule:macroTask:setTimeout("<root>::WTF::TestZone", ' + id + ')'
]);
}, null, null, 'unit-test');
});
it('should allow canceling of fns registered with setTimeout', function (done) {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
clearTimeout(cancelId);
setTimeout(function () {
expect(spy).not.toHaveBeenCalled();
done();
});
});
it('should allow double cancelation of fns registered with setTimeout', function (done) {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
setTimeout(function () {
expect(spy).toHaveBeenCalled();
setTimeout(function () {
clearTimeout(cancelId);
done();
});
});
});
it('should pass invalid values through', function () {
clearTimeout(null);
clearTimeout(<any>{});
});
});