-
Notifications
You must be signed in to change notification settings - Fork 21
/
promises.spec.js
329 lines (260 loc) · 10.1 KB
/
promises.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
describe('promises', function(){
var $httpBackend, $http, $q, $timeout, $rootScope;
beforeEach(inject(function(_$httpBackend_, _$http_, _$q_, _$timeout_, _$rootScope_){
$httpBackend = _$httpBackend_;
$http = _$http_;
$q = _$q_;
$timeout = _$timeout_;
$rootScope = _$rootScope_;
}));
describe('simplest example', function(){
it('should call success callback on successful result', function(){
$httpBackend.expectGET('http://api/user').respond(200, {data: ['obj1'], total: 10});
var responseData;
$http.get('http://api/user').then(function(response){
responseData = response.data;
});
$httpBackend.flush();
expect(responseData).toEqual({data: ['obj1'], total: 10});
});
it('should call error callback on error result', function(){
$httpBackend.expectGET('http://api/user').respond(400, {error_code: 10});
var responseData;
$http.get('http://api/user').then(
null,
function(response){
responseData = response.data;
}
);
$httpBackend.flush();
expect(responseData).toEqual({error_code: 10});
});
});
describe('returning values from callbacks', function(){
it('should process and return only needed data', function(){
var User = function(data){
return angular.extend(this, data);
};
$httpBackend.expectGET('http://api/user').respond(200, {
result: {
data: [{name: 'Artem'}],
page: 1,
total: 10
}
});
var data = {};
$http.get('http://api/user').then(function(response){
var usersInfo = {};
usersInfo.list = _.collect(response.data.result.data, function(u){ return new User(u); });
usersInfo.total = response.data.result.total;
return usersInfo;
}).then(function(usersInfo){
data.users = usersInfo;
});
$httpBackend.flush();
expect(data.users.list.length).toBe(1);
expect(data.users.list[0] instanceof User).toBeTruthy();
expect(data.users.list[0].name).toBe('Artem');
expect(data.users.total).toBe(10);
});
it('should resolve empty data from error callback for expected error', function(){
$httpBackend.expectGET('http://api/user').respond(400, {error_code: 10, error_message: 'Not initialized'});
var data = {};
$http.get('http://api/user').then(
null,
function(response){
if (response.data && response.data.error_code == 10){
return {
list: [],
total: 0
};
}
}
).then(function(usersInfo){
data.users = usersInfo;
});
$httpBackend.flush();
expect(data.users.list.length).toBe(0);
expect(data.users.total).toBe(0);
});
it('should resolve undefined from error callback for unexpected error', function(){
$httpBackend.expectGET('http://api/user').respond(400, {error_code: 11});
var data = {};
$http.get('http://api/user').then(
null,
function(response){
if (response.data && response.data.error_code == 10){
return {
list: [],
total: 0
};
}
}
).then(function(usersInfo){
data.users = usersInfo;
});
$httpBackend.flush();
expect(data.users).toBeUndefined();
});
it('should reject error_code from error callback for unexpected error', function(){
$httpBackend.expectGET('http://api/user').respond(400, {error_code: 11});
var error;
$http.get('http://api/user').then(
null,
function(response){
if (response.data && response.data.error_code == 10){
return {
list: [],
total: 0
};
}
return $q.reject(response.data ? response.data.error_code : null);
}
).then(
null,
function(errorCode){
error = errorCode;
}
);
$httpBackend.flush();
expect(error).toBe(11);
});
it('should reject null from error callback for unexpected error without data', function(){
$httpBackend.expectGET('http://api/user').respond(400);
var error;
$http.get('http://api/user').then(
null,
function(response){
if (response.data && response.data.error_code == 10){
return {
list: [],
total: 0
};
}
return $q.reject(response.data ? response.data.error_code : null);
}
).then(
null,
function(errorCode){
error = errorCode;
}
);
$httpBackend.flush();
expect(error).toBeNull();
});
});
describe('chaining promises', function(){
it('should chain calls', function(){
$httpBackend.expectGET('http://api/user/10').respond(200, {id: 10, name: 'Artem', group_id: 1});
$httpBackend.expectGET('http://api/group/1').respond(200, {id: 1, name: 'Some group'});
var user;
$http.get('http://api/user/10').then(function(response){
user = response.data;
return $http.get('http://api/group/' + user.group_id);
}).then(function(response){
user.group = response.data;
});
$httpBackend.flush();
expect(user).toEqual({
id: 10,
name: 'Artem',
group_id: 1,
group: {
id: 1,
name: 'Some group'
}
});
});
});
describe('combining promises', function(){
it('should wait for all promises in array', function(){
$httpBackend.expectGET('http://api/obj1').respond(200, {type: 'obj1'});
var obj1, obj2;
var request1 = $http.get('http://api/obj1');
var request2 = $timeout(function(){ return {type: 'obj2'}; });
$q.all([request1, request2]).then(function(values){
obj1 = values[0].data;
obj2 = values[1];
});
expect(obj1).toBeUndefined();
expect(obj2).toBeUndefined();
$httpBackend.flush();
expect(obj1).toBeUndefined();
expect(obj2).toBeUndefined();
$timeout.flush();
expect(obj1).toEqual({type: 'obj1'});
expect(obj2).toEqual({type: 'obj2'});
});
it('should wait for all promises in array', function(){
$httpBackend.expectGET('http://api/obj1').respond(200, {type: 'obj1'});
var obj1, obj2;
$q.all({
obj1: $http.get('http://api/obj1'),
obj2: $timeout(function(){ return {type: 'obj2'}; })
}).then(function(values){
obj1 = values.obj1.data;
obj2 = values.obj2;
});
expect(obj1).toBeUndefined();
expect(obj2).toBeUndefined();
$httpBackend.flush();
expect(obj1).toBeUndefined();
expect(obj2).toBeUndefined();
$timeout.flush();
expect(obj1).toEqual({type: 'obj1'});
expect(obj2).toEqual({type: 'obj2'});
});
});
describe('$q.when and unit-tests', function(){
it('should create promise for object', function(){
var res;
$q.when({id: 1}).then(function(value){
res = value;
});
$rootScope.$digest();
expect(res).toEqual({id: 1});
});
it('should be used for mocks', function(){
var UserApi = {
get: function(id){
return $http.get('http://api/user/' + id);
}
};
spyOn(UserApi, 'get').andReturn($q.when({id: 1, name: 'Artem'}));
var res;
UserApi.get(1).then(function(user){
res = user;
});
$rootScope.$digest();
expect(res).toEqual({id: 1, name: 'Artem'});
});
});
describe('interpolation and expressions eval', function(){
it('should use resolved value during expression eval', function(){
$rootScope.resPromise = $timeout(function(){ return 10; });
var res = $rootScope.$eval('resPromise + 2');
expect(res).toBe(2);
$timeout.flush();
res = $rootScope.$eval('resPromise + 2');
expect(res).toBe(12);
});
it('should use resolved value in $watch', function(){
var res;
$rootScope.resPromise = $timeout(function(){ return 10; });
$rootScope.$watch('resPromise', function(newVal){
res = newVal;
});
expect(res).toBeUndefined();
$timeout.flush();
expect(res).toBe(10);
});
it('should be promise-object', function(){
$rootScope.resPromise = function(){
return $timeout(function(){ return 10; });
};
var res = $rootScope.$eval('resPromise()');
expect(typeof res.then).toBe('function');
});
});
});