aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiPeng2011-06-24 12:26:44 -0700
committerIgor Minar2011-07-18 00:04:14 -0700
commit7974e7eb5f3821b77691819683f9aa37f3c37473 (patch)
tree72eea1f97eb3d7f7d79774a8ed831fdc60dc448b
parentf9b4c9da648f81aef1fbee41d24822e420eb56f9 (diff)
downloadangular.js-7974e7eb5f3821b77691819683f9aa37f3c37473.tar.bz2
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
-rw-r--r--src/Browser.js35
-rw-r--r--src/angular-mocks.js7
-rw-r--r--src/scenario/Application.js1
-rw-r--r--test/BrowserSpecs.js55
-rw-r--r--test/scenario/ApplicationSpec.js4
5 files changed, 43 insertions, 59 deletions
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 {
@@ -137,16 +148,6 @@ function Browser(window, document, body, XHR, $log) {
/**
* @workInProgress
* @ngdoc method
- * @name angular.service.$browser#poll
- * @methodOf angular.service.$browser
- */
- self.poll = function() {
- forEach(pollFns, function(pollFn){ pollFn(); });
- };
-
- /**
- * @workInProgress
- * @ngdoc method
* @name angular.service.$browser#addPollFn
* @methodOf angular.service.$browser
*
@@ -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));
});
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index d6eb5697..bf9cfd03 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -8,7 +8,9 @@ describe('browser', function(){
}
fakeSetTimeout.flush = function() {
- forEach(setTimeoutQueue, function(fn) {
+ var currentTimeoutQueue = setTimeoutQueue;
+ setTimeoutQueue = [];
+ forEach(currentTimeoutQueue, function(fn) {
fn();
});
};
@@ -364,31 +366,27 @@ describe('browser', function(){
});
describe('poller', function(){
- beforeEach(function() {
- spyOn(browser, 'startPoller');
- });
- it('should call all fns on poll', function(){
+ it('should call functions in pollFns in regular intervals', function(){
var log = '';
browser.addPollFn(function(){log+='a';});
browser.addPollFn(function(){log+='b';});
expect(log).toEqual('');
- browser.poll();
+ fakeSetTimeout.flush();
expect(log).toEqual('ab');
- browser.poll();
+ fakeSetTimeout.flush();
expect(log).toEqual('abab');
});
it('should startPoller', function(){
- var log = '';
- var setTimeoutSpy = jasmine.createSpy('setTimeout');
- browser.addPollFn(function(){log+='.';});
- browser.startPoller.andCallThrough();
- browser.startPoller(50, setTimeoutSpy);
- expect(log).toEqual('.');
- expect(setTimeoutSpy.mostRecentCall.args[1]).toEqual(50);
- setTimeoutSpy.mostRecentCall.args[0]();
- expect(log).toEqual('..');
+ expect(setTimeoutQueue.length).toEqual(0);
+
+ browser.addPollFn(function(){});
+ expect(setTimeoutQueue.length).toEqual(1);
+
+ //should remain 1 as it is the check fn
+ browser.addPollFn(function(){});
+ expect(setTimeoutQueue.length).toEqual(1);
});
it('should return fn that was passed into addPollFn', function() {
@@ -396,22 +394,6 @@ describe('browser', function(){
var returnedFn = browser.addPollFn(fn);
expect(returnedFn).toBe(fn);
});
-
- it('should auto start poller when first fn registered', function() {
- browser.addPollFn(function() {});
-
- expect(browser.startPoller).toHaveBeenCalled();
- expect(browser.startPoller.callCount).toBe(1);
- });
-
- it('should auto start poller only when first fn registered', function() {
- browser.startPoller.andCallThrough();
- browser.addPollFn(function() {});
- browser.addPollFn(function() {});
- browser.addPollFn(function() {});
-
- expect(browser.startPoller.callCount).toBe(1);
- });
});
@@ -421,7 +403,8 @@ describe('browser', function(){
fakeWindow = {
location: {href:"http://server"},
- document: {}
+ document: {},
+ setTimeout: fakeSetTimeout
};
browser = new Browser(fakeWindow, {}, {});
@@ -435,12 +418,12 @@ describe('browser', function(){
fakeWindow.location.href = "http://server/#newHash";
expect(events).toEqual([]);
- browser.poll();
+ fakeSetTimeout.flush();
expect(events).toEqual(['x']);
//don't do anything if url hasn't changed
events = [];
- browser.poll();
+ fakeSetTimeout.flush();
expect(events).toEqual([]);
});
@@ -493,7 +476,7 @@ describe('browser', function(){
browser.onHashChange(callback);
window.location.hash = 'new-hash';
- browser.startPoller(100, setTimeout);
+ browser.addPollFn(function() {});
waitsFor(function() {
return callback.callCount;
diff --git a/test/scenario/ApplicationSpec.js b/test/scenario/ApplicationSpec.js
index 1cab11ec..8c31726f 100644
--- a/test/scenario/ApplicationSpec.js
+++ b/test/scenario/ApplicationSpec.js
@@ -121,9 +121,6 @@ describe('angular.scenario.Application', function() {
};
testWindow.angular.service.$browser = function() {
return {
- poll: function() {
- polled = true;
- },
notifyWhenNoOutstandingRequests: function(fn) {
handlers.push(fn);
}
@@ -137,7 +134,6 @@ describe('angular.scenario.Application', function() {
expect($document).toBeDefined();
expect($document[0].className).toEqual('test-foo');
});
- expect(polled).toBeTruthy();
expect(handlers.length).toEqual(1);
handlers[0]();
});