UNPKG

3.92 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 return function () {
42 var args = [];
43 for (var _i = 0; _i < arguments.length; _i++) {
44 args[_i - 0] = arguments[_i];
45 }
46 var proxyZoneSpec = ProxyZoneSpec.assertPresent();
47 if (_inFakeAsyncCall) {
48 throw new Error('fakeAsync() calls can not be nested');
49 }
50 _inFakeAsyncCall = true;
51 try {
52 if (!_fakeAsyncTestZoneSpec) {
53 if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
54 throw new Error('fakeAsync() calls can not be nested');
55 }
56 _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
57 }
58 var res = void 0;
59 var lastProxyZoneSpec = proxyZoneSpec.getDelegate();
60 proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
61 try {
62 res = fn.apply(void 0, args);
63 flushMicrotasks();
64 }
65 finally {
66 proxyZoneSpec.setDelegate(lastProxyZoneSpec);
67 }
68 if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
69 throw new Error((_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length + " ") +
70 "periodic timer(s) still in the queue.");
71 }
72 if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
73 throw new Error(_fakeAsyncTestZoneSpec.pendingTimers.length + " timer(s) still in the queue.");
74 }
75 return res;
76 }
77 finally {
78 _inFakeAsyncCall = false;
79 resetFakeAsyncZone();
80 }
81 };
82}
83function _getFakeAsyncZoneSpec() {
84 if (_fakeAsyncTestZoneSpec == null) {
85 throw new Error('The code should be running in the fakeAsync zone to call this function');
86 }
87 return _fakeAsyncTestZoneSpec;
88}
89/**
90 * Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
91 *
92 * The microtasks queue is drained at the very start of this function and after any timer callback
93 * has been executed.
94 *
95 * ## Example
96 *
97 * {@example testing/ts/fake_async.ts region='basic'}
98 *
99 * @experimental
100 */
101export function tick(millis) {
102 if (millis === void 0) { millis = 0; }
103 _getFakeAsyncZoneSpec().tick(millis);
104}
105/**
106 * Discard all remaining periodic tasks.
107 *
108 * @experimental
109 */
110export function discardPeriodicTasks() {
111 var zoneSpec = _getFakeAsyncZoneSpec();
112 var pendingTimers = zoneSpec.pendingPeriodicTimers;
113 zoneSpec.pendingPeriodicTimers.length = 0;
114}
115/**
116 * Flush any pending microtasks.
117 *
118 * @experimental
119 */
120export function flushMicrotasks() {
121 _getFakeAsyncZoneSpec().flushMicrotasks();
122}
123//# sourceMappingURL=fake_async.js.map
\No newline at end of file