Skip to content

Commit

Permalink
Build URL only with angular >= 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Savater Sebastien committed May 11, 2016
1 parent 0f65f60 commit fab2d44
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
* @return {Boolean} retrns true if cached, otherwise false
*/
function isCached(config) {
var cache,
defaultCache = $cacheFactory.get('$http'),
defaults = $httpProvider.defaults,
url = buildUrl(config.url, config.paramSerializer(config.params));
var cache;
var defaultCache = $cacheFactory.get('$http');
var defaults = $httpProvider.defaults;
var url = config.url;

if (config.paramSerializer !== undefined) {
url = buildUrl(config.url, config.paramSerializer(config.params));
}

// Choose the proper cache source. Borrowed from angular: $http service
if ((config.cache || defaults.cache) && config.cache !== false &&
Expand All @@ -96,7 +100,6 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
return url;
}


return {
'request': function(config) {
// Check to make sure this request hasn't already been cached and that
Expand Down
41 changes: 41 additions & 0 deletions test/loading-bar-interceptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ describe 'loadingBarInterceptor Service', ->
expect(cfpLoadingBar.status()).toBe 1
$timeout.flush()

it 'should not increment if the response is cached with different params', inject (cfpLoadingBar) ->
return unless angular.version.major >= 1 && angular.version.minor >= 4

$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()

$httpBackend.verifyNoOutstandingRequest()


$httpBackend.expectGET(endpoint + "?q=angular").respond response
$http.get(endpoint, params: { q: 'angular' }, cache: true).then (data) ->
result = data

$httpBackend.verifyNoOutstandingExpectation()
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, params: { q: 'angular' }, 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 increment the loading bar when not all requests have been recieved', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
Expand Down

0 comments on commit fab2d44

Please sign in to comment.