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

Commit 4487cff

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 c13c666 commit 4487cff

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

@@ -1457,6 +1499,33 @@ describe('errors', function() {
14571499
});
14581500
});
14591501

1502+
describe('handling rejections', function() {
1503+
var $httpBackend;
1504+
var $resource;
1505+
1506+
beforeEach(module('ngResource'));
1507+
1508+
beforeEach(inject(function(_$httpBackend_, _$resource_) {
1509+
$httpBackend = _$httpBackend_;
1510+
$resource = _$resource_;
1511+
1512+
$httpBackend.whenGET('/CreditCard').respond(404);
1513+
}));
1514+
1515+
1516+
it('should reject the promise even when there is an error callback', function() {
1517+
var errorCb1 = jasmine.createSpy('errorCb1');
1518+
var errorCb2 = jasmine.createSpy('errorCb2');
1519+
var CreditCard = $resource('/CreditCard');
1520+
1521+
CreditCard.get(noop, errorCb1).$promise.catch(errorCb2);
1522+
$httpBackend.flush();
1523+
1524+
expect(errorCb1).toHaveBeenCalledOnce();
1525+
expect(errorCb2).toHaveBeenCalledOnce();
1526+
});
1527+
});
1528+
14601529
describe('cancelling requests', function() {
14611530
var httpSpy;
14621531
var $httpBackend;

0 commit comments

Comments
 (0)