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(){
|
|
|
|
|
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-16 21:48:11 +00:00
|
|
|
it('should default to empty parameters', function(){
|
|
|
|
|
xhr.expectGET('URL').respond({});
|
|
|
|
|
resource.route('URL').query();
|
|
|
|
|
});
|
|
|
|
|
|
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'});
|
|
|
|
|
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05});
|
|
|
|
|
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'});
|
|
|
|
|
expect(callback).wasNotCalled();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:123, name:'misko'});
|
|
|
|
|
expect(callback).wasCalledWith(cc);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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({});
|
|
|
|
|
expect(callback).wasNotCalled();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:123, number:'9876'});
|
|
|
|
|
expect(callback).wasCalledWith(cc);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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'});
|
|
|
|
|
expect(callback).wasNotCalled();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(cc).toEqual({id:{key:123}, name:'rama'});
|
|
|
|
|
expect(callback).wasCalledWith(cc);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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([]);
|
|
|
|
|
expect(callback).wasNotCalled();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
|
|
|
|
|
expect(callback).wasCalledWith(ccs);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete resource', function(){
|
|
|
|
|
xhr.expectDELETE("/CreditCard/123").respond({});
|
|
|
|
|
|
|
|
|
|
CreditCard.remove({id:123}, callback);
|
|
|
|
|
expect(callback).wasNotCalled();
|
|
|
|
|
xhr.flush();
|
|
|
|
|
nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
expect(cc.$get).not.toBeDefined();
|
|
|
|
|
expect(cc.$query).not.toBeDefined();
|
|
|
|
|
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});
|
|
|
|
|
expect(callback).wasCalledWith(cc);
|
|
|
|
|
});
|
|
|
|
|
|
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(){
|
|
|
|
|
var scope = angular.compile('<div></div>');
|
|
|
|
|
var Person = scope.$resource('/Person/:id');
|
|
|
|
|
scope.$browser.xhr.expectGET('/Person/123').respond('\n{\nname:\n"misko"\n}\n');
|
|
|
|
|
var person = Person.get({id:123});
|
|
|
|
|
scope.$browser.xhr.flush();
|
|
|
|
|
expect(person.name).toEqual('misko');
|
|
|
|
|
});
|
|
|
|
|
|
2010-03-15 21:36:50 +00:00
|
|
|
});
|