angular.js/test/service/cookiesSpec.js
Misko Hevery 8f0dcbab80 feat(scope): new and improved scope implementation
- Speed improvements (about 4x on flush phase)
- Memory improvements (uses no function closures)
- Break $eval into $apply, $dispatch, $flush
- Introduced $watch and $observe

Breaks angular.equals() use === instead of ==
Breaks angular.scope() does not take parent as first argument
Breaks scope.$watch() takes scope as first argument
Breaks scope.$set(), scope.$get are removed
Breaks scope.$config is removed
Breaks $route.onChange callback has not "this" bounded
2011-08-02 01:00:03 +02:00

100 lines
3 KiB
JavaScript

'use strict';
describe('$cookies', function() {
var scope, $browser;
beforeEach(function() {
$browser = new MockBrowser();
$browser.cookieHash['preexisting'] = 'oldCookie';
scope = angular.scope(angular.service, {$browser: $browser});
scope.$cookies = scope.$service('$cookies');
});
afterEach(function(){
dealoc(scope);
});
it('should provide access to existing cookies via object properties and keep them in sync',
function(){
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
// access internal cookie storage of the browser mock directly to simulate behavior of
// document.cookie
$browser.cookieHash['brandNew'] = 'cookie';
$browser.poll();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'});
$browser.cookieHash['brandNew'] = 'cookie2';
$browser.poll();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'});
delete $browser.cookieHash['brandNew'];
$browser.poll();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
});
it('should create or update a cookie when a value is assigned to a property', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$flush();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
scope.$cookies.oatmealCookie = 'gone';
scope.$flush();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie': 'gone'});
});
it('should drop or reset any cookie that was set to a non-string value', function() {
scope.$cookies.nonString = [1, 2, 3];
scope.$cookies.nullVal = null;
scope.$cookies.undefVal = undefined;
scope.$cookies.preexisting = function(){};
scope.$flush();
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
});
it('should remove a cookie when a $cookies property is deleted', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$flush();
$browser.poll();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
delete scope.$cookies.oatmealCookie;
scope.$flush();
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
});
it('should drop or reset cookies that browser refused to store', function() {
var i, longVal;
for (i=0; i<5000; i++) {
longVal += '*';
}
//drop if no previous value
scope.$cookies.longCookie = longVal;
scope.$flush();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
//reset if previous value existed
scope.$cookies.longCookie = 'shortVal';
scope.$flush();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
scope.$cookies.longCookie = longVal;
scope.$flush();
expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
});
});