Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 26db937

Browse files
gkalpakpetebacondarwin
authored andcommitted
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 #14837.
1 parent 1d0e582 commit 26db937

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

@@ -886,6 +886,7 @@ describe("basic usage", function() {
886886
expect(cc.url).toBe('/new-id');
887887
});
888888

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

@@ -1003,6 +1004,7 @@ describe("basic usage", function() {
10031004
});
10041005
});
10051006

1007+
10061008
it('should allow per action response interceptor that gets full response', function() {
10071009
CreditCard = $resource('/CreditCard', {}, {
10081010
query: {
@@ -1058,6 +1060,46 @@ describe("basic usage", function() {
10581060
expect(response.status).toBe(404);
10591061
expect(response.config).toBeDefined();
10601062
});
1063+
1064+
1065+
it('should fulfill the promise with the value returned by the responseError interceptor',
1066+
inject(function($q) {
1067+
CreditCard = $resource('/CreditCard', {}, {
1068+
test1: {
1069+
method: 'GET',
1070+
interceptor: {responseError: function() { return 'foo'; }}
1071+
},
1072+
test2: {
1073+
method: 'GET',
1074+
interceptor: {responseError: function() { return $q.resolve('bar'); }}
1075+
},
1076+
test3: {
1077+
method: 'GET',
1078+
interceptor: {responseError: function() { return $q.reject('baz'); }}
1079+
}
1080+
});
1081+
1082+
$httpBackend.whenGET('/CreditCard').respond(404);
1083+
1084+
callback.calls.reset();
1085+
CreditCard.test1().$promise.then(callback);
1086+
$httpBackend.flush();
1087+
1088+
expect(callback).toHaveBeenCalledOnceWith('foo');
1089+
1090+
callback.calls.reset();
1091+
CreditCard.test2().$promise.then(callback);
1092+
$httpBackend.flush();
1093+
1094+
expect(callback).toHaveBeenCalledOnceWith('bar');
1095+
1096+
callback.calls.reset();
1097+
CreditCard.test3().$promise.then(null, callback);
1098+
$httpBackend.flush();
1099+
1100+
expect(callback).toHaveBeenCalledOnceWith('baz');
1101+
})
1102+
);
10611103
});
10621104

10631105

@@ -1393,6 +1435,33 @@ describe('errors', function() {
13931435
});
13941436
});
13951437

1438+
describe('handling rejections', function() {
1439+
var $httpBackend;
1440+
var $resource;
1441+
1442+
beforeEach(module('ngResource'));
1443+
1444+
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1445+
$httpBackend = _$httpBackend_;
1446+
$resource = _$resource_;
1447+
1448+
$httpBackend.whenGET('/CreditCard').respond(404);
1449+
}));
1450+
1451+
1452+
it('should reject the promise even when there is an error callback', function() {
1453+
var errorCb1 = jasmine.createSpy('errorCb1');
1454+
var errorCb2 = jasmine.createSpy('errorCb2');
1455+
var CreditCard = $resource('/CreditCard');
1456+
1457+
CreditCard.get(noop, errorCb1).$promise.catch(errorCb2);
1458+
$httpBackend.flush();
1459+
1460+
expect(errorCb1).toHaveBeenCalledOnce();
1461+
expect(errorCb2).toHaveBeenCalledOnce();
1462+
});
1463+
});
1464+
13961465
describe('cancelling requests', function() {
13971466
var httpSpy;
13981467
var $httpBackend;

0 commit comments

Comments
 (0)