-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcookie-api.test.ts
More file actions
138 lines (119 loc) · 4.21 KB
/
cookie-api.test.ts
File metadata and controls
138 lines (119 loc) · 4.21 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
import cookieApi from '../src/utils/CookieApi'
import MockDate from 'mockdate'
beforeEach(() => {
deleteAllCookies()
})
test('should remove cookie when it is present', () => {
document.cookie = 'mockCookie=mockValue'
cookieApi.write('mockCookie', undefined, {})
expect(document.cookie).toEqual('')
})
test('should do nothing when removing a nonexisting cookie', () => {
document.cookie = 'mockCookie=mockValue'
cookieApi.write('someothercookiethatdoesntexist', undefined, {})
expect(document.cookie).toEqual('mockCookie=mockValue')
})
test('it should create and store a new cookie', () => {
cookieApi.write('mockCookie', 'value', {})
expect(document.cookie).toEqual('mockCookie=value')
})
test('it should update an existing cookie', () => {
document.cookie = 'mockCookie=oldvalue'
cookieApi.write('mockCookie', 'newValue', {})
expect(document.cookie).toEqual('mockCookie=newValue')
})
test('it should read a cookie', () => {
document.cookie = 'firstCookie=value1;'
expect(cookieApi.read('firstCookie')).toEqual('value1')
})
test('it should read correctly read cookie when there are multiple', () => {
document.cookie = 'firstCookie=value1;'
document.cookie = 'secondCookie=value2;'
document.cookie = 'thirdCookie=value3;'
expect(cookieApi.read('secondCookie')).toEqual('value2')
})
test('should accept path option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
path: '/mypath',
})
const path = getValFromCookieString(cookieString, 'path')
expect(path).toEqual('/mypath')
})
test('should accept domain option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
domain: '.example.com',
})
const domain = getValFromCookieString(cookieString, 'domain')
expect(domain).toEqual('.example.com')
})
test('should accept secure option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
secure: true,
})
expect(cookieString).toContain('secure')
})
test('should accept "lax" as samesite option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
samesite: 'lax',
})
const samesite = getValFromCookieString(cookieString, 'samesite')
expect(samesite).toEqual('lax')
})
test('should accept "strict" as samesite option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
samesite: 'strict',
})
const samesite = getValFromCookieString(cookieString, 'samesite')
expect(samesite).toEqual('strict')
})
test('should accept "none" as samesite option', () => {
const cookieString = cookieApi.buildCookieString('myCookie', 'myCookieVal', {
samesite: 'none',
})
const samesite = getValFromCookieString(cookieString, 'samesite')
expect(samesite).toEqual('none')
})
test('should set expiration date to a year after current date', () => {
MockDate.set(1608829845699) // mock date to 12/24/2020
const cookieString = cookieApi.buildCookieString(
'myCookie',
'myCookieVal',
{}
)
const expires = getValFromCookieString(cookieString, 'expires')
expect(expires).toEqual('Fri, 24 Dec 2021 17:10:45 GMT')
MockDate.reset() // stop mocking date
})
test('should always use epoch time as expire time on remove', function() {
const cookieString = cookieApi.buildCookieString('myCookie', undefined, {})
const expires = getValFromCookieString(cookieString, 'expires')
expect(expires).toEqual('Thu, 01 Jan 1970 00:00:00 GMT')
})
function deleteAllCookies() {
const cookies = document.cookie.split(';')
const path = window.location.pathname
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i]
const eqPos = cookie.indexOf('=')
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
const parts = path.split('/')
while (parts.length) {
document.cookie =
name +
'=;path=' +
(parts.join('/') || '/') +
';expires=Thu, 01 Jan 1970 00:00:00 GMT'
parts.pop()
}
}
}
function getValFromCookieString(cookieString: string, propertyName: string) {
let result = ''
cookieString.split(';').forEach(keyval => {
const [key, val] = keyval.split('=')
if (key === propertyName) {
result = val
}
})
return result
}