/
cookies.test.ts
47 lines (38 loc) · 1.35 KB
/
cookies.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
40
41
42
43
44
45
46
47
import * as path from 'path'
import * as cookieUtils from 'cookie'
import { pageWith } from 'page-with'
function createRuntime() {
return pageWith({ example: path.resolve(__dirname, 'cookies.mocks.ts') })
}
test('allows setting cookies on the mocked response', async () => {
const runtime = await createRuntime()
const res = await runtime.request('/user')
const headers = await res.allHeaders()
const body = await res.json()
expect(headers).toHaveProperty('x-powered-by', 'msw')
expect(headers).not.toHaveProperty('set-cookie')
expect(body).toEqual({
mocked: true,
})
// Should be able to access the response cookies.
const cookieString = await runtime.page.evaluate(() => {
return document.cookie
})
const allCookies = cookieUtils.parse(cookieString)
expect(allCookies).toEqual({ myCookie: 'value' })
})
test('allows setting multiple response cookies', async () => {
const runtime = await createRuntime()
const res = await runtime.request('/order')
const headers = await res.allHeaders()
expect(headers).toHaveProperty('x-powered-by', 'msw')
expect(headers).not.toHaveProperty('set-cookie')
const cookieString = await runtime.page.evaluate(() => {
return document.cookie
})
const allCookies = cookieUtils.parse(cookieString)
expect(allCookies).toEqual({
firstCookie: 'yes',
secondCookie: 'no',
})
})