forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookiesSpec.js
139 lines (105 loc) · 4.48 KB
/
cookiesSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
describe('$cookies', function() {
beforeEach(module('ngCookies', function($provide) {
$provide.factory('$browser', function(){
return angular.extend(new angular.mock.$Browser(), {cookieHash: {preexisting:'oldCookie'}});
});
}));
it('should provide access to existing cookies via object properties and keep them in sync',
inject(function($cookies, $browser, $rootScope) {
expect($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($cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie'});
$browser.cookieHash['brandNew'] = 'cookie2';
$browser.poll();
expect($cookies).toEqual({'preexisting': 'oldCookie', 'brandNew':'cookie2'});
delete $browser.cookieHash['brandNew'];
$browser.poll();
expect($cookies).toEqual({'preexisting': 'oldCookie'});
}));
it('should create or update a cookie when a value is assigned to a property',
inject(function($cookies, $browser, $rootScope) {
$cookies.oatmealCookie = 'nom nom';
$rootScope.$digest();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
$cookies.oatmealCookie = 'gone';
$rootScope.$digest();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie': 'gone'});
}));
it('should drop or reset any cookie that was set to a non-string value',
inject(function($cookies, $browser, $rootScope) {
$cookies.nonString = [1, 2, 3];
$cookies.nullVal = null;
$cookies.undefVal = undefined;
$cookies.preexisting = function() {};
$rootScope.$digest();
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
expect($cookies).toEqual({'preexisting': 'oldCookie'});
}));
it('should remove a cookie when a $cookies property is deleted',
inject(function($cookies, $browser, $rootScope) {
$cookies.oatmealCookie = 'nom nom';
$rootScope.$digest();
$browser.poll();
expect($browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
delete $cookies.oatmealCookie;
$rootScope.$digest();
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
}));
it('should drop or reset cookies that browser refused to store',
inject(function($cookies, $browser, $rootScope) {
var i, longVal;
for (i=0; i<5000; i++) {
longVal += '*';
}
//drop if no previous value
$cookies.longCookie = longVal;
$rootScope.$digest();
expect($cookies).toEqual({'preexisting': 'oldCookie'});
//reset if previous value existed
$cookies.longCookie = 'shortVal';
$rootScope.$digest();
expect($cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
$cookies.longCookie = longVal;
$rootScope.$digest();
expect($cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
}));
});
describe('$cookieStore', function() {
beforeEach(module('ngCookies'));
it('should serialize objects to json', inject(function($cookieStore, $browser, $rootScope) {
$cookieStore.put('objectCookie', {id: 123, name: 'blah'});
$rootScope.$digest();
expect($browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
}));
it('should deserialize json to object', inject(function($cookieStore, $browser) {
$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
$browser.poll();
expect($cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
}));
it('should delete objects from the store when remove is called', inject(function($cookieStore, $browser, $rootScope) {
$cookieStore.put('gonner', { "I'll":"Be Back"});
$rootScope.$digest(); //force eval in test
$browser.poll();
expect($browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
$cookieStore.remove('gonner');
$rootScope.$digest();
expect($browser.cookies()).toEqual({});
}));
it('should handle empty string value cookies', inject(function ($cookieStore, $browser, $rootScope) {
$cookieStore.put("emptyCookie",'');
$rootScope.$digest();
expect($browser.cookies()).
toEqual({ 'emptyCookie': '""' });
expect($cookieStore.get("emptyCookie")).toEqual('');
$browser.cookieHash['blankCookie'] = '';
$browser.poll();
expect($cookieStore.get("blankCookie")).toEqual('');
}))
});