diff options
| author | Igor Minar | 2010-12-04 23:49:26 -0800 |
|---|---|---|
| committer | Igor Minar | 2010-12-06 16:45:59 -0800 |
| commit | 011fa39c2a0b5da843395b538fc4e52e5ade8287 (patch) | |
| tree | b5cc7ee72fb2fbcc76da2588822a21c2cedb614c /test/servicesSpec.js | |
| parent | 58d0e8945d772eddbfecbe6a645b2f1c4dd38bf2 (diff) | |
| download | angular.js-011fa39c2a0b5da843395b538fc4e52e5ade8287.tar.bz2 | |
add $browser.defer and $defer service and fix async xhr cache issue
- Closes #152 ($resource().query() sometimes calls callback before
returning, and it shouldn't)
- add $browser.defer method
- add $defer service
- integrate $browser.defer with outstandingRequests counter in $browser
- fix all old tests that relied on buggy behavior
Diffstat (limited to 'test/servicesSpec.js')
| -rw-r--r-- | test/servicesSpec.js | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/test/servicesSpec.js b/test/servicesSpec.js index ff90e0a1..5a9a4702 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -329,6 +329,47 @@ describe("service", function(){ }); }); + + describe('$defer', function() { + var $defer, $exceptionHandler; + + beforeEach(function(){ + scope = createScope({}, angularService, { + '$exceptionHandler': jasmine.createSpy('$exceptionHandler') + }); + + $browser = scope.$inject('$browser'); + $defer = scope.$inject('$defer'); + $exceptionHandler = scope.$inject('$exceptionHandler'); + }); + + + it('should delegate functions to $browser.defer', function() { + var counter = 0; + $defer(function() { counter++; }); + + expect(counter).toBe(0); + + $browser.defer.flush(); + expect(counter).toBe(1); + + $browser.defer.flush(); //does nothing + expect(counter).toBe(1); + + expect($exceptionHandler).not.toHaveBeenCalled(); + }); + + + it('should delegate exception to the $exceptionHandler service', function() { + $defer(function() { throw "Test Error"; }); + expect($exceptionHandler).not.toHaveBeenCalled(); + + $browser.defer.flush(); + expect($exceptionHandler).toHaveBeenCalledWith("Test Error"); + }); + }); + + describe('$xhr', function(){ var log; function callback(code, response) { @@ -426,12 +467,15 @@ describe("service", function(){ $browserXhr.expectGET('/url').respond('first'); cache('GET', '/url', null, callback); $browserXhr.flush(); + $browserXhr.expectGET('/url').respond('ERROR'); cache('GET', '/url', null, callback); + $browser.defer.flush(); $browserXhr.flush(); expect(log).toEqual('"first";"first";'); + cache('GET', '/url', null, callback, false); - $browserXhr.flush(); + $browser.defer.flush(); expect(log).toEqual('"first";"first";"first";'); }); @@ -439,9 +483,12 @@ describe("service", function(){ $browserXhr.expectGET('/url').respond('first'); cache('GET', '/url', null, callback, true); $browserXhr.flush(); + $browserXhr.expectGET('/url').respond('ERROR'); cache('GET', '/url', null, callback, true); + $browser.defer.flush(); expect(log).toEqual('"first";"first";'); + $browserXhr.flush(); expect(log).toEqual('"first";"first";"ERROR";'); }); @@ -449,8 +496,11 @@ describe("service", function(){ it('should serve requests from cache', function(){ cache.data.url = {value:'123'}; cache('GET', 'url', null, callback); + $browser.defer.flush(); expect(log).toEqual('"123";'); + cache('GET', 'url', null, callback, false); + $browser.defer.flush(); expect(log).toEqual('"123";"123";'); }); @@ -478,6 +528,21 @@ describe("service", function(){ cache('POST', 'abc', {}); expect(cache.data.url).toBeUndefined(); }); + + it('should call callback asynchronously for both cache hit and cache miss', function() { + $browserXhr.expectGET('/url').respond('+'); + cache('GET', '/url', null, callback); + expect(log).toEqual(''); //callback hasn't executed + + $browserXhr.flush(); + expect(log).toEqual('"+";'); //callback has executed + + cache('GET', '/url', null, callback); + expect(log).toEqual('"+";'); //callback hasn't executed + + $browser.defer.flush(); + expect(log).toEqual('"+";"+";'); //callback has executed + }); }); }); |
