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

Commit c37bfde

Browse files
kseamonIgorMinar
authored andcommitted
fix($resource): properly call error callback when resource is called with two arguments
1 parent f6bcbb5 commit c37bfde

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Resource.js

+7
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,16 @@ ResourceFactory.prototype = {
7676
case 4:
7777
error = a4;
7878
success = a3;
79+
//fallthrough
7980
case 3:
8081
case 2:
8182
if (isFunction(a2)) {
83+
if (isFunction(a1)) {
84+
success = a1;
85+
error = a2;
86+
break;
87+
}
88+
8289
success = a2;
8390
error = a3;
8491
//fallthrough

test/ResourceSpec.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,35 @@ describe("resource", function() {
244244

245245
describe('failure mode', function() {
246246
var ERROR_CODE = 500,
247-
ERROR_RESPONSE = 'Server Error';
247+
ERROR_RESPONSE = 'Server Error',
248+
errorCB;
248249

249250
beforeEach(function() {
250-
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
251+
errorCB = jasmine.createSpy();
251252
});
252253

253254
it('should report error when non 2xx if error callback is not provided', function() {
255+
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
254256
CreditCard.get({id:123});
255257
xhr.flush();
256258
expect($xhrErr).toHaveBeenCalled();
257259
});
258260

259261
it('should call the error callback if provided on non 2xx response', function() {
260-
CreditCard.get({id:123}, noop, callback);
262+
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
263+
CreditCard.get({id:123}, callback, errorCB);
264+
xhr.flush();
265+
expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);
266+
expect(callback).not.toHaveBeenCalled();
267+
expect($xhrErr).not.toHaveBeenCalled();
268+
});
269+
270+
it('should call the error callback if provided on non 2xx response', function() {
271+
xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);
272+
CreditCard.get(callback, errorCB);
261273
xhr.flush();
262-
expect(callback).toHaveBeenCalledWith(500, ERROR_RESPONSE);
274+
expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);
275+
expect(callback).not.toHaveBeenCalled();
263276
expect($xhrErr).not.toHaveBeenCalled();
264277
});
265278
});

0 commit comments

Comments
 (0)