Skip to content

Commit

Permalink
improvements on #133
Browse files Browse the repository at this point in the history
  • Loading branch information
chieffancypants committed Feb 24, 2015
1 parent 5a77fcd commit 449eaa2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
},

'response': function(response) {
if (!response || !response.config) {
$log.error('Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50');
return response;
}

if (!response.config.ignoreLoadingBar && !isCached(response.config)) {
reqsCompleted++;
$rootScope.$broadcast('cfpLoadingBar:loaded', {url: response.config.url, result: response});
Expand All @@ -120,9 +125,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
},

'responseError': function(rejection) {
if (!rejection.config) {
$log.error('Other interceptors are not returning config object \n https://github.com/chieffancypants/angular-loading-bar/pull/50');
if (!rejection || !rejection.config) {
$log.error('Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50');
return $q.reject(rejection);
}

if (!rejection.config.ignoreLoadingBar && !isCached(rejection.config)) {
reqsCompleted++;
$rootScope.$broadcast('cfpLoadingBar:loaded', {url: rejection.config.url, result: rejection});
Expand Down
65 changes: 59 additions & 6 deletions test/loading-bar-interceptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe 'loadingBarInterceptor Service', ->
$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
Expand All @@ -88,7 +87,6 @@ describe 'loadingBarInterceptor Service', ->
$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) ->
Expand Down Expand Up @@ -160,6 +158,7 @@ describe 'loadingBarInterceptor Service', ->
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
Expand All @@ -178,7 +177,6 @@ describe 'loadingBarInterceptor Service', ->
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
Expand All @@ -196,8 +194,6 @@ describe 'loadingBarInterceptor Service', ->

$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
Expand Down Expand Up @@ -343,7 +339,6 @@ describe 'loadingBarInterceptor Service', ->
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
Expand Down Expand Up @@ -493,3 +488,61 @@ describe 'LoadingBar only', ->

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']

0 comments on commit 449eaa2

Please sign in to comment.