UNPKG

3.99 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
9var ProxyZoneSpec = Zone['ProxyZoneSpec'];
10var _fakeAsyncTestZoneSpec = null;
11/**
12 * Clears out the shared fake async zone for a test.
13 * To be called in a global `beforeEach`.
14 *
15 * @experimental
16 */
17export function resetFakeAsyncZone() {
18 _fakeAsyncTestZoneSpec = null;
19 ProxyZoneSpec.assertPresent().resetDelegate();
20}
21var _inFakeAsyncCall = false;
22/**
23 * Wraps a function to be executed in the fakeAsync zone:
24 * - microtasks are manually executed by calling `flushMicrotasks()`,
25 * - timers are synchronous, `tick()` simulates the asynchronous passage of time.
26 *
27 * If there are any pending timers at the end of the function, an exception will be thrown.
28 *
29 * Can be used to wrap inject() calls.
30 *
31 * ## Example
32 *
33 * {@example testing/ts/fake_async.ts region='basic'}
34 *
35 * @param fn
36 * @returns {Function} The function wrapped to be executed in the fakeAsync zone
37 *
38 * @experimental
39 */
40export function fakeAsync(fn) {
41 // Not using an arrow function to preserve context passed from call site
42 return function () {
43 var args = [];
44 for (var _i = 0; _i < arguments.length; _i++) {
45 args[_i - 0] = arguments[_i];
46 }
47 var proxyZoneSpec = ProxyZoneSpec.assertPresent();
48 if (_inFakeAsyncCall) {
49 throw new Error('fakeAsync() calls can not be nested');
50 }
51 _inFakeAsyncCall = true;
52 try {
53 if (!_fakeAsyncTestZoneSpec) {
54 if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
55 throw new Error('fakeAsync() calls can not be nested');
56 }
57 _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
58 }
59 var res = void 0;
60 var lastProxyZoneSpec = proxyZoneSpec.getDelegate();
61 proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
62 try {
63 res = fn.apply(this, args);
64 flushMicrotasks();
65 }
66 finally {
67 proxyZoneSpec.setDelegate(lastProxyZoneSpec);
68 }
69 if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
70 throw new Error((_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length + " ") +
71 "periodic timer(s) still in the queue.");
72 }
73 if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
74 throw new Error(_fakeAsyncTestZoneSpec.pendingTimers.length + " timer(s) still in the queue.");
75 }
76 return res;
77 }
78 finally {
79 _inFakeAsyncCall = false;
80 resetFakeAsyncZone();
81 }
82 };
83}
84function _getFakeAsyncZoneSpec() {
85 if (_fakeAsyncTestZoneSpec == null) {
86 throw new Error('The code should be running in the fakeAsync zone to call this function');
87 }
88 return _fakeAsyncTestZoneSpec;
89}
90/**
91 * Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
92 *
93 * The microtasks queue is drained at the very start of this function and after any timer callback
94 * has been executed.
95 *
96 * ## Example
97 *
98 * {@example testing/ts/fake_async.ts region='basic'}
99 *
100 * @experimental
101 */
102export function tick(millis) {
103 if (millis === void 0) { millis = 0; }
104 _getFakeAsyncZoneSpec().tick(millis);
105}
106/**
107 * Discard all remaining periodic tasks.
108 *
109 * @experimental
110 */
111export function discardPeriodicTasks() {
112 var zoneSpec = _getFakeAsyncZoneSpec();
113 var pendingTimers = zoneSpec.pendingPeriodicTimers;
114 zoneSpec.pendingPeriodicTimers.length = 0;
115}
116/**
117 * Flush any pending microtasks.
118 *
119 * @experimental
120 */
121export function flushMicrotasks() {
122 _getFakeAsyncZoneSpec().flushMicrotasks();
123}
124//# sourceMappingURL=fake_async.js.map
\No newline at end of file