From 7974e7eb5f3821b77691819683f9aa37f3c37473 Mon Sep 17 00:00:00 2001 From: DiPeng Date: Fri, 24 Jun 2011 12:26:44 -0700 Subject: refactor($browser): hide startPoll and poll methods Breaks $browser.poll() method is moved inline to $browser.startpoll() Breaks $browser.startpoll() method is made private Refactor tests to reflect updated browser API Closes #387 --- src/Browser.js | 35 +++++++++++++++++------------------ src/angular-mocks.js | 7 +++++++ src/scenario/Application.js | 1 - 3 files changed, 24 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Browser.js b/src/Browser.js index 4340ac4e..61396bd1 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -10,8 +10,9 @@ var XHR = window.XMLHttpRequest || function () { /** - * @private - * @name Browser + * @ngdoc service + * @name angular.service.$browser + * @requires $log * * @description * Constructor for the object exposed as $browser service. @@ -21,6 +22,11 @@ var XHR = window.XMLHttpRequest || function () { * - hide all the global state in the browser caused by the window object * - abstract away all the browser specific features and inconsistencies * + * For tests we provide {@link angular.mock.service.$browser mock implementation} of the `$browser` + * service, which can be used for convenient testing of the application without the interaction with + * the real browser apis. + */ +/** * @param {object} window The global window object. * @param {object} document jQuery wrapped document. * @param {object} body jQuery wrapped document.body. @@ -121,6 +127,11 @@ function Browser(window, document, body, XHR, $log) { * @param {function()} callback Function that will be called when no outstanding request */ self.notifyWhenNoOutstandingRequests = function(callback) { + // force browser to execute all pollFns - this is needed so that cookies and other pollers fire + // at some deterministic time in respect to the test runner's actions. Leaving things up to the + // regular poller would result in flaky tests. + forEach(pollFns, function(pollFn){ pollFn(); }); + if (outstandingRequestCount === 0) { callback(); } else { @@ -134,16 +145,6 @@ function Browser(window, document, body, XHR, $log) { var pollFns = [], pollTimeout; - /** - * @workInProgress - * @ngdoc method - * @name angular.service.$browser#poll - * @methodOf angular.service.$browser - */ - self.poll = function() { - forEach(pollFns, function(pollFn){ pollFn(); }); - }; - /** * @workInProgress * @ngdoc method @@ -159,14 +160,12 @@ function Browser(window, document, body, XHR, $log) { * @returns {function()} the added function */ self.addPollFn = function(fn) { - if (!pollTimeout) self.startPoller(100, setTimeout); + if (!pollTimeout) startPoller(100, setTimeout); pollFns.push(fn); return fn; }; /** - * @workInProgress - * @ngdoc method * @name angular.service.$browser#startPoller * @methodOf angular.service.$browser * @@ -177,9 +176,9 @@ function Browser(window, document, body, XHR, $log) { * Configures the poller to run in the specified intervals, using the specified * setTimeout fn and kicks it off. */ - self.startPoller = function(interval, setTimeout) { - (function check(){ - self.poll(); + function startPoller(interval, setTimeout) { + (function check() { + forEach(pollFns, function(pollFn){ pollFn(); }); pollTimeout = setTimeout(check, interval); })(); }; diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 3de04772..9154f8b5 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -304,6 +304,13 @@ function MockBrowser() { } MockBrowser.prototype = { +/** + * @name angular.mock.service.$browser#poll + * @methodOf angular.mock.service.$browser + * + * @description + * run all fns in pollFns + */ poll: function poll(){ angular.forEach(this.pollFns, function(pollFn){ pollFn(); diff --git a/src/scenario/Application.js b/src/scenario/Application.js index 28561ed3..1c87c5a0 100644 --- a/src/scenario/Application.js +++ b/src/scenario/Application.js @@ -89,7 +89,6 @@ angular.scenario.Application.prototype.executeAction = function(action) { return action.call(this, $window, _jQuery($window.document)); } var $browser = $window.angular.service.$browser(); - $browser.poll(); $browser.notifyWhenNoOutstandingRequests(function() { action.call(self, $window, _jQuery($window.document)); }); -- cgit v1.2.3