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 | */
|
8 | var _global = (typeof window === 'undefined' ? global : window);
|
9 | /**
|
10 | * Wraps a test function in an asynchronous test zone. The test will automatically
|
11 | * complete when all asynchronous calls within this zone are done. Can be used
|
12 | * to wrap an {@link inject} call.
|
13 | *
|
14 | * Example:
|
15 | *
|
16 | * ```
|
17 | * it('...', async(inject([AClass], (object) => {
|
18 | * object.doSomething.then(() => {
|
19 | * expect(...);
|
20 | * })
|
21 | * });
|
22 | * ```
|
23 | *
|
24 | * @stable
|
25 | */
|
26 | export function async(fn) {
|
27 | // If we're running using the Jasmine test framework, adapt to call the 'done'
|
28 | // function when asynchronous activity is finished.
|
29 | if (_global.jasmine) {
|
30 | return function (done) {
|
31 | if (!done) {
|
32 | // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
|
33 | // fake it here and assume sync.
|
34 | done = function () { };
|
35 | done.fail = function (e) { throw e; };
|
36 | }
|
37 | runInTestZone(fn, done, function (err) {
|
38 | if (typeof err === 'string') {
|
39 | return done.fail(new Error(err));
|
40 | }
|
41 | else {
|
42 | done.fail(err);
|
43 | }
|
44 | });
|
45 | };
|
46 | }
|
47 | // Otherwise, return a promise which will resolve when asynchronous activity
|
48 | // is finished. This will be correctly consumed by the Mocha framework with
|
49 | // it('...', async(myFn)); or can be used in a custom framework.
|
50 | return function () { return new Promise(function (finishCallback, failCallback) {
|
51 | runInTestZone(fn, finishCallback, failCallback);
|
52 | }); };
|
53 | }
|
54 | function runInTestZone(fn, finishCallback, failCallback) {
|
55 | var currentZone = Zone.current;
|
56 | var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
|
57 | if (AsyncTestZoneSpec === undefined) {
|
58 | throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
59 | 'Please make sure that your environment includes zone.js/dist/async-test.js');
|
60 | }
|
61 | var ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
62 | if (ProxyZoneSpec === undefined) {
|
63 | throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
64 | 'Please make sure that your environment includes zone.js/dist/proxy.js');
|
65 | }
|
66 | var proxyZoneSpec = ProxyZoneSpec.get();
|
67 | ProxyZoneSpec.assertPresent();
|
68 | // We need to create the AsyncTestZoneSpec outside the ProxyZone.
|
69 | // If we do it in ProxyZone then we will get to infinite recursion.
|
70 | var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
|
71 | var previousDelegate = proxyZoneSpec.getDelegate();
|
72 | proxyZone.parent.run(function () {
|
73 | var testZoneSpec = new AsyncTestZoneSpec(function () {
|
74 | // Need to restore the original zone.
|
75 | currentZone.run(function () {
|
76 | if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
77 | // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
78 | proxyZoneSpec.setDelegate(previousDelegate);
|
79 | }
|
80 | finishCallback();
|
81 | });
|
82 | }, function (error) {
|
83 | // Need to restore the original zone.
|
84 | currentZone.run(function () {
|
85 | if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
86 | // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
87 | proxyZoneSpec.setDelegate(previousDelegate);
|
88 | }
|
89 | failCallback(error);
|
90 | });
|
91 | }, 'test');
|
92 | proxyZoneSpec.setDelegate(testZoneSpec);
|
93 | });
|
94 | return Zone.current.runGuarded(fn);
|
95 | }
|
96 | //# sourceMappingURL=async.js.map |
\ | No newline at end of file |