2011-07-17 08:05:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
describe("resource", function() {
|
|
|
|
|
var xhr, resource, CreditCard, callback;
|
|
|
|
|
|
|
|
|
|
beforeEach(function(){
|
2010-04-27 18:18:08 +00:00
|
|
|
var browser = new MockBrowser();
|
|
|
|
|
xhr = browser.xhr;
|
|
|
|
|
resource = new ResourceFactory(xhr);
|
2010-03-15 22:57:12 +00:00
|
|
|
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
|
2010-03-15 21:36:50 +00:00
|
|
|
charge:{
|
|
|
|
|
method:'POST',
|
|
|
|
|
params:{verb:'!charge'}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
callback = jasmine.createSpy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should build resource", function(){
|
2010-08-18 23:23:12 +00:00
|
|
|
expect(typeof CreditCard).toBe($function);
|
|
|
|
|
expect(typeof CreditCard.get).toBe($function);
|
|
|
|
|
expect(typeof CreditCard.save).toBe($function);
|
|
|
|
|
expect(typeof CreditCard.remove).toBe($function);
|
|
|
|
|
expect(typeof CreditCard['delete']).toBe($function);
|
|
|
|
|
expect(typeof CreditCard.query).toBe($function);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
2010-03-16 21:48:11 +00:00
|
|
|
it('should default to empty parameters', function(){
|
|
|
|
|
xhr.expectGET('URL').respond({});
|
|
|
|
|
resource.route('URL').query();
|
|
|
|
|
});
|
|
|
|
|
|
2010-07-22 18:18:32 +00:00
|
|
|
it('should ignore slashes of undefinend parameters', function(){
|
|
|
|
|
var R = resource.route('/Path/:a/:b/:c');
|
|
|
|
|
xhr.expectGET('/Path').respond({});
|
|
|
|
|
xhr.expectGET('/Path/1').respond({});
|
|
|
|
|
xhr.expectGET('/Path/2/3').respond({});
|
|
|
|
|
xhr.expectGET('/Path/4/5/6').respond({});
|
|
|
|
|
R.get({});
|
|
|
|
|
R.get({a:1});
|
|
|
|
|
R.get({a:2, b:3});
|
|
|
|
|
R.get({a:4, b:5, c:6});
|
|
|
|
|
});
|
|
|
|
|
|
2011-02-11 01:57:42 +00:00
|
|
|
it('should correctly encode url params', function(){
|
|
|
|
|
var R = resource.route('/Path/:a');
|
|
|
|
|
xhr.expectGET('/Path/foo%231').respond({});
|
2011-02-17 00:48:21 +00:00
|
|
|
xhr.expectGET('/Path/doh!@foo?bar=baz%231').respond({});
|
2011-02-11 01:57:42 +00:00
|
|
|
R.get({a: 'foo#1'});
|
|
|
|
|
R.get({a: 'doh!@foo', bar: 'baz#1'});
|
|
|
|
|
});
|
|
|
|
|
|
2011-02-17 00:48:21 +00:00
|
|
|
it('should not encode @ in url params', function() {
|
2011-04-01 04:45:28 +00:00
|
|
|
//encodeURIComponent is too agressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
|
2011-02-17 00:48:21 +00:00
|
|
|
//with regards to the character set (pchar) allowed in path segments
|
|
|
|
|
//so we need this test to make sure that we don't over-encode the params and break stuff like
|
|
|
|
|
//buzz api which uses @self
|
|
|
|
|
|
|
|
|
|
var R = resource.route('/Path/:a');
|
2011-04-01 04:45:28 +00:00
|
|
|
xhr.expectGET('/Path/doh@fo%20o?!do%26h=g%3Da+h&:bar=$baz@1').respond({});
|
|
|
|
|
R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'});
|
2011-03-25 16:28:42 +00:00
|
|
|
});
|
2011-02-17 00:48:21 +00:00
|
|
|
|
2011-03-18 17:31:28 +00:00
|
|
|
it('should encode & in url params', function() {
|
|
|
|
|
var R = resource.route('/Path/:a');
|
|
|
|
|
xhr.expectGET('/Path/doh&foo?bar=baz%261').respond({});
|
|
|
|
|
R.get({a: 'doh&foo', bar: 'baz&1'});
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 22:57:12 +00:00
|
|
|
it("should build resource with default param", function(){
|
|
|
|
|
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});
|
2010-07-13 21:15:01 +00:00
|
|
|
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05});
|
2010-03-15 22:57:12 +00:00
|
|
|
var item = LineItem.get({id:456});
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(item).toEqual({id:'abc'});
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
it("should create resource", function(){
|
2010-04-27 18:18:08 +00:00
|
|
|
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'});
|
2010-03-15 21:36:50 +00:00
|
|
|
|
|
|
|
|
var cc = CreditCard.save({name:'misko'}, callback);
|
|
|
|
|
nakedExpect(cc).toEqual({name:'misko'});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2010-03-15 21:36:50 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:123, name:'misko'});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).toHaveBeenCalledWith(cc);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should read resource", function(){
|
|
|
|
|
xhr.expectGET("/CreditCard/123").respond({id:123, number:'9876'});
|
|
|
|
|
var cc = CreditCard.get({id:123}, callback);
|
|
|
|
|
expect(cc instanceof CreditCard).toBeTruthy();
|
|
|
|
|
nakedExpect(cc).toEqual({});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2010-03-15 21:36:50 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:123, number:'9876'});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).toHaveBeenCalledWith(cc);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
2010-09-15 17:27:58 +00:00
|
|
|
it("should read partial resource", function(){
|
|
|
|
|
xhr.expectGET("/CreditCard").respond([{id:{key:123}}]);
|
|
|
|
|
xhr.expectGET("/CreditCard/123").respond({id:{key:123}, number:'9876'});
|
|
|
|
|
var ccs = CreditCard.query();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
expect(ccs.length).toEqual(1);
|
|
|
|
|
var cc = ccs[0];
|
|
|
|
|
expect(cc instanceof CreditCard).toBeTruthy();
|
|
|
|
|
expect(cc.number).not.toBeDefined();
|
|
|
|
|
cc.$get(callback);
|
|
|
|
|
xhr.flush();
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).toHaveBeenCalledWith(cc);
|
2010-09-15 17:27:58 +00:00
|
|
|
expect(cc.number).toEqual('9876');
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
it("should update resource", function(){
|
2010-04-27 18:18:08 +00:00
|
|
|
xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'});
|
2010-03-15 21:36:50 +00:00
|
|
|
|
|
|
|
|
var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback);
|
|
|
|
|
nakedExpect(cc).toEqual({id:{key:123}, name:'misko'});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2010-03-15 21:36:50 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should query resource", function(){
|
|
|
|
|
xhr.expectGET("/CreditCard?key=value").respond([{id:1}, {id:2}]);
|
|
|
|
|
|
|
|
|
|
var ccs = CreditCard.query({key:'value'}, callback);
|
|
|
|
|
expect(ccs).toEqual([]);
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2010-03-15 21:36:50 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).toHaveBeenCalledWith(ccs);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
2010-05-07 19:09:14 +00:00
|
|
|
it("should have all arguments optional", function(){
|
|
|
|
|
xhr.expectGET('/CreditCard').respond([{id:1}]);
|
|
|
|
|
var log = '';
|
|
|
|
|
var ccs = CreditCard.query(function(){ log += 'cb;'; });
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(ccs).toEqual([{id:1}]);
|
|
|
|
|
expect(log).toEqual('cb;');
|
|
|
|
|
});
|
|
|
|
|
|
2011-04-04 23:04:37 +00:00
|
|
|
it('should delete resource and call callback', function(){
|
|
|
|
|
xhr.expectDELETE("/CreditCard/123").respond(200, {});
|
2010-03-15 21:36:50 +00:00
|
|
|
|
|
|
|
|
CreditCard.remove({id:123}, callback);
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2010-03-15 21:36:50 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
|
2011-04-04 23:04:37 +00:00
|
|
|
|
|
|
|
|
callback.reset();
|
|
|
|
|
xhr.expectDELETE("/CreditCard/333").respond(204, null);
|
|
|
|
|
CreditCard.remove({id:333}, callback);
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2011-04-04 23:04:37 +00:00
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should post charge verb', function(){
|
2010-04-27 18:18:08 +00:00
|
|
|
xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'});
|
2010-03-15 21:36:50 +00:00
|
|
|
|
|
|
|
|
CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback);
|
|
|
|
|
});
|
|
|
|
|
|
2010-08-10 18:17:08 +00:00
|
|
|
it('should post charge verb on instance', function(){
|
|
|
|
|
xhr.expectPOST('/CreditCard/123!charge?amount=10', {id:{key:123}, name:'misko'}).respond({success:'ok'});
|
|
|
|
|
|
|
|
|
|
var card = new CreditCard({id:{key:123}, name:'misko'});
|
|
|
|
|
card.$charge({amount:10}, callback);
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
it('should create on save', function(){
|
2010-04-27 18:18:08 +00:00
|
|
|
xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123});
|
2010-03-15 21:36:50 +00:00
|
|
|
var cc = new CreditCard();
|
2010-09-15 17:27:58 +00:00
|
|
|
expect(cc.$get).toBeDefined();
|
|
|
|
|
expect(cc.$query).toBeDefined();
|
2010-03-15 21:36:50 +00:00
|
|
|
expect(cc.$remove).toBeDefined();
|
|
|
|
|
expect(cc.$save).toBeDefined();
|
|
|
|
|
|
|
|
|
|
cc.name = 'misko';
|
|
|
|
|
cc.$save(callback);
|
|
|
|
|
nakedExpect(cc).toEqual({name:'misko'});
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:123});
|
2011-05-31 08:32:54 +00:00
|
|
|
expect(callback).toHaveBeenCalledWith(cc);
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|
|
|
|
|
|
2011-03-30 22:22:14 +00:00
|
|
|
it('should not mutate the resource object if response contains no body', function(){
|
2011-03-22 17:51:48 +00:00
|
|
|
var data = {id:{key:123}, number:'9876'};
|
|
|
|
|
xhr.expectGET("/CreditCard/123").respond(data);
|
|
|
|
|
var cc = CreditCard.get({id:123});
|
|
|
|
|
xhr.flush();
|
|
|
|
|
expect(cc instanceof CreditCard).toBeTruthy();
|
|
|
|
|
var idBefore = cc.id;
|
|
|
|
|
|
|
|
|
|
xhr.expectPOST("/CreditCard/123", data).respond('');
|
|
|
|
|
cc.$save();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
expect(idBefore).toEqual(cc.id);
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 22:57:12 +00:00
|
|
|
it('should bind default parameters', function(){
|
|
|
|
|
xhr.expectGET('/CreditCard/123.visa?minimum=0.05').respond({id:123});
|
|
|
|
|
var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});
|
|
|
|
|
var visa = Visa.get({id:123});
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(visa).toEqual({id:123});
|
|
|
|
|
});
|
2010-03-15 21:36:50 +00:00
|
|
|
|
2010-04-30 00:28:33 +00:00
|
|
|
it('should excersize full stack', function(){
|
2011-02-25 22:03:02 +00:00
|
|
|
var scope = angular.compile('<div></div>')();
|
2011-01-04 19:53:23 +00:00
|
|
|
var $browser = scope.$service('$browser');
|
|
|
|
|
var $resource = scope.$service('$resource');
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
var Person = $resource('/Person/:id');
|
2010-12-23 17:03:34 +00:00
|
|
|
$browser.xhr.expectGET('/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
|
2010-04-30 00:28:33 +00:00
|
|
|
var person = Person.get({id:123});
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
$browser.xhr.flush();
|
2010-04-30 00:28:33 +00:00
|
|
|
expect(person.name).toEqual('misko');
|
2010-12-02 04:29:54 +00:00
|
|
|
dealoc(scope);
|
2010-04-30 00:28:33 +00:00
|
|
|
});
|
|
|
|
|
|
2010-07-13 21:09:53 +00:00
|
|
|
it('should return the same object when verifying the cache', function(){
|
2011-02-25 22:03:02 +00:00
|
|
|
var scope = angular.compile('<div></div>')();
|
2011-01-04 19:53:23 +00:00
|
|
|
var $browser = scope.$service('$browser');
|
|
|
|
|
var $resource = scope.$service('$resource');
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}});
|
2010-12-23 17:03:34 +00:00
|
|
|
$browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]');
|
2010-07-13 21:09:53 +00:00
|
|
|
var person = Person.query({id:123});
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
$browser.xhr.flush();
|
2010-07-13 21:09:53 +00:00
|
|
|
expect(person[0].name).toEqual('misko');
|
|
|
|
|
|
2010-12-23 17:03:34 +00:00
|
|
|
$browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"rob"\n}\n]');
|
2010-07-13 21:09:53 +00:00
|
|
|
var person2 = Person.query({id:123});
|
2010-12-05 07:49:26 +00:00
|
|
|
$browser.defer.flush();
|
|
|
|
|
|
2010-07-13 21:09:53 +00:00
|
|
|
expect(person2[0].name).toEqual('misko');
|
|
|
|
|
var person2Cache = person2;
|
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject
- inject method can return, instance, or call function which have $inject
property
- initialize services with $creation=[eager|eager-publish] this means that
only some of the services are now globally accessible
- upgraded $become on scope to use injector hence respect the $inject property
for injection
- $become should not be run multiple times and will most likely be removed
in future version
- added $new on scope to create a child scope
- $inject is respected on constructor function
- simplified scopes so that they no longer have separate __proto__ for
parent, api, behavior and instance this should speed up execution since
scope will now create one __proto__ chain per scope (not three).
BACKWARD COMPATIBILITY WARNING:
- services now need to have $inject instead of inject property for proper
injection this breaks backward compatibility
- not all services are now published into root scope
(only: $location, $cookie, $window)
- if you have widget/directive which uses services on scope
(such as this.$xhr), you will now have to inject that service in
(as it is not published on the root scope anymore)
2010-10-09 00:30:13 +00:00
|
|
|
$browser.xhr.flush();
|
2010-07-13 21:09:53 +00:00
|
|
|
expect(person2Cache).toEqual(person2);
|
|
|
|
|
expect(person2[0].name).toEqual('rob');
|
2010-12-02 04:29:54 +00:00
|
|
|
dealoc(scope);
|
2010-07-13 21:09:53 +00:00
|
|
|
});
|
|
|
|
|
|
2010-05-19 18:51:17 +00:00
|
|
|
describe('failure mode', function(){
|
|
|
|
|
it('should report error when non 200', function(){
|
|
|
|
|
xhr.expectGET('/CreditCard/123').respond(500, "Server Error");
|
|
|
|
|
var cc = CreditCard.get({id:123});
|
|
|
|
|
try {
|
|
|
|
|
xhr.flush();
|
|
|
|
|
fail('expected exception, non thrown');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
expect(e.status).toEqual(500);
|
|
|
|
|
expect(e.response).toEqual('Server Error');
|
|
|
|
|
expect(e.message).toEqual('500: Server Error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|