diff options
| author | Igor Minar | 2010-09-25 15:29:48 -0700 |
|---|---|---|
| committer | Igor Minar | 2010-09-26 23:54:31 -0700 |
| commit | 3eec8c1a517f8b93a5afd15b7f83b33c5df7e54b (patch) | |
| tree | 9d9574d2245257456966bb0d15853f8f0d620958 /test | |
| parent | 9171a2b2b50d0a8217c98e0017a7d2a0a1a37380 (diff) | |
| download | angular.js-3eec8c1a517f8b93a5afd15b7f83b33c5df7e54b.tar.bz2 | |
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
Diffstat (limited to 'test')
| -rw-r--r-- | test/BrowserSpecs.js | 28 | ||||
| -rw-r--r-- | test/angular-mocks.js | 1 | ||||
| -rw-r--r-- | test/servicesSpec.js | 42 |
3 files changed, 46 insertions, 25 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js index 148fbca4..1a4e8585 100644 --- a/test/BrowserSpecs.js +++ b/test/BrowserSpecs.js @@ -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); + }); }); }); diff --git a/test/angular-mocks.js b/test/angular-mocks.js index 5b5c9863..b02fabaf 100644 --- a/test/angular-mocks.js +++ b/test/angular-mocks.js @@ -84,6 +84,7 @@ MockBrowser.prototype = { addPollFn: function(pollFn) { this.pollFns.push(pollFn); + return pollFn; }, hover: function(onHover) { diff --git a/test/servicesSpec.js b/test/servicesSpec.js index 3416f0ea..258af46b 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -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'}); }); }); |
