|
1 |
| -function resolvePromise(promise) { |
2 |
| - if (!promise.then) throw new Error('Expected a promise, but got ' + jasmine.pp(promise) + '.'); |
3 |
| - |
4 |
| - var result; |
5 |
| - promise.then(function (value) { |
6 |
| - result = { success: true, value: value }; |
7 |
| - }, function (error) { |
8 |
| - result = { success: false, error: error }; |
| 1 | +angular.module('ngMock') |
| 2 | + .config(function ($provide) { |
| 3 | + $provide.decorator('$q', function ($delegate, $rootScope) { |
| 4 | + $delegate.flush = function() { |
| 5 | + $rootScope.$digest(); |
| 6 | + }; |
| 7 | + |
| 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 | + }); |
| 18 | + |
| 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 | + }; |
| 24 | + } |
| 25 | + return promise; |
| 26 | + } |
| 27 | + |
| 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 | + }); |
| 35 | + |
| 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 | + }); |
9 | 46 | });
|
10 | 47 |
|
11 |
| - jasmine.getEnv().currentSpec.$injector.get('$rootScope').$digest(); |
12 |
| - if (!result) throw new Error('Promise is still pending'); |
| 48 | + |
| 49 | +function resolvedPromise(promise) { |
| 50 | + if (!promise.then) throw new Error('Expected a promise, but got ' + jasmine.pp(promise) + '.'); |
| 51 | + var result = promise.$$resolved; |
| 52 | + if (!isDefined(result)) throw new Error('Promise has not been augmented by ngMock'); |
| 53 | + if (!result) throw new Error('Promise is not resolved yet'); |
13 | 54 | return result;
|
14 | 55 | }
|
15 | 56 |
|
16 | 57 | function resolvedValue(promise) {
|
17 |
| - var result = resolvePromise(promise); |
| 58 | + var result = resolvedPromise(promise); |
18 | 59 | if (!result.success) throw result.error;
|
19 | 60 | return result.value;
|
20 | 61 | }
|
21 | 62 |
|
22 | 63 | function resolvedError(promise) {
|
23 |
| - var result = resolvePromise(promise); |
| 64 | + var result = resolvedPromise(promise); |
24 | 65 | if (result.success) throw new Error('Promise was expected to fail but returned ' + jasmin.pp(res.value) + '.');
|
25 | 66 | return result.error;
|
26 | 67 | }
|
0 commit comments