-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathaxios.test.js
94 lines (77 loc) · 2.35 KB
/
axios.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
jest.setTimeout(60000)
const { Nuxt, Builder } = require('nuxt-edge')
const axios = require('axios')
const config = require('./fixture/nuxt.config')
const url = path => `http://localhost:3000${path}`
describe('axios module', () => {
let nuxt
let addTemplate
beforeAll(async () => {
nuxt = new Nuxt(config)
// Spy addTemplate
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
nuxt.moduleContainer.addTemplate
)
await new Builder(nuxt).build()
await nuxt.listen(3000)
})
afterAll(async () => {
await nuxt.close()
})
test('baseURL', () => {
expect(addTemplate).toBeDefined()
const call = addTemplate.mock.calls.find(args =>
args[0].src.includes('plugin.js')
)
const options = call[0].options
expect(options.baseURL.toString()).toBe(
`http://localhost:3000/test_api`
)
expect(options.browserBaseURL.toString()).toBe('/test_api')
})
test('asyncData', async () => {
const html = (await axios.get(url('/asyncData'))).data
expect(html).toContain('foo/bar')
})
test('mounted', async () => {
const window = await nuxt.renderAndGetWindow(url('/mounted'))
window.onNuxtReady(() => {
const html = window.document.body.innerHTML
expect(html).toContain('foo/bar')
})
})
test('init', async () => {
const window = await nuxt.renderAndGetWindow(url('/mounted'))
window.onNuxtReady(() => {
const $axios = window.$nuxt.$axios
expect($axios.defaults.xsrfHeaderName).toBe('X-CSRF-TOKEN')
})
})
test('ssr', async () => {
const makeReq = login =>
axios
.get(url('/ssr' + (login ? '?login' : '')))
.then(r => r.data)
.then(h => /session-[0-9]+/.exec(h))
.then(m => (m && m[0] ? m[0] : null))
const a = await makeReq()
const b = await makeReq(true)
const c = await makeReq()
const d = await makeReq(true)
expect(a).toBeNull()
expect(b).not.toBeNull()
expect(c).toBeNull() // Important!
expect(d).not.toBeNull()
expect(b).not.toBe(d)
})
test('ssr no brotli', async () => {
const makeReq = login =>
axios
.get(url('/ssr' + (login ? '?login' : '')))
.then(r => r.data)
.then(h => /encoding-\$(.*)\$/.exec(h))
.then(m => (m && m[1] ? m[1] : null))
const result = await makeReq()
expect(result).toBe('gzip, deflate')
})
})