forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($cookies): move logic into $cookies and deprecate $cookieStore
The new API on `$cookies` includes: * `get` * `put` * `getObject` * `putObject` * `getAll` * `remove` The new API no longer polls the browser for changes to the cookies and no longer copy cookie values onto the `$cookies` object. The polling is expensive and caused issues with the `$cookies` properties not synchronizing correctly with the actual browser cookie values. The reason the polling was originally added was to allow communication between different tabs, but there are better ways to do this today (for example `localStorage`). DEPRECATION NOTICE: `$cookieStore` is now deprecated as all the useful logic has been moved to `$cookies`, to which `$cookieStore` now simply delegates calls. BREAKING CHANGE: `$cookies` no longer exposes properties that represent the current browser cookie values. Now you must explicitly the methods described above to access the cookie values. This also means that you can no longer watch the `$cookies` properties for changes to the browser's cookies. This feature is generally only needed if a 3rd party library was programmatically changing the cookies at runtime. If you rely on this then you must either write code that can react to the 3rd party library making the changes to cookies or implement your own polling mechanism. Closes angular#6411 Closes angular#7631
- Loading branch information
Showing
4 changed files
with
145 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,27 @@ | ||
'use strict'; | ||
|
||
describe('$cookieStore', function() { | ||
var mockedCookies; | ||
|
||
beforeEach(function() { | ||
var lastCookies = {}; | ||
mockedCookies = {}; | ||
module('ngCookies', { | ||
$$cookieWriter: function(name, value) { | ||
mockedCookies[name] = value; | ||
}, | ||
$$cookieReader: function() { | ||
if (!angular.equals(lastCookies, mockedCookies)) { | ||
lastCookies = angular.copy(mockedCookies); | ||
mockedCookies = angular.copy(mockedCookies); | ||
} | ||
return mockedCookies; | ||
} | ||
}); | ||
}); | ||
|
||
it('should serialize objects to json', inject(function($cookieStore, $$cookieReader, $rootScope) { | ||
$cookieStore.put('objectCookie', {id: 123, name: 'blah'}); | ||
$rootScope.$digest(); | ||
expect($$cookieReader()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'}); | ||
|
||
beforeEach(module('ngCookies', { | ||
$cookies: jasmine.createSpyObj('$cookies', ['getObject', 'putObject', 'remove']) | ||
})); | ||
|
||
|
||
it('should deserialize json to object', inject(function($cookieStore, $browser, $$cookieWriter) { | ||
$$cookieWriter('objectCookie', '{"id":123,"name":"blah"}'); | ||
$browser.poll(); | ||
expect($cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'}); | ||
it('should get cookie', inject(function($cookieStore, $cookies) { | ||
$cookies.getObject.andReturn('value'); | ||
expect($cookieStore.get('name')).toBe('value'); | ||
expect($cookies.getObject).toHaveBeenCalledWith('name'); | ||
})); | ||
|
||
|
||
it('should delete objects from the store when remove is called', inject(function($cookieStore, $browser, $rootScope, $$cookieReader) { | ||
$cookieStore.put('gonner', { "I'll":"Be Back"}); | ||
$rootScope.$digest(); //force eval in test | ||
$browser.poll(); | ||
expect($$cookieReader()).toEqual({'gonner': '{"I\'ll":"Be Back"}'}); | ||
|
||
$cookieStore.remove('gonner'); | ||
$rootScope.$digest(); | ||
expect($$cookieReader()).toEqual({}); | ||
it('should put cookie', inject(function($cookieStore, $cookies) { | ||
$cookieStore.put('name', 'value'); | ||
expect($cookies.putObject).toHaveBeenCalledWith('name', 'value'); | ||
})); | ||
it('should handle empty string value cookies', inject(function($cookieStore, $browser, $rootScope, $$cookieReader) { | ||
$cookieStore.put("emptyCookie",''); | ||
$rootScope.$digest(); | ||
expect($$cookieReader()). | ||
toEqual({ 'emptyCookie': '""' }); | ||
expect($cookieStore.get("emptyCookie")).toEqual(''); | ||
|
||
mockedCookies['blankCookie'] = ''; | ||
$browser.poll(); | ||
expect($cookieStore.get("blankCookie")).toEqual(''); | ||
|
||
|
||
it('should remove cookie', inject(function($cookieStore, $cookies) { | ||
$cookieStore.remove('name'); | ||
expect($cookies.remove).toHaveBeenCalledWith('name'); | ||
})); | ||
}); | ||
}); |
Oops, something went wrong.