|
1 | 1 | // Promise testing support
|
2 |
| -angular.module('ngMock') |
3 |
| - .config(function ($provide) { |
4 |
| - $provide.decorator('$q', function ($delegate, $rootScope) { |
5 |
| - $delegate.flush = function() { |
6 |
| - $rootScope.$digest(); |
7 |
| - }; |
| 2 | +angular.module('ngMock').config(function ($provide) { |
| 3 | + $provide.decorator('$q', function ($delegate, $rootScope) { |
| 4 | + $delegate.flush = function() { |
| 5 | + $rootScope.$digest(); |
| 6 | + }; |
8 | 7 |
|
9 |
| - // Add callbacks to the promise that expose the resolved value/error |
10 |
| - function expose(promise) { |
11 |
| - // Don't add hooks to the same promise twice (shouldn't happen anyway) |
12 |
| - if (!promise.hasOwnProperty('$$resolved')) { |
13 |
| - promise.$$resolved = false; |
14 |
| - promise.then(function (value) { |
15 |
| - promise.$$resolved = { success: true, value: value }; |
16 |
| - }, function (error) { |
17 |
| - promise.$$resolved = { success: false, error: error }; |
18 |
| - }); |
| 8 | + // Add callbacks to the promise that expose the resolved value/error |
| 9 | + function expose(promise) { |
| 10 | + // Don't add hooks to the same promise twice (shouldn't happen anyway) |
| 11 | + if (!promise.hasOwnProperty('$$resolved')) { |
| 12 | + promise.$$resolved = false; |
| 13 | + promise.then(function (value) { |
| 14 | + promise.$$resolved = { success: true, value: value }; |
| 15 | + }, function (error) { |
| 16 | + promise.$$resolved = { success: false, error: error }; |
| 17 | + }); |
19 | 18 |
|
20 |
| - // We need to expose() any then()ed promises recursively |
21 |
| - var qThen = promise.then; |
22 |
| - promise.then = function () { |
23 |
| - return expose(qThen.apply(this, arguments)); |
24 |
| - }; |
25 |
| - } |
26 |
| - return promise; |
| 19 | + // We need to expose() any then()ed promises recursively |
| 20 | + var qThen = promise.then; |
| 21 | + promise.then = function () { |
| 22 | + return expose(qThen.apply(this, arguments)); |
| 23 | + }; |
27 | 24 | }
|
| 25 | + return promise; |
| 26 | + } |
28 | 27 |
|
29 |
| - // Wrap functions that return a promise |
30 |
| - angular.forEach([ 'when', 'all', 'reject'], function (name) { |
31 |
| - var qFunc = $delegate[name]; |
32 |
| - $delegate[name] = function () { |
33 |
| - return expose(qFunc.apply(this, arguments)); |
34 |
| - }; |
35 |
| - }); |
| 28 | + // Wrap functions that return a promise |
| 29 | + angular.forEach([ 'when', 'all', 'reject'], function (name) { |
| 30 | + var qFunc = $delegate[name]; |
| 31 | + $delegate[name] = function () { |
| 32 | + return expose(qFunc.apply(this, arguments)); |
| 33 | + }; |
| 34 | + }); |
36 | 35 |
|
37 |
| - // Wrap defer() |
38 |
| - var qDefer = $delegate.defer; |
39 |
| - $delegate.defer = function () { |
40 |
| - var deferred = qDefer(); |
41 |
| - expose(deferred.promise); |
42 |
| - return deferred; |
43 |
| - } |
| 36 | + // Wrap defer() |
| 37 | + var qDefer = $delegate.defer; |
| 38 | + $delegate.defer = function () { |
| 39 | + var deferred = qDefer(); |
| 40 | + expose(deferred.promise); |
| 41 | + return deferred; |
| 42 | + } |
| 43 | + |
| 44 | + return $delegate; |
| 45 | + }); |
| 46 | +}); |
44 | 47 |
|
| 48 | +try { |
| 49 | + // Animation testing support |
| 50 | + angular.module('mock.animate').config(function ($provide) { |
| 51 | + $provide.decorator('$animate', function ($delegate) { |
| 52 | + $delegate.flush = function() { |
| 53 | + while (this.queue.length > 0) { |
| 54 | + this.flushNext(this.queue[0].method); |
| 55 | + } |
| 56 | + }; |
45 | 57 | return $delegate;
|
46 | 58 | });
|
47 | 59 | });
|
48 |
| - |
| 60 | +} catch (e) {} |
| 61 | + |
49 | 62 | function testablePromise(promise) {
|
50 | 63 | if (!promise || !promise.then) throw new Error('Expected a promise, but got ' + jasmine.pp(promise) + '.');
|
51 | 64 | if (!isDefined(promise.$$resolved)) throw new Error('Promise has not been augmented by ngMock');
|
|
0 commit comments