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 +++++++------ test/angular-mocks.js | 20 +++++++++++++++++--- test/testabilityPatch.js | 9 +++++---- 3 files changed, 29 insertions(+), 13 deletions(-) (limited to 'test') 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(); diff --git a/test/angular-mocks.js b/test/angular-mocks.js index 3e272313..715b4d75 100644 --- a/test/angular-mocks.js +++ b/test/angular-mocks.js @@ -23,11 +23,19 @@ */ function MockBrowser() { - var self = this, expectations = {}, requests = []; + var self = this, + expectations = {}, + requests = []; self.url = "http://server"; self.watches = []; - self.xhr = function(method, url, callback) { + self.xhr = function(method, url, data, callback) { + if (isFunction(data)) { + callback = data; + data = null; + } + if (data && isObject(data)) data = angular.toJson(data); + if (data && isString(data)) url += "|" + data; var expect = expectations[method] || {}; var response = expect[url]; if (!response) { @@ -39,7 +47,9 @@ function MockBrowser() { }; self.xhr.expectations = expectations; self.xhr.requests = requests; - self.xhr.expect = function(method, url) { + self.xhr.expect = function(method, url, data) { + if (data && isObject(data)) data = angular.toJson(data); + if (data && isString(data)) url += "|" + data; var expect = expectations[method] || (expectations[method] = {}); return { respond: function(response) { @@ -47,6 +57,10 @@ function MockBrowser() { } }; }; + self.xhr.expectGET = angular.bind(self, self.xhr.expect, 'GET'); + self.xhr.expectPOST = angular.bind(self, self.xhr.expect, 'POST'); + self.xhr.expectDELETE = angular.bind(self, self.xhr.expect, 'DELETE'); + self.xhr.expectPUT = angular.bind(self, self.xhr.expect, 'PUT'); self.xhr.flush = function() { while(requests.length) { requests.pop()(); diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index ff537a09..572e6a36 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -10,20 +10,21 @@ function childNode(element, index) { } extend(angular, { - 'element': jqLite, + 'bind': bind, 'compile': compile, - 'scope': createScope, 'copy': copy, + 'element': jqLite, 'extend': extend, 'foreach': foreach, - 'noop':noop, 'identity':identity, 'isUndefined': isUndefined, 'isDefined': isDefined, 'isString': isString, 'isFunction': isFunction, 'isNumber': isNumber, - 'isArray': isArray + 'isArray': isArray, + 'noop':noop, + 'scope': createScope }); -- cgit v1.2.3