From 011fa39c2a0b5da843395b538fc4e52e5ade8287 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Sat, 4 Dec 2010 23:49:26 -0800 Subject: 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 --- test/BrowserSpecs.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'test/BrowserSpecs.js') 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() { -- cgit v1.2.3