forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplateRequestSpec.js
292 lines (222 loc) · 8.74 KB
/
templateRequestSpec.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
'use strict';
describe('$templateRequest', function() {
describe('provider', function() {
describe('httpOptions', function() {
it('should default to undefined and fallback to default $http options', function() {
var defaultHeader;
module(function($templateRequestProvider) {
expect($templateRequestProvider.httpOptions()).toBeUndefined();
});
inject(function($templateRequest, $http, $templateCache) {
spyOn($http, 'get').and.callThrough();
$templateRequest('tpl.html');
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
cache: $templateCache,
transformResponse: []
});
});
});
it('should be configurable', function() {
function someTransform() {}
module(function($templateRequestProvider) {
// Configure the template request service to provide specific headers and transforms
$templateRequestProvider.httpOptions({
headers: { Accept: 'moo' },
transformResponse: [someTransform]
});
});
inject(function($templateRequest, $http, $templateCache) {
spyOn($http, 'get').and.callThrough();
$templateRequest('tpl.html');
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
cache: $templateCache,
transformResponse: [someTransform],
headers: { Accept: 'moo' }
});
});
});
it('should be allow you to override the cache', function() {
var httpOptions = {};
module(function($templateRequestProvider) {
$templateRequestProvider.httpOptions(httpOptions);
});
inject(function($templateRequest, $http, $cacheFactory) {
spyOn($http, 'get').and.callThrough();
var customCache = $cacheFactory('customCache');
httpOptions.cache = customCache;
$templateRequest('tpl.html');
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
cache: customCache,
transformResponse: []
});
});
});
});
});
it('should download the provided template file',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('<div>abc</div>');
var content;
$templateRequest('tpl.html').then(function(html) { content = html; });
$rootScope.$digest();
$httpBackend.flush();
expect(content).toBe('<div>abc</div>');
}));
it('should cache the request to prevent extra downloads',
inject(function($rootScope, $templateRequest, $templateCache, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('matias');
var content = [];
function tplRequestCb(html) {
content.push(html);
}
$templateRequest('tpl.html').then(tplRequestCb);
$httpBackend.flush();
$templateRequest('tpl.html').then(tplRequestCb);
$rootScope.$digest();
expect(content[0]).toBe('matias');
expect(content[1]).toBe('matias');
expect($templateCache.get('tpl.html')).toBe('matias');
}));
it('should return the cached value on the first request',
inject(function($rootScope, $templateRequest, $templateCache, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('matias');
spyOn($templateCache, 'put').and.returnValue('_matias');
var content = [];
function tplRequestCb(html) {
content.push(html);
}
$templateRequest('tpl.html').then(tplRequestCb);
$rootScope.$digest();
$httpBackend.flush();
expect(content[0]).toBe('_matias');
}));
it('should call `$exceptionHandler` on request error', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function($exceptionHandler, $httpBackend, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond(404, '', {}, 'Not Found');
var err;
$templateRequest('tpl.html').catch(function(reason) { err = reason; });
$httpBackend.flush();
expect(err).toEqualMinErr('$templateRequest', 'tpload',
'Failed to load template: tpl.html (HTTP status: 404 Not Found)');
expect($exceptionHandler.errors[0]).toEqualMinErr('$templateRequest', 'tpload',
'Failed to load template: tpl.html (HTTP status: 404 Not Found)');
});
});
it('should not call `$exceptionHandler` on request error when `ignoreRequestError` is true',
function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function($exceptionHandler, $httpBackend, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond(404);
var err;
$templateRequest('tpl.html', true).catch(function(reason) { err = reason; });
$httpBackend.flush();
expect(err.status).toBe(404);
expect($exceptionHandler.errors).toEqual([]);
});
}
);
it('should not call `$exceptionHandler` when the template is empty',
inject(function($exceptionHandler, $httpBackend, $rootScope, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond('');
var onError = jasmine.createSpy('onError');
$templateRequest('tpl.html').catch(onError);
$rootScope.$digest();
$httpBackend.flush();
expect(onError).not.toHaveBeenCalled();
expect($exceptionHandler.errors).toEqual([]);
})
);
it('should accept empty templates and refuse null or undefined templates in cache',
inject(function($rootScope, $templateRequest, $templateCache, $sce) {
// Will throw on any template not in cache.
spyOn($sce, 'getTrustedResourceUrl').and.returnValue(false);
expect(function() {
$templateRequest('tpl.html'); // should go through $sce
$rootScope.$digest();
}).toThrow();
$templateCache.put('tpl.html'); // is a no-op, so $sce check as well.
expect(function() {
$templateRequest('tpl.html');
$rootScope.$digest();
}).toThrow();
$templateCache.removeAll();
$templateCache.put('tpl.html', null); // makes no sense, but it's been added, so trust it.
expect(function() {
$templateRequest('tpl.html');
$rootScope.$digest();
}).not.toThrow();
$templateCache.removeAll();
$templateCache.put('tpl.html', ''); // should work (empty template)
expect(function() {
$templateRequest('tpl.html');
$rootScope.$digest();
}).not.toThrow();
$templateCache.removeAll();
}));
it('should keep track of how many requests are going on',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('a.html').respond('a');
$httpBackend.expectGET('b.html').respond('c');
$templateRequest('a.html');
$templateRequest('b.html');
expect($templateRequest.totalPendingRequests).toBe(2);
$rootScope.$digest();
$httpBackend.flush();
expect($templateRequest.totalPendingRequests).toBe(0);
$httpBackend.expectGET('c.html').respond(404);
$templateRequest('c.html');
expect($templateRequest.totalPendingRequests).toBe(1);
$rootScope.$digest();
try {
$httpBackend.flush();
} catch (e) { /* empty */ }
expect($templateRequest.totalPendingRequests).toBe(0);
}));
it('should not try to parse a response as JSON',
inject(function($templateRequest, $httpBackend) {
var spy = jasmine.createSpy('success');
$httpBackend.expectGET('a.html').respond('{{text}}', {
'Content-Type': 'application/json'
});
$templateRequest('a.html').then(spy);
$httpBackend.flush();
expect(spy).toHaveBeenCalledOnceWith('{{text}}');
}));
it('should use custom response transformers (array)', function() {
module(function($httpProvider) {
$httpProvider.defaults.transformResponse.push(function(data) {
return data + '!!';
});
});
inject(function($templateRequest, $httpBackend) {
var spy = jasmine.createSpy('success');
$httpBackend.expectGET('a.html').respond('{{text}}', {
'Content-Type': 'application/json'
});
$templateRequest('a.html').then(spy);
$httpBackend.flush();
expect(spy).toHaveBeenCalledOnceWith('{{text}}!!');
});
});
it('should use custom response transformers (function)', function() {
module(function($httpProvider) {
$httpProvider.defaults.transformResponse = function(data) {
return data + '!!';
};
});
inject(function($templateRequest, $httpBackend) {
var spy = jasmine.createSpy('success');
$httpBackend.expectGET('a.html').respond('{{text}}', {
'Content-Type': 'application/json'
});
$templateRequest('a.html').then(spy);
$httpBackend.flush();
expect(spy).toHaveBeenCalledOnceWith('{{text}}!!');
});
});
});