/
cookie.test.ts
39 lines (31 loc) · 1 KB
/
cookie.test.ts
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
/**
* @jest-environment jsdom
*/
import * as cookieUtils from 'cookie'
import { cookie } from './cookie'
import { response } from '../response'
import { clearCookies } from '../../test/support/utils'
beforeAll(() => {
clearCookies()
})
afterEach(() => {
clearCookies()
})
test('sets a given response cookie', async () => {
const result = await response(cookie('myCookie', 'value'))
expect(result.headers.get('set-cookie')).toBe('myCookie=value')
// Propagates the response cookies on the document.
const allCookies = cookieUtils.parse(document.cookie)
expect(allCookies).toEqual({ myCookie: 'value' })
})
test('supports setting multiple response cookies', async () => {
const result = await response(
cookie('firstCookie', 'yes'),
cookie('secondCookie', 'no'),
)
expect(result.headers.get('set-cookie')).toBe(
'secondCookie=no, firstCookie=yes',
)
const allCookies = cookieUtils.parse(document.cookie)
expect(allCookies).toEqual({ firstCookie: 'yes', secondCookie: 'no' })
})