fix($resource): properly call error callback when resource is called with two arguments

This commit is contained in:
Karl Seamon 2011-08-18 15:07:04 -04:00
parent b99b0a8072
commit 6114c8f504
2 changed files with 24 additions and 4 deletions

View file

@ -76,9 +76,16 @@ ResourceFactory.prototype = {
case 4: case 4:
error = a4; error = a4;
success = a3; success = a3;
//fallthrough
case 3: case 3:
case 2: case 2:
if (isFunction(a2)) { if (isFunction(a2)) {
if (isFunction(a1)) {
success = a1;
error = a2;
break;
}
success = a2; success = a2;
error = a3; error = a3;
//fallthrough //fallthrough

View file

@ -244,22 +244,35 @@ describe("resource", function() {
describe('failure mode', function() { describe('failure mode', function() {
var ERROR_CODE = 500, var ERROR_CODE = 500,
ERROR_RESPONSE = 'Server Error'; ERROR_RESPONSE = 'Server Error',
errorCB;
beforeEach(function() { beforeEach(function() {
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE); errorCB = jasmine.createSpy();
}); });
it('should report error when non 2xx if error callback is not provided', function() { it('should report error when non 2xx if error callback is not provided', function() {
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get({id:123}); CreditCard.get({id:123});
xhr.flush(); xhr.flush();
expect($xhrErr).toHaveBeenCalled(); expect($xhrErr).toHaveBeenCalled();
}); });
it('should call the error callback if provided on non 2xx response', function() { it('should call the error callback if provided on non 2xx response', function() {
CreditCard.get({id:123}, noop, callback); xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get({id:123}, callback, errorCB);
xhr.flush(); xhr.flush();
expect(callback).toHaveBeenCalledWith(500, ERROR_RESPONSE); expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);
expect(callback).not.toHaveBeenCalled();
expect($xhrErr).not.toHaveBeenCalled();
});
it('should call the error callback if provided on non 2xx response', function() {
xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);
CreditCard.get(callback, errorCB);
xhr.flush();
expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE);
expect(callback).not.toHaveBeenCalled();
expect($xhrErr).not.toHaveBeenCalled(); expect($xhrErr).not.toHaveBeenCalled();
}); });
}); });