-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.js
More file actions
281 lines (220 loc) · 9.09 KB
/
index.test.js
File metadata and controls
281 lines (220 loc) · 9.09 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { describe, it, expect, beforeEach, vi } from 'vitest';
import Cookie from './index.js';
// Helper to clear all cookies between tests
function clearAllCookies() {
document.cookie.split(';').forEach((c) => {
const key = c.trim().split('=')[0];
document.cookie = key + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
});
}
// Intercept document.cookie writes so we can inspect the raw cookie string.
function spyCookieSetter() {
const calls = [];
const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie');
Object.defineProperty(document, 'cookie', {
get: descriptor.get.bind(document),
set(value) {
calls.push(value);
descriptor.set.call(document, value);
},
configurable: true,
});
return {
calls,
restore() {
Object.defineProperty(document, 'cookie', descriptor);
},
};
}
describe('Cookie', () => {
let cookie;
beforeEach(() => {
cookie = new Cookie();
clearAllCookies();
});
// -------------------------------------------------------------------------
// constructor
// -------------------------------------------------------------------------
describe('constructor', () => {
it('defaults namespace to empty string', () => {
expect(cookie.namespace).toBe('');
});
it('stores the given namespace', () => {
const ns = new Cookie('app_');
expect(ns.namespace).toBe('app_');
});
});
// -------------------------------------------------------------------------
// set()
// -------------------------------------------------------------------------
describe('set()', () => {
it('sets a simple cookie', () => {
cookie.set('name', 'alice');
expect(document.cookie).toContain('name=alice');
});
it('encodes special characters in the value', () => {
cookie.set('greeting', 'hello world');
expect(cookie.get('greeting')).toBe('hello world');
});
it('accepts a numeric expiry (days in the future)', () => {
cookie.set('name', 'alice', 7);
expect(document.cookie).toContain('name=alice');
});
it('accepts a Date expiry', () => {
const future = new Date(Date.now() + 86_400_000);
cookie.set('name', 'alice', future);
expect(document.cookie).toContain('name=alice');
});
it('accepts a UTC string expiry', () => {
const future = new Date(Date.now() + 86_400_000).toUTCString();
cookie.set('name', 'alice', future);
expect(document.cookie).toContain('name=alice');
});
it('emits Secure without a value (not Secure=true)', () => {
const spy = spyCookieSetter();
cookie.set('name', 'alice');
expect(spy.calls[0]).toContain('; Secure');
expect(spy.calls[0]).not.toContain('Secure=true');
spy.restore();
});
it('omits Secure when overridden with false', () => {
const spy = spyCookieSetter();
cookie.set('name', 'alice', null, '/', { Secure: false });
expect(spy.calls[0]).not.toContain('Secure');
spy.restore();
});
it('respects a custom path', () => {
const spy = spyCookieSetter();
cookie.set('name', 'alice', null, '/admin');
expect(spy.calls[0]).toContain('path=/admin');
spy.restore();
});
it('spreads additional options into the cookie string', () => {
const spy = spyCookieSetter();
cookie.set('name', 'alice', null, '/', { SameSite: 'Strict' });
expect(spy.calls[0]).toContain('SameSite=Strict');
spy.restore();
});
});
// -------------------------------------------------------------------------
// get()
// -------------------------------------------------------------------------
describe('get()', () => {
it('retrieves the value of an existing cookie', () => {
cookie.set('color', 'blue');
expect(cookie.get('color')).toBe('blue');
});
it('returns the default value when the cookie does not exist', () => {
expect(cookie.get('missing', 'default')).toBe('default');
});
it('returns an empty string by default when the cookie does not exist', () => {
expect(cookie.get('missing')).toBe('');
});
it('decodes percent-encoded values', () => {
cookie.set('msg', 'héllo');
expect(cookie.get('msg')).toBe('héllo');
});
it('handles values containing an equals sign', () => {
cookie.set('token', 'a=b');
expect(cookie.get('token')).toBe('a=b');
});
});
// -------------------------------------------------------------------------
// isset()
// -------------------------------------------------------------------------
describe('isset()', () => {
it('returns true when the cookie exists', () => {
cookie.set('present', '1');
expect(cookie.isset('present')).toBe(true);
});
it('returns false when the cookie does not exist', () => {
expect(cookie.isset('absent')).toBe(false);
});
});
// -------------------------------------------------------------------------
// remove()
// -------------------------------------------------------------------------
describe('remove()', () => {
it('removes an existing cookie', () => {
cookie.set('temp', 'data');
expect(cookie.isset('temp')).toBe(true);
cookie.remove('temp');
expect(cookie.isset('temp')).toBe(false);
});
it('does not throw when removing a non-existent cookie', () => {
expect(() => cookie.remove('ghost')).not.toThrow();
});
});
// -------------------------------------------------------------------------
// namespace
// -------------------------------------------------------------------------
describe('namespace', () => {
it('prefixes the key with the namespace when setting', () => {
const ns = new Cookie('app_');
ns.set('user', 'bob');
expect(document.cookie).toContain('app_user=bob');
});
it('retrieves the namespaced cookie with the same instance', () => {
const ns = new Cookie('app_');
ns.set('user', 'bob');
expect(ns.get('user')).toBe('bob');
});
it('does not expose the cookie to a different namespace', () => {
const ns1 = new Cookie('a_');
const ns2 = new Cookie('b_');
ns1.set('key', 'one');
ns2.set('key', 'two');
expect(ns1.get('key')).toBe('one');
expect(ns2.get('key')).toBe('two');
});
it('isset() respects the namespace', () => {
const ns = new Cookie('ns_');
expect(ns.isset('x')).toBe(false);
ns.set('x', '1');
expect(ns.isset('x')).toBe(true);
expect(cookie.isset('x')).toBe(false);
});
it('remove() respects the namespace', () => {
const ns = new Cookie('ns_');
ns.set('y', '1');
ns.remove('y');
expect(ns.isset('y')).toBe(false);
});
});
// -------------------------------------------------------------------------
// _qualify()
// -------------------------------------------------------------------------
describe('_qualify()', () => {
it('returns the key unchanged when no namespace and no special chars', () => {
expect(cookie._qualify('simple')).toBe('simple');
});
it('prepends the namespace', () => {
const ns = new Cookie('ns_');
expect(ns._qualify('key')).toBe('ns_key');
});
it('escapes regex-special characters so they match literally', () => {
const qualified = cookie._qualify('foo.bar');
expect(qualified).toBe('foo\\.bar');
});
});
// -------------------------------------------------------------------------
// _getCookiePattern()
// -------------------------------------------------------------------------
describe('_getCookiePattern()', () => {
it('returns a RegExp', () => {
expect(cookie._getCookiePattern('key')).toBeInstanceOf(RegExp);
});
it('matches a cookie at the start of the cookie string', () => {
const pattern = cookie._getCookiePattern('key');
expect('key=value').toMatch(pattern);
});
it('matches a cookie that follows another cookie', () => {
const pattern = cookie._getCookiePattern('second');
expect('first=1; second=2').toMatch(pattern);
});
it('does not match a partial key name', () => {
const pattern = cookie._getCookiePattern('key');
expect('mykey=value').not.toMatch(pattern);
});
});
});