Properly initialize cookie service in order to preserve existing cookies

- previously the poller initialized the cookie cache too late which
  was causing previously existing cookies to be deleted by cookie service
- refactored the poller api so that the addPollFn returns the added fn
- fixed older cookie service tests
- removed "this.$onEval(PRIORITY_LAST, update);" because it is not needed
This commit is contained in:
Igor Minar 2010-09-25 15:29:48 -07:00
parent 9171a2b2b5
commit 3eec8c1a51
5 changed files with 65 additions and 30 deletions

View file

@ -79,8 +79,21 @@ function Browser(location, document, head, XHR, $log) {
foreach(pollFns, function(pollFn){ pollFn(); });
}
self.poll = poll;
self.addPollFn = bind(pollFns, push);
self.startPoller = function(interval, setTimeout){
/**
* Adds a function to the list of functions that poller periodically executes
* @return {Function} the added function
*/
self.addPollFn = function(/**Function*/fn){
pollFns.push(fn);
return fn;
};
/**
* Configures the poller to run in the specified intervals, using the specified setTimeout fn and
* kicks it off.
*/
self.startPoller = function(/**number*/interval, /**Function*/setTimeout){
(function check(){
poll();
setTimeout(check, interval);

View file

@ -409,16 +409,17 @@ angularService('$cookies', function($browser) {
rootScope = this,
lastCookies;
$browser.addPollFn(function(){
//creates a poller fn that copies all cookies from the $browser to service & inits the service
$browser.addPollFn(function() {
var currentCookies = $browser.cookies();
if (lastCookies != currentCookies) {
lastCookies = currentCookies;
copy(currentCookies, cookies);
rootScope.$eval();
}
});
})();
this.$onEval(PRIORITY_FIRST, update);
//at the end of each eval, push cookies
this.$onEval(PRIORITY_LAST, update);
return cookies;

View file

@ -76,11 +76,11 @@ describe('browser', function(){
beforeEach(function() {
deleteAllCookies();
logs = {log:[], warn:[], info:[], error:[]}
log = {log: function() {logs.log.push(Array.prototype.slice.call(arguments))},
warn: function() {logs.warn.push(Array.prototype.slice.call(arguments))},
info: function() {logs.info.push(Array.prototype.slice.call(arguments))},
error: function() {logs.error.push(Array.prototype.slice.call(arguments))}};
logs = {log:[], warn:[], info:[], error:[]};
log = {log: function() { logs.log.push(slice.call(arguments)); },
warn: function() { logs.warn.push(slice.call(arguments)); },
info: function() { logs.info.push(slice.call(arguments)); },
error: function() { logs.error.push(slice.call(arguments)); }};
browser = new Browser({}, jqLite(document), undefined, XHR, log);
expect(document.cookie).toEqual('');
});
@ -102,7 +102,7 @@ describe('browser', function(){
});
describe('remove via (cookieName, undefined)', function() {
describe('remove via cookies(cookieName, undefined)', function() {
it('should remove a cookie when it is present', function() {
document.cookie = 'foo=bar';
@ -122,7 +122,7 @@ describe('browser', function(){
});
describe('put via (cookieName, string)', function() {
describe('put via cookies(cookieName, string)', function() {
it('should create and store a cookie', function() {
browser.cookies('cookieName', 'cookieValue');
@ -194,10 +194,10 @@ describe('browser', function(){
});
describe('get via (cookieName)', function() {
describe('get via cookies()[cookieName]', function() {
it('should return undefined for nonexistent cookie', function() {
expect(browser.cookies('nonexistent')).not.toBeDefined();
expect(browser.cookies().nonexistent).not.toBeDefined();
});
@ -221,7 +221,7 @@ describe('browser', function(){
});
describe('getAll', function() {
describe('getAll via cookies()', function() {
it('should return cookies as hash', function() {
document.cookie = "foo1=bar1";
@ -252,7 +252,7 @@ describe('browser', function(){
});
describe('poll', function(){
describe('poller', function(){
it('should call all fns on poll', function(){
var log = '';
browser.addPollFn(function(){log+='a';});
@ -274,5 +274,11 @@ describe('browser', function(){
setTimeoutSpy.mostRecentCall.args[0]();
expect(log).toEqual('..');
});
it('should return fn that was passed into addPollFn', function() {
var fn = function() { return 1; };
var returnedFn = browser.addPollFn(fn);
expect(returnedFn).toBe(fn);
});
});
});

View file

@ -84,6 +84,7 @@ MockBrowser.prototype = {
addPollFn: function(pollFn) {
this.pollFns.push(pollFn);
return pollFn;
},
hover: function(onHover) {

View file

@ -373,22 +373,33 @@ describe("service", function(){
describe('$cookies', function() {
var scope;
beforeEach(function() {
var browser = new MockBrowser();
browser.cookieHash['preexisting'] = 'oldCookie';
scope = createScope(null, angularService, {$browser: browser});
});
it('should provide access to existing cookies via object properties and keep them in sync',
function(){
expect(scope.$cookies).toEqual({});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
scope.$browser.cookies('brandNew', 'cookie');
// access internal cookie storage of the browser mock directly to simulate behavior of
// document.cookie
scope.$browser.cookieHash['brandNew'] = 'cookie';
scope.$browser.poll();
expect(scope.$cookies).toEqual({'brandNew':'cookie'});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'});
scope.$browser.cookies('brandNew', 'cookie2');
scope.$browser.cookieHash['brandNew'] = 'cookie2';
scope.$browser.poll();
expect(scope.$cookies).toEqual({'brandNew':'cookie2'});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'});
scope.$browser.cookies('brandNew', undefined);
delete scope.$browser.cookieHash['brandNew'];
scope.$browser.poll();
expect(scope.$cookies).toEqual({});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
});
@ -396,20 +407,22 @@ describe("service", function(){
scope.$cookies.oatmealCookie = 'nom nom';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'nom nom'});
expect(scope.$browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
scope.$cookies.oatmealCookie = 'gone';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'gone'});
expect(scope.$browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie': 'gone'});
});
it('should ignore non-string values when asked to create a cookie', function() {
scope.$cookies.nonString = [1, 2, 3];
scope.$eval();
expect(scope.$browser.cookies()).toEqual({});
expect(scope.$cookies).toEqual({});
expect(scope.$browser.cookies()).toEqual({'preexisting': 'oldCookie'});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
});
@ -418,19 +431,20 @@ describe("service", function(){
scope.$cookies.undefVal = undefined;
scope.$eval();
expect(scope.$browser.cookies()).toEqual({});
expect(scope.$browser.cookies()).toEqual({'preexisting': 'oldCookie'});
});
it('should remove a cookie when a $cookies property is deleted', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'nom nom'});
expect(scope.$browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
delete scope.$cookies.oatmealCookie;
scope.$eval();
expect(scope.$browser.cookies()).toEqual({});
expect(scope.$browser.cookies()).toEqual({'preexisting': 'oldCookie'});
});
});