-
Notifications
You must be signed in to change notification settings - Fork 63
/
cookies.test.js
146 lines (133 loc) · 4.44 KB
/
cookies.test.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
140
141
142
143
144
145
146
import cookies from './cookies';
import options from './options';
import delay from 'delay';
describe('cookies', () => {
it('is defined', () => {
expect(cookies).toBeDefined();
});
it('can set, read and remove cookies', () => {
expect(cookies.name).toBe(null);
cookies.name = 'Francisco';
expect(cookies.name).toBe('Francisco');
delete cookies.name;
expect(cookies.name).toBe(null);
});
it('does work with the underlying engine', () => {
expect(document.cookie).toBe("");
cookies.name = 'Francisco';
const raw = decodeURIComponent(document.cookie);
expect(raw).toBe('name=' + JSON.stringify('Francisco'));
delete cookies.name;
expect(document.cookie).toBe("");
});
it('can list the cookies', () => {
cookies.firstname = 'Francisco';
cookies.lastname = 'Presencia';
expect(Object.keys(cookies)).toEqual(['firstname', 'lastname']);
expect(Object.values(cookies)).toEqual(['Francisco', 'Presencia']);
expect(Object.entries(cookies)).toEqual([['firstname', 'Francisco'], ['lastname', 'Presencia']]);
delete cookies.firstname;
delete cookies.lastname;
});
it('can iterate with "in"', () => {
cookies.firstname = 'Francisco';
cookies.lastname = 'Presencia';
const keys = [];
for (let key in cookies) {
keys.push(key);
}
expect(keys).toEqual(['firstname', 'lastname']);
delete cookies.firstname;
delete cookies.lastname;
});
it('throws for the iteration since it is not yet ready', () => {
cookies.firstname = 'Francisco';
cookies.lastname = 'Presencia';
const values = [];
for (let val of cookies) {
values.push(val);
}
expect(values).toEqual(['Francisco', 'Presencia']);
});
it('retains the types', () => {
cookies.id = 1;
cookies.accepted = true;
cookies.name = 'Francisco';
cookies.friends = [3, 5];
cookies.user = { id: 1, accepted: true, name: 'Francisco' };
expect(typeof cookies.id).toEqual('number');
expect(typeof cookies.accepted).toEqual('boolean');
expect(typeof cookies.name).toEqual('string');
expect(Array.isArray(cookies.friends)).toEqual(true);
expect(typeof cookies.user).toEqual('object');
for (let key in cookies) {
delete cookies[key];
}
});
it('can iterate with invalid items', () => {
cookies.firstname = 'Francisco';
document.cookie = 'lastname=Presencia';
document.cookie = 'age=25';
const keys = [];
for (let key in cookies) {
keys.push(key);
}
const values = [];
for (let key of cookies) {
values.push(key);
}
expect(keys).toEqual(['firstname', 'lastname', 'age']);
expect(values).toEqual(['Francisco', 'Presencia', 25]);
expect(Object.keys(cookies)).toEqual(['firstname', 'lastname', 'age']);
expect(Object.values(cookies)).toEqual(['Francisco', 'Presencia', 25]);
expect(Object.entries(cookies)).toEqual([
['firstname', 'Francisco'],
['lastname', 'Presencia'],
['age', 25]
]);
delete cookies.firstname;
delete cookies.lastname;
delete cookies.age;
});
// it('ignores failing values on the iteration', () => {
// session.firstname = 'Francisco';
// sessionStorage.setItem('lastname', 'Presencia');
// expect(Object.keys(session)).toEqual(['firstname']);
// expect(Object.values(session)).toEqual(['Francisco']);
// expect(Object.entries(session)).toEqual([['firstname', 'Francisco']]);
// delete session.firstname;
// sessionStorage.removeItem('lastname');
// });
describe('options', () => {
it('will expire naturally', async () => {
cookies[options] = {};
expect(cookies.id).toBe(null);
cookies.id = 10;
await delay(100);
expect(cookies.id).toBe(10);
delete cookies.id;
await delay(100);
expect(cookies.id).toBe(null);
});
it('can set the expiration', async () => {
cookies[options] = { expires: 1 };
expect(cookies.id).toBe(null);
cookies.id = 10;
expect(cookies.id).toBe(10);
await delay(1100);
expect(cookies[options].expires).toBe(1);
expect(cookies.id).toBe(null);
});
it('can set the expiration', async () => {
cookies[options].expires = 2;
expect(cookies.id).toBe(null);
cookies.id = 10;
expect(cookies.id).toBe(10);
expect(cookies[options].expires).toBe(2);
await delay(1100);
expect(cookies.id).toBe(10);
await delay(1000);
expect(cookies.id).toBe(null);
});
});
});