forked from googlearchive/cloud-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservicesSpec.js
More file actions
270 lines (212 loc) · 8.25 KB
/
Copy pathservicesSpec.js
File metadata and controls
270 lines (212 loc) · 8.25 KB
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
'use strict';
/* jasmine specs for services */
describe('service', function() {
beforeEach(module('playgroundApp.services'));
describe('Alert', function() {
var Alert;
beforeEach(inject(function(_Alert_) {
Alert = _Alert_;
}));
it('should be empty at the start', function() {
expect(Alert.alerts().length).toBe(0);
});
describe('Alert.note', function() {
it('should be able to add an alert', function() {
Alert.note('Test alert');
expect(Alert.alerts().length).toBe(1);
expect(Alert.alerts()[0]).toEqual({icon: 'icon-hand-right',
msg: 'Test alert'});
});
});
describe('Alert methods', function() {
it('should be able to add and remove some alerts', function() {
var methods = ['info', 'success', 'error'];
var messages = ['info message', 'success message', 'error message'];
var icons = ['icon-info-sign', 'icon-ok', 'icon-exclamation-sign'];
for (var i = 0; i < methods.length; i++) {
Alert[methods[i]](messages[i]);
}
expect(Alert.alerts().length).toBe(3);
for (var i = 0; i < methods.length; i++) {
expect(Alert.alerts()[i]).toEqual(
{msg: messages[i], icon: icons[i], type: methods[i]});
}
Alert.remove_alert(1);
expect(Alert.alerts().length).toBe(2);
expect(Alert.alerts()[1]).toEqual({msg: messages[2],
icon: 'icon-exclamation-sign',
type: 'error'});
});
});
});
describe('pgHttpInterceptor', function() {
it('should return HTTP normal responses unmodified',
inject(function(pgHttpInterceptor) {
// TODO: use jasmine spy instead
var called = false;
var http_promise = {
then: function(success_fn, error_fn) {
called = true;
// normal HTTP response
return success_fn('original http response');
}
};
expect(called).toBe(false);
var response = pgHttpInterceptor(http_promise);
expect(response).toEqual('original http response');
expect(called).toBe(true);
}));
// TODO: renable this or comparable test
xit('should recognize and log X-Cloud-Playground-Error error repsonses',
inject(function(pgHttpInterceptor, $log) {
var error_response = {
config: {},
data: 'error response body',
headers: function(name) {
return name == 'X-Cloud-Playground-Error' ? 'True' : undefined;
},
status: 500,
};
var http_promise = {
then: function(success_fn, error_fn) {
error_fn(error_response);
}
};
var response = pgHttpInterceptor(http_promise);
expect(response).toBeUndefined();
expect($log.error.logs.pop()).toEqual(['Error:\nerror response body']);
$log.assertEmpty();
}));
// TODO: renable this or comparable test
xit('should log generic HTTP error repsonses',
inject(function(pgHttpInterceptor, $log) {
var error_response = {
config: {},
data: 'error response body',
headers: function(name) { return undefined; },
status: 500,
};
var http_promise = {
then: function(success_fn, error_fn) {
error_fn(error_response);
}
};
var response = pgHttpInterceptor(http_promise);
expect(response).toBeUndefined();
expect($log.error.logs.pop()).toEqual(['HTTP ERROR', error_response]);
$log.assertEmpty();
}));
// TODO: renable this or comparable test
xit('should log raised errors',
inject(function(pgHttpInterceptor, $log) {
var error_response = Error('raised error');
var http_promise = {
then: function(success_fn, error_fn) {
error_fn(error_response);
}
};
var response = pgHttpInterceptor(http_promise);
expect(response).toBeUndefined();
expect($log.error.logs.pop()).toEqual([error_response]);
$log.assertEmpty();
}));
});
describe('DoSerial', function() {
it('should be chainable', inject(function(DoSerial) {
var result = DoSerial.then(function() {});
expect(result).toBe(DoSerial);
result = DoSerial.tick(function() {});
expect(result).toBe(DoSerial);
}));
it('should not accept null argument', inject(function(DoSerial) {
expect(DoSerial.then).toThrow();
}));
it('should execute simple task synchronously', inject(function(DoSerial) {
var fn = jasmine.createSpy();
DoSerial.then(fn);
expect(fn).toHaveBeenCalled();
}));
it('should execute tasks in order',
inject(function(DoSerial, $log, $timeout) {
DoSerial.then(function() { $log.log(1); });
DoSerial.tick();
DoSerial
.then(function() { $log.log(2); })
.then(function() { $log.log(3); })
.tick()
.then(function() { $log.log(4); });
DoSerial.tick();
expect($log.log.logs).toEqual([[1]]);
$timeout.flush();
expect($log.log.logs).toEqual([[1], [2], [3], [4]]);
}));
it('should wait for promise to be satisfied before continuing',
inject(function(DoSerial, $log, $timeout) {
DoSerial
.then(function() { return $timeout(function() { $log.log(1); }); })
.then(function() { $log.log(2); });
expect($log.log.logs).toEqual([]);
$timeout.flush();
expect($log.log.logs).toEqual([[1], [2]]);
}));
xit('should accept both promises and functions', inject(function(DoSerial) {
// TODO
}));
it('should log and continue after synchronous exception', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function(DoSerial, $log, $exceptionHandler, $timeout) {
expect($log.assertEmpty());
DoSerial
.then(function() { $log.log(1); })
.then(function() { throw 'banana peel'; })
.then(function() { $log.log(2); });
expect($exceptionHandler.errors).toEqual(['banana peel']);
expect($log.log.logs).toEqual([[1], [2]]);
});
});
it('should log and continue after exception in promise', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function(DoSerial, $log, $exceptionHandler, $timeout) {
expect($log.assertEmpty());
DoSerial
.then(function() { $log.log(1); })
.then(function() { return $timeout(function() { $log.log(2); }); })
.then(function() {
return $timeout(function() { throw 'apple core'; }); })
.then(function() { $log.log(3); });
$timeout.flush();
expect($exceptionHandler.errors).toEqual(['apple core']);
expect($log.log.logs).toEqual([[1], [2], [3]]);
});
});
});
// TODO: DETERMINE if there's a better way to test window / document stuff
describe('DomElementById', function() {
it('should call $window.document.getElementById(:id)',
inject(function($window, DomElementById) {
var elem = $window.document.createElement('div');
elem.id = 'myid';
$window.document.getElementById = jasmine.createSpy('getElementById')
.andReturn(elem);
var result = DomElementById('myid');
expect($window.document.getElementById).toHaveBeenCalledWith('myid');
expect(result).toBe(elem);
}));
});
describe('WrappedElementById', function() {
it('should return angular.element(:elem)',
inject(function($window, WrappedElementById) {
var elem = $window.document.createElement('div');
var wrappedElem = angular.element(elem);
$window.document.getElementById = jasmine.createSpy('getElementById')
.andReturn(elem);
var result = WrappedElementById('myid');
expect($window.document.getElementById).toHaveBeenCalledWith('myid');
expect(result).toEqual(wrappedElem);
}));
});
});