diff options
| author | Misko Hevery | 2010-04-29 17:28:33 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-04-29 17:28:33 -0700 |
| commit | c7913a4b7a3f5ffb0ea6bb1e636ac9d4a0e75c32 (patch) | |
| tree | 5266b2b7b3f2a3d1dba3a652427e5ce4744bd477 /test | |
| parent | 913729ee0120cc72e13b18d826c6da0fe2b98bf7 (diff) | |
| download | angular.js-c7913a4b7a3f5ffb0ea6bb1e636ac9d4a0e75c32.tar.bz2 | |
added $xhr service with bulk and cache, hooked up $resource
Diffstat (limited to 'test')
| -rw-r--r-- | test/ResourceSpec.js | 9 | ||||
| -rw-r--r-- | test/servicesSpec.js | 82 |
2 files changed, 91 insertions, 0 deletions
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('<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'); + }); + }); diff --git a/test/servicesSpec.js b/test/servicesSpec.js index a7391f34..f15d6ad7 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -159,6 +159,88 @@ describe("service", function(){ }); }); + describe('$xhr', function(){ + var log, xhr; + function callback(code, response) { + expect(code).toEqual(200); + log = log + toJson(response) + ';'; + }; + + beforeEach(function(){ + log = ''; + xhr = scope.$browser.xhr; + }); + + it('should forward the request to $browser and decode JSON', function(){ + xhr.expectGET('/reqGET').respond('first'); + xhr.expectGET('/reqGETjson').respond('["second"]'); + xhr.expectPOST('/reqPOST', {post:'data'}).respond('third'); + + scope.$xhr('GET', '/reqGET', null, callback); + scope.$xhr('GET', '/reqGETjson', null, callback); + scope.$xhr('POST', '/reqPOST', {post:'data'}, callback); + + xhr.flush(); + + expect(log).toEqual('"third";["second"];"first";'); + }); + + describe('bulk', function(){ + it('should collect requests', function(){ + scope.$xhr.bulk.url = "/"; + scope.$xhr.bulk('GET', '/req1', null, callback); + scope.$xhr.bulk('POST', '/req2', {post:'data'}, callback); + + xhr.expectPOST('/', { + requests:[{method:'GET', url:'/req1', data: null}, + {method:'POST', url:'/req2', data:{post:'data'} }] + }).respond([ + {status:200, response:'first'}, + {status:200, response:'second'} + ]); + scope.$xhr.bulk.flush(function(){ log += 'DONE';}); + xhr.flush(); + expect(log).toEqual('"first";"second";DONE'); + }); + }); + + describe('cache', function(){ + var cache; + beforeEach(function(){ cache = scope.$xhr.cache; }); + it('should cache requests', function(){ + xhr.expectGET('/url').respond('first'); + cache('GET', '/url', null, callback); + xhr.flush(); + xhr.expectGET('/url').respond('ERROR'); + cache('GET', '/url', null, callback); + xhr.flush(); + expect(log).toEqual('"first";"first";'); + }); + + it('should serve requests from cache', function(){ + cache.data.url = {value:'123'}; + cache('GET', 'url', null, callback); + expect(log).toEqual('"123";'); + }); + + it('should keep track of in flight requests and request only once', function(){ + cache.delegate = scope.$xhr.bulk; + xhr.expectPOST('/bulk', { + requests:[{method:'GET', url:'/url', data: null}] + }).respond([ + {status:200, response:'123'}, + ]); + cache('GET', '/url', null, callback); + cache('GET', '/url', null, callback); + cache.delegate.flush(); + xhr.flush(); + expect(log).toEqual('"123";"123";'); + }); + }); + + }); + + }); |
