diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/BrowserSpecs.js | 39 | ||||
| -rw-r--r-- | test/ResourceSpec.js | 2 | ||||
| -rw-r--r-- | test/angular-mocks.js | 10 | ||||
| -rw-r--r-- | test/servicesSpec.js | 67 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 11 |
5 files changed, 122 insertions, 7 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js index 5f28b610..eb43e3c5 100644 --- a/test/BrowserSpecs.js +++ b/test/BrowserSpecs.js @@ -1,8 +1,21 @@ describe('browser', function(){ - var browser, location, head, xhr; + var browser, location, head, xhr, setTimeoutQueue; + + function fakeSetTimeout(fn) { + setTimeoutQueue.push(fn); + } + + fakeSetTimeout.flush = function() { + foreach(setTimeoutQueue, function(fn) { + fn(); + }); + }; + beforeEach(function(){ + setTimeoutQueue = []; + location = {href:"http://server", hash:""}; head = { scripts: [], @@ -14,7 +27,7 @@ describe('browser', function(){ this.open = noop; this.setRequestHeader = noop; this.send = noop; - }); + }, undefined, fakeSetTimeout); }); it('should contain cookie cruncher', function() { @@ -59,6 +72,28 @@ describe('browser', function(){ }); + describe('defer', function() { + it('should execute fn asynchroniously via setTimeout', function() { + var counter = 0; + browser.defer(function() {counter++;}); + expect(counter).toBe(0); + + fakeSetTimeout.flush(); + expect(counter).toBe(1); + }); + + + it('should update outstandingRequests counter', function() { + var callback = jasmine.createSpy('callback'); + browser.defer(callback); + expect(callback).not.wasCalled(); + + fakeSetTimeout.flush(); + expect(callback).wasCalled(); + }); + }); + + describe('cookies', function() { function deleteAllCookies() { diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index a026263b..6b987bd8 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -184,6 +184,8 @@ describe("resource", function() { $browser.xhr.expectGET('/Person/123').respond('[\n{\nname:\n"rob"\n}\n]'); var person2 = Person.query({id:123}); + $browser.defer.flush(); + expect(person2[0].name).toEqual('misko'); var person2Cache = person2; $browser.xhr.flush(); diff --git a/test/angular-mocks.js b/test/angular-mocks.js index 0f637bf7..fd53a189 100644 --- a/test/angular-mocks.js +++ b/test/angular-mocks.js @@ -113,6 +113,15 @@ function MockBrowser() { self.cookieHash = {}; self.lastCookieHash = {}; + self.deferredFns = []; + + self.defer = function(fn) { + self.deferredFns.push(fn); + }; + + self.defer.flush = function() { + while (self.deferredFns.length) self.deferredFns.shift()(); + }; } MockBrowser.prototype = { @@ -156,7 +165,6 @@ MockBrowser.prototype = { return this.cookieHash; } } - }; angular.service('$browser', function(){ 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 + }); }); }); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index cb3b76a1..ceec2c90 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -530,6 +530,7 @@ describe("widget", function(){ scope.url = 'myUrl'; scope.$inject('$xhr.cache').data.myUrl = {value:'{{name}}'}; scope.$init(); + scope.$inject('$browser').defer.flush(); expect(element.text()).toEqual('misko'); dealoc(scope); }); @@ -542,6 +543,7 @@ describe("widget", function(){ scope.url = 'myUrl'; scope.$inject('$xhr.cache').data.myUrl = {value:'{{name}}'}; scope.$init(); + scope.$inject('$browser').defer.flush(); expect(element.text()).toEqual('igor'); @@ -558,9 +560,11 @@ describe("widget", function(){ scope.url = 'myUrl'; scope.$inject('$xhr.cache').data.myUrl = {value:'{{c=c+1}}'}; scope.$init(); - // This should not be 4, but to fix this properly - // we need to have real events on the scopes. - expect(element.text()).toEqual('4'); + scope.$inject('$browser').defer.flush(); + + // this one should really be just '1', but due to lack of real events things are not working + // properly. see discussion at: http://is.gd/ighKk + expect(element.text()).toEqual('2'); dealoc(scope); }); @@ -573,6 +577,7 @@ describe("widget", function(){ scope.url = 'myUrl'; scope.$inject('$xhr.cache').data.myUrl = {value:'my partial'}; scope.$init(); + scope.$inject('$browser').defer.flush(); expect(element.text()).toEqual('my partial'); expect(scope.loaded).toBe(true); dealoc(scope); |
