/
q.extensions.spec.js
146 lines (127 loc) · 4.51 KB
/
q.extensions.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
describe('horizon.framework.util.q.extensions', function () {
describe('allSettled', function() {
var service, $q, $scope;
var failedPromise = function() {
var deferred2 = $q.defer();
deferred2.reject('failed');
return deferred2.promise;
};
var passedPromise = function() {
var deferred1 = $q.defer();
deferred1.resolve('passed');
return deferred1.promise;
};
beforeEach(module('horizon.framework.util.q'));
beforeEach(inject(function($injector, _$rootScope_) {
service = $injector.get('horizon.framework.util.q.extensions');
$q = $injector.get('$q');
$scope = _$rootScope_.$new();
}));
it('should define allSettled', function () {
expect(service.allSettled).toBeDefined();
});
it('should resolve all given promises', function() {
service.allSettled([{
promise: failedPromise(),
context: '1'
}, {
promise: passedPromise(),
context: '2'
}]).then(onAllSettled);
$scope.$apply();
function onAllSettled(resolvedPromises) {
expect(resolvedPromises.fail.length).toEqual(1);
expect(resolvedPromises.fail[0]).toEqual({data: 'failed', context: '1'});
expect(resolvedPromises.pass.length).toEqual(1);
expect(resolvedPromises.pass[0]).toEqual({data: 'passed', context: '2'});
}
});
it('should maintain order of promises regardless of resolve/reject order', function() {
var defs = [$q.defer(), $q.defer(), $q.defer(), $q.defer(), $q.defer(), $q.defer()];
service.allSettled([{
promise: defs[0].promise,
context: '1'
},{
promise: defs[1].promise,
context: '2'
},{
promise: defs[2].promise,
context: '3'
},{
promise: defs[3].promise,
context: '4'
},{
promise: defs[4].promise,
context: '5'
},{
promise: defs[5].promise,
context: '6'
}]).then(onAllSettled);
defs[1].reject();
defs[2].resolve();
defs[0].resolve();
defs[5].reject();
defs[3].reject();
defs[4].resolve();
$scope.$apply();
function onAllSettled(resolvedPromises) {
var pass = resolvedPromises.pass;
var fail = resolvedPromises.fail;
expect(pass.length).toBe(3);
expect(fail.length).toBe(3);
expect(pass[0].context).toBe('1');
expect(pass[1].context).toBe('3');
expect(pass[2].context).toBe('5');
expect(fail[0].context).toBe('2');
expect(fail[1].context).toBe('4');
expect(fail[2].context).toBe('6');
}
});
});
describe('booleanAsPromise', function() {
var service, $scope;
beforeEach(module('horizon.framework.util.q'));
beforeEach(inject(function($injector, _$rootScope_) {
service = $injector.get('horizon.framework.util.q.extensions');
$scope = _$rootScope_.$new();
}));
it('should define booleanAsPromise', function () {
expect(service.booleanAsPromise).toBeDefined();
});
it('should reject the promise if condition does not evaluates to true', function() {
var testValues = [ false, null, {}, 'A', 7 ];
var rejectCount = 0;
testValues.map(function doTest(testValue) {
service.booleanAsPromise(testValue).then(angular.noop, function failTest() {
rejectCount++;
});
$scope.$apply();
});
expect(rejectCount).toEqual(testValues.length);
});
it('should resolve the promise only if condition to true', function() {
var passCount = 0;
service.booleanAsPromise(true).then(function passTest() {
passCount++;
});
$scope.$apply();
expect(passCount).toEqual(1);
});
});
});
})();