Skip to content

Commit e994389

Browse files
committed
chore(testUtils): implement $animate.flush()
- Reformat and clean up whitespace
1 parent 9ac410c commit e994389

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

test/testUtils.js

+51-38
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,64 @@
11
// 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+
};
87

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+
});
1918

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+
};
2724
}
25+
return promise;
26+
}
2827

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+
});
3635

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+
});
4447

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+
};
4557
return $delegate;
4658
});
4759
});
48-
60+
} catch (e) {}
61+
4962
function testablePromise(promise) {
5063
if (!promise || !promise.then) throw new Error('Expected a promise, but got ' + jasmine.pp(promise) + '.');
5164
if (!isDefined(promise.$$resolved)) throw new Error('Promise has not been augmented by ngMock');

0 commit comments

Comments
 (0)