forked from nuxt-community/axios-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxios.test.js
179 lines (143 loc) · 4.32 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
jest.setTimeout(60000)
const { Nuxt, Builder } = require('nuxt-edge')
const axios = require('axios')
const config = require('./fixture/nuxt.config')
let nuxt, addTemplate
const url = path => `http://localhost:3000${path}`
const setupNuxt = async (config) => {
nuxt = new Nuxt(config)
// Spy addTemplate
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
nuxt.moduleContainer.addTemplate
)
const build = new Builder(nuxt)
await build.validatePages()
await build.generateRoutesAndFiles()
await nuxt.listen(3000)
}
const testSuite = () => {
test('baseURL', () => {
expect(addTemplate).toBeDefined()
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
const options = call[0].options
const proto = options.https ? 'https' : 'http'
expect(options.baseURL.toString()).toBe(`${proto}://localhost:3000/api`)
expect(options.browserBaseURL.toString()).toBe('/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('createCopy', async () => {
const window = await nuxt.renderAndGetWindow(url('/mounted'))
window.onNuxtReady(() => {
const $axios = window.$nuxt.$axios
const newInstance = $axios.create()
expect(newInstance.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')
})
}
describe('module', () => {
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()
})
testSuite()
})
describe('other options', () => {
beforeAll(async () => {
config.axios = {
prefix: '/api',
proxy: {},
credentials: true,
https: true,
retry: false
}
await setupNuxt(config)
})
afterAll(async () => {
await nuxt.close()
})
testSuite()
})
describe('browserBaseURL', () => {
beforeAll(async () => {
config.axios = {
browserBaseURL: '/api'
}
await setupNuxt(config)
})
afterAll(async () => {
await nuxt.close()
})
test('custom', () => {
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/')
expect(options.browserBaseURL.toString()).toBe('/api')
})
})
describe('empty config', () => {
beforeAll(async () => {
config.axios = {}
await setupNuxt(config)
})
afterAll(async () => {
await nuxt.close()
})
test('preset baseURL and browserBaseURL', () => {
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/')
expect(options.browserBaseURL.toString()).toBe('http://localhost:3000/')
})
})