forked from chieffancypants/angular-loading-bar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloading-bar-interceptor.coffee
554 lines (429 loc) · 17.5 KB
/
loading-bar-interceptor.coffee
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
isLoadingBarInjected = (doc) ->
injected = false
divs = angular.element(doc).find('div')
for i in divs
if angular.element(i).attr('id') is 'loading-bar'
injected = true
break
return injected
flush = null
describe 'loadingBarInterceptor Service', ->
$http = $httpBackend = $document = $timeout = result = loadingBar = $animate = null
response = {message:'OK'}
endpoint = '/service'
beforeEach ->
module 'ngAnimateMock', 'chieffancypants.loadingBar', (cfpLoadingBarProvider) ->
loadingBar = cfpLoadingBarProvider
return
result = null
inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$animate_) ->
$http = _$http_
$httpBackend = _$httpBackend_
$document = _$document_
$timeout = _$timeout_
$animate = _$animate_
# Angular 1.4 removed triggerCalbacks(), so try them both:
flush = () ->
$animate.flush && $animate.flush()
$animate.triggerCallbacks && $animate.triggerCallbacks()
beforeEach ->
this.addMatchers
toBeBetween: (high, low) ->
if low > high
temp = low
low = high
high = temp
return this.actual > low && this.actual < high
afterEach ->
$httpBackend.verifyNoOutstandingRequest()
$timeout.verifyNoPendingTasks()
it 'should not increment if the response is cached in a cacheFactory', inject (cfpLoadingBar, $cacheFactory) ->
cache = $cacheFactory('loading-bar')
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint, cache: cache).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
cfpLoadingBar.complete() # set as complete
$timeout.flush()
flush()
$http.get(endpoint, cache: cache).then (data) ->
result = data
# no need to flush $httpBackend since the response is cached
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout
it 'should not increment if the response is cached using $http.defaults.cache', inject (cfpLoadingBar, $cacheFactory) ->
$http.defaults.cache = $cacheFactory('loading-bar')
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
cfpLoadingBar.complete() # set as complete
$timeout.flush()
flush()
$http.get(endpoint).then (data) ->
result = data
# no need to flush $httpBackend since the response is cached
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout
it 'should not increment if the response is cached', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint, cache: true).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
cfpLoadingBar.complete() # set as complete
$timeout.flush()
flush()
$http.get(endpoint, cache: true).then (data) ->
result = data
# no need to flush $httpBackend since the response is cached
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout
it 'should use default cache when $http.defaults.cache is true', inject (cfpLoadingBar, $cacheFactory) ->
# $http.defaults.cache = $cacheFactory('loading-bar')
$http.defaults.cache = true
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
cfpLoadingBar.complete() # set as complete
$timeout.flush()
flush()
$http.get(endpoint).then (data) ->
result = data
# no need to flush $httpBackend since the response is cached
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout
it 'should not cache when the request is a POST', inject (cfpLoadingBar) ->
$httpBackend.expectPOST(endpoint).respond response
$http.post(endpoint, {message: 'post'}).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush()
flush()
$httpBackend.expectPOST(endpoint).respond response
$http.post(endpoint, {message: 'post'}).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush()
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush()
it 'should increment the loading bar when not all requests have been recieved', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint).then (data) ->
result = data
$http.get(endpoint).then (data) ->
result = data
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 0.5
$httpBackend.flush()
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush() # loading bar is animated, so flush timeout
it 'should count http errors as responses so the loading bar can complete', inject (cfpLoadingBar) ->
# $httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond 401
$httpBackend.expectGET(endpoint).respond 401
$http.get(endpoint)
$http.get(endpoint)
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 0.5
$httpBackend.flush()
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush()
it 'should insert the loadingbar into the DOM when a request is sent', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint)
$http.get(endpoint)
$httpBackend.flush(1)
$timeout.flush() # flush the latencyThreshold timeout
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
$httpBackend.flush()
$timeout.flush()
it 'should insert the loadingbar as the last children of the parent container', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint)
$http.get(endpoint)
$httpBackend.flush(1)
$timeout.flush() # flush the latencyThreshold timeout
parent = $document.find(cfpLoadingBar.parentSelector)[0]
children = parent.childNodes
expect(children[children.length - 1].id).toBe 'loading-bar-spinner'
expect(children[children.length - 2].id).toBe 'loading-bar'
$httpBackend.flush()
$timeout.flush()
it 'should remove the loading bar when all requests have been received', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint)
$http.get(endpoint)
$httpBackend.flush(1)
$timeout.flush() # flush the latencyThreshold timeout
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
$httpBackend.flush()
$timeout.flush()
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false
it 'should get and set status', inject (cfpLoadingBar) ->
cfpLoadingBar.start()
$timeout.flush()
cfpLoadingBar.set(0.4)
expect(cfpLoadingBar.status()).toBe 0.4
cfpLoadingBar.set(0.9)
expect(cfpLoadingBar.status()).toBe 0.9
cfpLoadingBar.complete()
$timeout.flush()
it 'should increment things randomly', inject (cfpLoadingBar) ->
cfpLoadingBar.start()
$timeout.flush()
# increments between 3 - 6%
cfpLoadingBar.set(0.1)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(3, 6)
cfpLoadingBar.set(0.2)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(3, 6)
# increments between 0 - 3%
cfpLoadingBar.set(0.25)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 3)
cfpLoadingBar.set(0.5)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 3)
# increments between 0 - 2%
cfpLoadingBar.set(0.65)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 2)
cfpLoadingBar.set(0.75)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBeBetween(0, 2)
# increments 0.5%
cfpLoadingBar.set(0.9)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBe 0.5
cfpLoadingBar.set(0.97)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBeGreaterThan width
expect(width2 - width).toBe 0.5
# stops incrementing:
cfpLoadingBar.set(0.99)
lbar = angular.element(document.getElementById('loading-bar'))
width = lbar.children().css('width').slice(0, -1)
$timeout.flush()
width2 = lbar.children().css('width').slice(0, -1)
expect(width2).toBe width
cfpLoadingBar.complete()
$timeout.flush()
it 'should not set the status if the loading bar has not yet been started', inject (cfpLoadingBar) ->
cfpLoadingBar.set(0.5)
expect(cfpLoadingBar.status()).toBe 0
cfpLoadingBar.set(0.3)
expect(cfpLoadingBar.status()).toBe 0
cfpLoadingBar.start()
cfpLoadingBar.set(0.3)
expect(cfpLoadingBar.status()).toBe 0.3
cfpLoadingBar.complete()
$timeout.flush()
it 'should broadcast started and completed events', inject (cfpLoadingBar, $rootScope) ->
startedEventCalled = false
completedEventCalled = false
$rootScope.$on 'cfpLoadingBar:started', (event) ->
startedEventCalled = true
$rootScope.$on 'cfpLoadingBar:completed', (event) ->
completedEventCalled = true
expect(startedEventCalled).toBe false
expect(completedEventCalled).toBe false
cfpLoadingBar.start()
expect(startedEventCalled).toBe true
cfpLoadingBar.complete()
$timeout.flush()
expect(completedEventCalled).toBe true
it 'should debounce the calls to start()', inject (cfpLoadingBar, $rootScope) ->
startedEventCalled = 0
$rootScope.$on 'cfpLoadingBar:started', (event) ->
startedEventCalled += 1
cfpLoadingBar.start()
expect(startedEventCalled).toBe 1
cfpLoadingBar.start()
expect(startedEventCalled).toBe 1 # Should still be one, as complete was never called:
cfpLoadingBar.complete()
$timeout.flush()
flush()
cfpLoadingBar.start()
expect(startedEventCalled).toBe 2
cfpLoadingBar.complete()
$timeout.flush()
it 'should ignore requests when ignoreLoadingBar is true', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint, {ignoreLoadingBar: true})
$httpBackend.flush()
injected = isLoadingBarInjected $document.find(cfpLoadingBar.parentSelector)
expect(injected).toBe false
$timeout.flush()
it 'should ignore responses when ignoreLoadingBar is true (#70)', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET('/service2').respond response
$http.get(endpoint, {ignoreLoadingBar: true})
$http.get('/service2')
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.flush(1) # flush only the ignored request
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$httpBackend.flush()
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush() # loading bar is animated, so flush timeout
it 'should ignore errors when ignoreLoadingBar is true (#70)', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond 400
$httpBackend.expectGET('/service2').respond 400
$http.get(endpoint, {ignoreLoadingBar: true})
$http.get('/service2')
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.flush(1) # flush only the ignored request
expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$httpBackend.flush()
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush() # loading bar is animated, so flush timeout
describe 'LoadingBar only', ->
cfpLoadingBar = $document = $timeout = $animate = null
beforeEach ->
module 'cfp.loadingBar', 'ngAnimateMock'
inject (_$http_, _$httpBackend_, _$document_, _$timeout_, _$animate_, _cfpLoadingBar_) ->
$timeout = _$timeout_
$document = _$document_
$animate = _$animate_
cfpLoadingBar = _cfpLoadingBar_
it 'should be capable of being used alone', ->
# just a simple quick test to make sure:
cfpLoadingBar.start()
$timeout.flush()
# test setting progress
cfpLoadingBar.set(0.4)
expect(cfpLoadingBar.status()).toBe 0.4
# make sure it was injected into the DOM:
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
cfpLoadingBar.set(0.9)
expect(cfpLoadingBar.status()).toBe 0.9
# test the complete call, which should remove it from the DOM
cfpLoadingBar.complete()
$timeout.flush()
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false
it 'should start after multiple calls to complete()', ->
cfpLoadingBar.start()
$timeout.flush()
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
cfpLoadingBar.complete()
cfpLoadingBar.complete()
cfpLoadingBar.start()
$timeout.flush()
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
cfpLoadingBar.complete()
$timeout.flush()
flush()
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false
describe 'Interceptor tests', ->
provider = $http = $httpBackend = $log = null
endpoint = '/service'
response = {message:'OK'}
describe 'Success response', ->
beforeEach ->
module 'chieffancypants.loadingBar', ($httpProvider) ->
provider = $httpProvider
provider.interceptors.push ->
response: (resp) ->
return null
return
inject (_$http_, _$httpBackend_, _$log_) ->
$http = _$http_
$httpBackend = _$httpBackend_
$log = _$log_
it 'should detect poorly implemented interceptors and warn accordingly', ->
expect($log.error.logs.length).toBe 0
$httpBackend.expectGET(endpoint).respond 204
$http.get(endpoint)
$httpBackend.flush()
expect($log.error.logs.length).toBe 1
expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50']
describe 'Error response', ->
beforeEach ->
module 'chieffancypants.loadingBar', ($httpProvider) ->
provider = $httpProvider
provider.interceptors.push ($q) ->
responseError: (resp) ->
delete resp.config
$q.reject(resp);
return
inject (_$http_, _$httpBackend_, _$log_) ->
$http = _$http_
$httpBackend = _$httpBackend_
$log = _$log_
it 'should detect poorly implemented interceptors and warn accordingly', ->
expect($log.error.logs.length).toBe 0
$httpBackend.expectGET(endpoint).respond 500
$http.get(endpoint)
$httpBackend.flush()
expect($log.error.logs.length).toBe 1
expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50']