Skip to content

Commit 7ca3d80

Browse files
committed
test($resource): add some tests wrt handling response errors
The test currently fail on master, but should pass on 1.5.x. A subsequent commit will fix the regressions on master. (This commit should be backportable to 1.5.x.) Related to angular#14837.
1 parent 7ce7e09 commit 7ca3d80

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

test/ngResource/resourceSpec.js

+70-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("basic usage", function() {
3030
}
3131

3232
});
33-
callback = jasmine.createSpy();
33+
callback = jasmine.createSpy('callback');
3434
}));
3535

3636

@@ -907,6 +907,7 @@ describe("basic usage", function() {
907907
expect(cc.url).toBe('/new-id');
908908
});
909909

910+
910911
it('should pass the same transformed value to success callbacks and to promises', function() {
911912
$httpBackend.expect('GET', '/CreditCard').respond(200, { value: 'original' });
912913

@@ -1024,6 +1025,7 @@ describe("basic usage", function() {
10241025
});
10251026
});
10261027

1028+
10271029
it('should allow per action response interceptor that gets full response', function() {
10281030
CreditCard = $resource('/CreditCard', {}, {
10291031
query: {
@@ -1079,6 +1081,46 @@ describe("basic usage", function() {
10791081
expect(response.status).toBe(404);
10801082
expect(response.config).toBeDefined();
10811083
});
1084+
1085+
1086+
it('should fulfill the promise with the value returned by the responseError interceptor',
1087+
inject(function($q) {
1088+
CreditCard = $resource('/CreditCard', {}, {
1089+
test1: {
1090+
method: 'GET',
1091+
interceptor: {responseError: function() { return 'foo'; }}
1092+
},
1093+
test2: {
1094+
method: 'GET',
1095+
interceptor: {responseError: function() { return $q.resolve('bar'); }}
1096+
},
1097+
test3: {
1098+
method: 'GET',
1099+
interceptor: {responseError: function() { return $q.reject('baz'); }}
1100+
}
1101+
});
1102+
1103+
$httpBackend.whenGET('/CreditCard').respond(404);
1104+
1105+
callback.calls.reset();
1106+
CreditCard.test1().$promise.then(callback);
1107+
$httpBackend.flush();
1108+
1109+
expect(callback).toHaveBeenCalledOnceWith('foo');
1110+
1111+
callback.calls.reset();
1112+
CreditCard.test2().$promise.then(callback);
1113+
$httpBackend.flush();
1114+
1115+
expect(callback).toHaveBeenCalledOnceWith('bar');
1116+
1117+
callback.calls.reset();
1118+
CreditCard.test3().$promise.then(null, callback);
1119+
$httpBackend.flush();
1120+
1121+
expect(callback).toHaveBeenCalledOnceWith('baz');
1122+
})
1123+
);
10821124
});
10831125

10841126

@@ -1414,6 +1456,33 @@ describe('errors', function() {
14141456
});
14151457
});
14161458

1459+
describe('handling rejections', function() {
1460+
var $httpBackend;
1461+
var $resource;
1462+
1463+
beforeEach(module('ngResource'));
1464+
1465+
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1466+
$httpBackend = _$httpBackend_;
1467+
$resource = _$resource_;
1468+
1469+
$httpBackend.whenGET('/CreditCard').respond(404);
1470+
}));
1471+
1472+
1473+
it('should reject the promise even when there is an error callback', function() {
1474+
var errorCb1 = jasmine.createSpy('errorCb1');
1475+
var errorCb2 = jasmine.createSpy('errorCb2');
1476+
var CreditCard = $resource('/CreditCard');
1477+
1478+
CreditCard.get(noop, errorCb1).$promise.catch(errorCb2);
1479+
$httpBackend.flush();
1480+
1481+
expect(errorCb1).toHaveBeenCalledOnce();
1482+
expect(errorCb2).toHaveBeenCalledOnce();
1483+
});
1484+
});
1485+
14171486
describe('cancelling requests', function() {
14181487
var httpSpy;
14191488
var $httpBackend;

0 commit comments

Comments
 (0)