mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-30 04:50:28 +00:00
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
This commit is contained in:
parent
f9b4c9da64
commit
7974e7eb5f
5 changed files with 43 additions and 59 deletions
|
|
@ -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);
|
||||
})();
|
||||
};
|
||||
|
|
|
|||
7
src/angular-mocks.js
vendored
7
src/angular-mocks.js
vendored
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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]();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue