From cc71b745c3c821f5e012a363ae3267252a81fddb Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 15 Mar 2010 14:36:50 -0700 Subject: added resources; removed compiled code --- test/ResourceSpec.js | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 test/ResourceSpec.js (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js new file mode 100644 index 00000000..562d6500 --- /dev/null +++ b/test/ResourceSpec.js @@ -0,0 +1,159 @@ +function MockXHR(){ + this.expectations = { + 'GET': {}, + 'POST': {}, + 'DELETE': {} + }; + this.queue = []; +} +MockXHR.prototype = { + method: function(verb, url, data, callback) { + if (verb == 'POST') + url += '|' + angular.toJson(data); + var response = this.expectations[verb][url]; + if (!response) + throw "No expectation for " + verb + " on '" + url + "'."; + this.queue.push(function(){ + callback(response); + }); + }, + + expectGET: function(url) { + var self = this; + return { + respond: function(response){ + self.expectations.GET[url] = response; + } + }; + }, + + expectDELETE: function(url) { + var self = this; + return { + respond: function(response){ + self.expectations.DELETE[url] = response; + } + }; + }, + + expectPOST: function(url) { + var self = this; + return { + data: function(data){ + return { + respond: function(response){ + self.expectations.POST[url + '|' + angular.toJson(data)] = response; + } + }; + } + }; + }, + + flush: function(){ + while(this.queue.length) { + this.queue.shift()(); + } + } +}; + +describe("resource", function() { + var xhr, resource, CreditCard, callback; + + beforeEach(function(){ + xhr = new MockXHR(); + resource = new ResourceFactory(xhr); + CreditCard = resource.route('/CreditCard/:id:verb', {id:'id.key'}, { + 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'); + }); + + it("should create resource", function(){ + xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'}); + + 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(){ + xhr.expectPOST('/CreditCard/123').data({id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); + + 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(){ + xhr.expectPOST('/CreditCard/123!charge?amount=10').data({auth:'abc'}).respond({success:'ok'}); + + CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback); + }); + + it('should create on save', function(){ + xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123}); + 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); + }); + + +}); -- cgit v1.2.3 From 79b743e52feb2c57ba0ae42d6d2742bc2189b22f Mon Sep 17 00:00:00 2001 From: Adam Abrons Date: Mon, 15 Mar 2010 15:57:12 -0700 Subject: resources, with bind() --- test/ResourceSpec.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 562d6500..799c7378 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -62,7 +62,7 @@ describe("resource", function() { beforeEach(function(){ xhr = new MockXHR(); resource = new ResourceFactory(xhr); - CreditCard = resource.route('/CreditCard/:id:verb', {id:'id.key'}, { + CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { charge:{ method:'POST', params:{verb:'!charge'} @@ -80,6 +80,15 @@ describe("resource", function() { expect(typeof CreditCard.query).toBe('function'); }); + 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'}); + + }); + it("should create resource", function(){ xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'}); @@ -155,5 +164,12 @@ describe("resource", function() { expect(callback).wasCalledWith(cc); }); + 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}); + }); }); -- cgit v1.2.3 From c9aba8b442adce496f0600c88764f7ffcc166879 Mon Sep 17 00:00:00 2001 From: Adam Abrons Date: Tue, 16 Mar 2010 14:48:11 -0700 Subject: make xhr just a method --- test/ResourceSpec.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 799c7378..0c7af00a 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -61,7 +61,7 @@ describe("resource", function() { beforeEach(function(){ xhr = new MockXHR(); - resource = new ResourceFactory(xhr); + resource = new ResourceFactory(_(xhr.method).bind(xhr)); CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { charge:{ method:'POST', @@ -80,6 +80,11 @@ describe("resource", function() { expect(typeof CreditCard.query).toBe('function'); }); + it('should default to empty parameters', function(){ + xhr.expectGET('URL').respond({}); + resource.route('URL').query(); + }); + 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}); -- cgit v1.2.3 From e55c97debaa0ef8487ece219b6eadbc147ece1f9 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 29 Mar 2010 20:25:42 -0700 Subject: dissabled a lot of tests, and made the core test set pass. --- test/ResourceSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 0c7af00a..91900a91 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -61,7 +61,7 @@ describe("resource", function() { beforeEach(function(){ xhr = new MockXHR(); - resource = new ResourceFactory(_(xhr.method).bind(xhr)); + resource = new ResourceFactory(bind(xhr, xhr.method)); CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { charge:{ method:'POST', -- cgit v1.2.3 From fce48eb60a47be87a3d95e0750e54c19c2a346d0 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 27 Apr 2010 11:18:08 -0700 Subject: resources now use browser mock --- test/ResourceSpec.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 91900a91..08ca86b3 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -60,8 +60,9 @@ describe("resource", function() { var xhr, resource, CreditCard, callback; beforeEach(function(){ - xhr = new MockXHR(); - resource = new ResourceFactory(bind(xhr, xhr.method)); + var browser = new MockBrowser(); + xhr = browser.xhr; + resource = new ResourceFactory(xhr); CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, { charge:{ method:'POST', @@ -95,7 +96,7 @@ describe("resource", function() { }); it("should create resource", function(){ - xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'}); + xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123, name:'misko'}); var cc = CreditCard.save({name:'misko'}, callback); nakedExpect(cc).toEqual({name:'misko'}); @@ -117,7 +118,7 @@ describe("resource", function() { }); it("should update resource", function(){ - xhr.expectPOST('/CreditCard/123').data({id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); + xhr.expectPOST('/CreditCard/123', {id:{key:123}, name:'misko'}).respond({id:{key:123}, name:'rama'}); var cc = CreditCard.save({id:{key:123}, name:'misko'}, callback); nakedExpect(cc).toEqual({id:{key:123}, name:'misko'}); @@ -148,13 +149,13 @@ describe("resource", function() { }); it('should post charge verb', function(){ - xhr.expectPOST('/CreditCard/123!charge?amount=10').data({auth:'abc'}).respond({success:'ok'}); + xhr.expectPOST('/CreditCard/123!charge?amount=10', {auth:'abc'}).respond({success:'ok'}); CreditCard.charge({id:123, amount:10},{auth:'abc'}, callback); }); it('should create on save', function(){ - xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123}); + xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123}); var cc = new CreditCard(); expect(cc.$get).not.toBeDefined(); expect(cc.$query).not.toBeDefined(); -- cgit v1.2.3 From 913729ee0120cc72e13b18d826c6da0fe2b98bf7 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 29 Apr 2010 10:55:22 -0700 Subject: fix isssue where the jasmine currentSpec does not get updated and hence everything runs as last spec context. --- test/ResourceSpec.js | 58 ---------------------------------------------------- 1 file changed, 58 deletions(-) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 08ca86b3..f2a0ff41 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -1,61 +1,3 @@ -function MockXHR(){ - this.expectations = { - 'GET': {}, - 'POST': {}, - 'DELETE': {} - }; - this.queue = []; -} -MockXHR.prototype = { - method: function(verb, url, data, callback) { - if (verb == 'POST') - url += '|' + angular.toJson(data); - var response = this.expectations[verb][url]; - if (!response) - throw "No expectation for " + verb + " on '" + url + "'."; - this.queue.push(function(){ - callback(response); - }); - }, - - expectGET: function(url) { - var self = this; - return { - respond: function(response){ - self.expectations.GET[url] = response; - } - }; - }, - - expectDELETE: function(url) { - var self = this; - return { - respond: function(response){ - self.expectations.DELETE[url] = response; - } - }; - }, - - expectPOST: function(url) { - var self = this; - return { - data: function(data){ - return { - respond: function(response){ - self.expectations.POST[url + '|' + angular.toJson(data)] = response; - } - }; - } - }; - }, - - flush: function(){ - while(this.queue.length) { - this.queue.shift()(); - } - } -}; - describe("resource", function() { var xhr, resource, CreditCard, callback; -- cgit v1.2.3 From c7913a4b7a3f5ffb0ea6bb1e636ac9d4a0e75c32 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 29 Apr 2010 17:28:33 -0700 Subject: added $xhr service with bulk and cache, hooked up $resource --- test/ResourceSpec.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index f2a0ff41..f0bb6770 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -120,4 +120,13 @@ describe("resource", function() { nakedExpect(visa).toEqual({id:123}); }); + it('should excersize full stack', function(){ + var scope = angular.compile('
'); + 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'); + }); + }); -- cgit v1.2.3 From 038a743e6f49c347a38edc0e54dcbb175905a475 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 7 May 2010 12:09:14 -0700 Subject: xhr bulk fixes --- test/ResourceSpec.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index f0bb6770..d2d52d47 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -81,6 +81,15 @@ describe("resource", function() { expect(callback).wasCalledWith(ccs); }); + 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;'); + }); + it('should delete resource', function(){ xhr.expectDELETE("/CreditCard/123").respond({}); -- cgit v1.2.3 From 0f73084e9d21cea99f0535e6ca30a1341b7047dc Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 19 May 2010 11:51:17 -0700 Subject: added error handler to xhr requests --- test/ResourceSpec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test/ResourceSpec.js') diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index d2d52d47..2f285bcf 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -138,4 +138,19 @@ describe("resource", function() { expect(person.name).toEqual('misko'); }); + 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'); + } + }); + }); + }); -- cgit v1.2.3