diff options
| author | Karl Seamon | 2011-07-22 15:56:45 -0400 | 
|---|---|---|
| committer | Igor Minar | 2011-07-27 15:21:31 -0700 | 
| commit | b5594a773a6f07dcba914aa385f92d3305285b24 (patch) | |
| tree | 40823e64e9f74b356a8065edae9bbdf351082164 /test | |
| parent | f39420e7d7aca2a97eaa01853991facf65dcbd6e (diff) | |
| download | angular.js-b5594a773a6f07dcba914aa385f92d3305285b24.tar.bz2 | |
feat($xhr): add custom error callback to $xhr, $xhr.cache, $xhr.bulk, $resource
Closes #408
Diffstat (limited to 'test')
| -rw-r--r-- | test/ResourceSpec.js | 42 | ||||
| -rw-r--r-- | test/service/xhr.bulkSpec.js | 29 | ||||
| -rw-r--r-- | test/service/xhr.cacheSpec.js | 38 | ||||
| -rw-r--r-- | test/service/xhr.errorSpec.js | 6 | ||||
| -rw-r--r-- | test/service/xhrSpec.js | 42 | 
5 files changed, 120 insertions, 37 deletions
| diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 0b8d2187..81519f0f 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -1,12 +1,12 @@  'use strict';  describe("resource", function() { -  var xhr, resource, CreditCard, callback; +  var xhr, resource, CreditCard, callback, $xhrErr; -  beforeEach(function(){ -    var browser = new MockBrowser(); -    xhr = browser.xhr; -    resource = new ResourceFactory(xhr); +  beforeEach(function() { +    var scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')}); +    xhr = scope.$service('$browser').xhr; +    resource = new ResourceFactory(scope.$service('$xhr'));      CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {        charge:{          method:'POST', @@ -242,19 +242,25 @@ describe("resource", function() {      dealoc(scope);    }); -  describe('failure mode', function(){ -    it('should report error when non 200', function(){ -      xhr.expectGET('/CreditCard/123').respond(500, "Server Error"); -      var cc = CreditCard.get({id:123}); -      try { -        xhr.flush(); -        fail('expected exception, non thrown'); -      } catch (e) { -        expect(e.status).toEqual(500); -        expect(e.response).toEqual('Server Error'); -        expect(e.message).toEqual('500: Server Error'); -      } +  describe('failure mode', function() { +    var ERROR_CODE = 500, +        ERROR_RESPONSE = 'Server Error'; + +    beforeEach(function() { +      xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);      }); -  }); +    it('should report error when non 2xx if error callback is not provided', function() { +      CreditCard.get({id:123}); +      xhr.flush(); +      expect($xhrErr).toHaveBeenCalled(); +    }); + +    it('should call the error callback if provided on non 2xx response', function() { +      CreditCard.get({id:123}, noop, callback); +      xhr.flush(); +      expect(callback).toHaveBeenCalledWith(500, ERROR_RESPONSE); +      expect($xhrErr).not.toHaveBeenCalled(); +    }); +  });  }); diff --git a/test/service/xhr.bulkSpec.js b/test/service/xhr.bulkSpec.js index bc8d03f8..adcb61fa 100644 --- a/test/service/xhr.bulkSpec.js +++ b/test/service/xhr.bulkSpec.js @@ -4,13 +4,11 @@ describe('$xhr.bulk', function() {    var scope, $browser, $browserXhr, $log, $xhrBulk, $xhrError, log;    beforeEach(function(){ -    scope = angular.scope({}, angular.service, { -      '$xhr.error': $xhrError = jasmine.createSpy('$xhr.error'), -      '$log': $log = {} -    }); +    scope = angular.scope({}, null, {'$xhr.error': $xhrError = jasmine.createSpy('$xhr.error')});      $browser = scope.$service('$browser');      $browserXhr = $browser.xhr;      $xhrBulk = scope.$service('$xhr.bulk'); +    $log = scope.$service('$log');      log = '';    }); @@ -60,12 +58,29 @@ describe('$xhr.bulk', function() {      $browserXhr.flush();      expect($xhrError).toHaveBeenCalled(); -    var cb = $xhrError.mostRecentCall.args[0].callback; +    var cb = $xhrError.mostRecentCall.args[0].success;      expect(typeof cb).toEqual($function);      expect($xhrError).toHaveBeenCalledWith( -        {url:'/req1', method:'GET', data:null, callback:cb}, -        {status:404, response:'NotFound'}); +        {url: '/req1', method: 'GET', data: null, success: cb}, +        {status: 404, response: 'NotFound'});      expect(log).toEqual('"second";DONE');    }); + +  it('should handle non 200 status code by calling error callback if provided', function() { +    var callback = jasmine.createSpy('error'); + +    $xhrBulk.urls['/'] = {match: /.*/}; +    $xhrBulk('GET', '/req1', null, noop, callback); + +    $browserXhr.expectPOST('/', { +      requests:[{method: 'GET',  url: '/req1', data: null}] +    }).respond([{status: 404, response: 'NotFound'}]); + +    $xhrBulk.flush(); +    $browserXhr.flush(); + +    expect($xhrError).not.toHaveBeenCalled(); +    expect(callback).toHaveBeenCalledWith(404, 'NotFound'); +  });  }); diff --git a/test/service/xhr.cacheSpec.js b/test/service/xhr.cacheSpec.js index 905a9dae..f4654cd4 100644 --- a/test/service/xhr.cacheSpec.js +++ b/test/service/xhr.cacheSpec.js @@ -1,10 +1,10 @@  'use strict';  describe('$xhr.cache', function() { -  var scope, $browser, $browserXhr, cache, log; +  var scope, $browser, $browserXhr, $xhrErr, cache, log; -  beforeEach(function(){ -    scope = angular.scope(); +  beforeEach(function() { +    scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')});      $browser = scope.$service('$browser');      $browserXhr = $browser.xhr;      cache = scope.$service('$xhr.cache'); @@ -143,4 +143,36 @@ describe('$xhr.cache', function() {      $browser.defer.flush();      expect(evalSpy).toHaveBeenCalled();    }); + +  it('should call the error callback on error if provided', function() { +    var errorSpy = jasmine.createSpy('error'), +        successSpy = jasmine.createSpy('success'); + +    $browserXhr.expectGET('/url').respond(500, 'error'); + +    cache('GET', '/url', null, successSpy, errorSpy, false, true); +    $browserXhr.flush(); +    expect(errorSpy).toHaveBeenCalledWith(500, 'error'); +    expect(successSpy).not.toHaveBeenCalled(); + +    errorSpy.reset(); +    cache('GET', '/url', successSpy, errorSpy, false, true); +    $browserXhr.flush(); +    expect(errorSpy).toHaveBeenCalledWith(500, 'error'); +    expect(successSpy).not.toHaveBeenCalled(); +  }); + +  it('should call the $xhr.error on error if error callback not provided', function() { +    var errorSpy = jasmine.createSpy('error'), +        successSpy = jasmine.createSpy('success'); + +    $browserXhr.expectGET('/url').respond(500, 'error'); +    cache('GET', '/url', null, successSpy, false, true); +    $browserXhr.flush(); + +    expect(successSpy).not.toHaveBeenCalled(); +    expect($xhrErr).toHaveBeenCalledWith( +      {method: 'GET', url: '/url', data: null, success: successSpy}, +      {status: 500, body: 'error'}); +  });  }); diff --git a/test/service/xhr.errorSpec.js b/test/service/xhr.errorSpec.js index fdca93ec..d3af4565 100644 --- a/test/service/xhr.errorSpec.js +++ b/test/service/xhr.errorSpec.js @@ -29,10 +29,10 @@ describe('$xhr.error', function() {      $browserXhr.expectPOST('/req', 'MyData').respond(500, 'MyError');      $xhr('POST', '/req', 'MyData', callback);      $browserXhr.flush(); -    var cb = $xhrError.mostRecentCall.args[0].callback; +    var cb = $xhrError.mostRecentCall.args[0].success;      expect(typeof cb).toEqual($function);      expect($xhrError).toHaveBeenCalledWith( -        {url:'/req', method:'POST', data:'MyData', callback:cb}, -        {status:500, body:'MyError'}); +        {url: '/req', method: 'POST', data: 'MyData', success: cb}, +        {status: 500, body: 'MyError'});    });  }); diff --git a/test/service/xhrSpec.js b/test/service/xhrSpec.js index ed7cfc93..9f496535 100644 --- a/test/service/xhrSpec.js +++ b/test/service/xhrSpec.js @@ -1,12 +1,11 @@  'use strict';  describe('$xhr', function() { -  var scope, $browser, $browserXhr, $log, $xhr, log; +  var scope, $browser, $browserXhr, $log, $xhr, $xhrErr, log;    beforeEach(function(){ -    scope = angular.scope({}, angular.service, { '$log': $log = { -        error: dump -    } }); +    var scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')}); +    $log = scope.$service('$log');      $browser = scope.$service('$browser');      $browserXhr = $browser.xhr;      $xhr = scope.$service('$xhr'); @@ -57,12 +56,11 @@ describe('$xhr', function() {    it('should handle exceptions in callback', function(){ -    $log.error = jasmine.createSpy('$log.error');      $browserXhr.expectGET('/reqGET').respond('first');      $xhr('GET', '/reqGET', null, function(){ throw "MyException"; });      $browserXhr.flush(); -    expect($log.error).toHaveBeenCalledWith("MyException"); +    expect($log.error.logs.shift()).toContain('MyException');    }); @@ -104,6 +102,38 @@ describe('$xhr', function() {      expect(response).toEqual([1, 'abc', {foo:'bar'}]);    }); +  it('should call $xhr.error on error if no error callback provided', function() { +    var successSpy = jasmine.createSpy('success'); + +    $browserXhr.expectGET('/url').respond(500, 'error'); +    $xhr('GET', '/url', null, successSpy); +    $browserXhr.flush(); + +    expect(successSpy).not.toHaveBeenCalled(); +    expect($xhrErr).toHaveBeenCalledWith( +      {method: 'GET', url: '/url', data: null, success: successSpy}, +      {status: 500, body: 'error'} +    ); +  }); + +  it('should call the error callback on error if provided', function() { +    var errorSpy = jasmine.createSpy('error'), +        successSpy = jasmine.createSpy('success'); + +    $browserXhr.expectGET('/url').respond(500, 'error'); +    $xhr('GET', '/url', null, successSpy, errorSpy); +    $browserXhr.flush(); + +    expect(errorSpy).toHaveBeenCalledWith(500, 'error'); +    expect(successSpy).not.toHaveBeenCalled(); + +    errorSpy.reset(); +    $xhr('GET', '/url', successSpy, errorSpy); +    $browserXhr.flush(); + +    expect(errorSpy).toHaveBeenCalledWith(500, 'error'); +    expect(successSpy).not.toHaveBeenCalled(); +  });    describe('http headers', function() { | 
