diff options
| author | Misko Hevery | 2010-05-19 11:51:17 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2010-05-19 11:51:17 -0700 | 
| commit | 0f73084e9d21cea99f0535e6ca30a1341b7047dc (patch) | |
| tree | e4586731808a708ec0a8ce137c30e99e3cb7201b /test/servicesSpec.js | |
| parent | 1bdcf72e456c74256b14f98b26e969b9de637614 (diff) | |
| download | angular.js-0f73084e9d21cea99f0535e6ca30a1341b7047dc.tar.bz2 | |
added error handler to xhr requests
Diffstat (limited to 'test/servicesSpec.js')
| -rw-r--r-- | test/servicesSpec.js | 43 | 
1 files changed, 41 insertions, 2 deletions
diff --git a/test/servicesSpec.js b/test/servicesSpec.js index 17f71bdc..4e144dd1 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -1,8 +1,11 @@  describe("service", function(){ -  var scope; +  var scope, xhrErrorHandler;    beforeEach(function(){ -    scope = createScope(null, angularService, {}); +    xhrErrorHandler = jasmine.createSpy('$xhr.error'); +    scope = createScope(null, angularService, { +      '$xhr.error': xhrErrorHandler +    });    });    afterEach(function(){ @@ -194,6 +197,17 @@ describe("service", function(){        expect(log).toEqual('"third";["second"];"first";');      }); +    it('should handle non 200 status codes by forwarding to error handler', function(){ +      xhr.expectPOST('/req', 'MyData').respond(500, 'MyError'); +      scope.$xhr('POST', '/req', 'MyData', callback); +      xhr.flush(); +      var cb = xhrErrorHandler.mostRecentCall.args[0].callback; +      expect(typeof cb).toEqual('function'); +      expect(xhrErrorHandler).wasCalledWith( +          {url:'/req', method:'POST', data:'MyData', callback:cb}, +          {status:500, body:'MyError'}); +    }); +      describe('bulk', function(){        it('should collect requests', function(){          scope.$xhr.bulk.urls["/"] = {match:/.*/}; @@ -211,6 +225,31 @@ describe("service", function(){          xhr.flush();          expect(log).toEqual('"first";"second";DONE');        }); + +      it('should handle non 200 status code by forwarding to error handler', function(){ +        scope.$xhr.bulk.urls['/'] = {match:/.*/}; +        scope.$xhr.bulk('GET', '/req1', null, callback); +        scope.$xhr.bulk('POST', '/req2', {post:'data'}, callback); + +        xhr.expectPOST('/', { +          requests:[{method:'GET',  url:'/req1', data: null}, +                    {method:'POST', url:'/req2', data:{post:'data'} }] +        }).respond([ +          {status:404, response:'NotFound'}, +          {status:200, response:'second'} +        ]); +        scope.$xhr.bulk.flush(function(){ log += 'DONE';}); +        xhr.flush(); + +        expect(xhrErrorHandler).wasCalled(); +        var cb = xhrErrorHandler.mostRecentCall.args[0].callback; +        expect(typeof cb).toEqual('function'); +        expect(xhrErrorHandler).wasCalledWith( +            {url:'/req1', method:'GET', data:null, callback:cb}, +            {status:404, body:'NotFound'}); + +        expect(log).toEqual('"second";DONE'); +      });      });      describe('cache', function(){  | 
