Skip to content

Commit c63d5ef

Browse files
ricardogobbosouzapi0
authored andcommitted
test(module): increase coverage (#213)
1 parent bcfeda3 commit c63d5ef

File tree

5 files changed

+122
-47
lines changed

5 files changed

+122
-47
lines changed

test/axios.test.js

Lines changed: 109 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,34 @@ jest.setTimeout(60000)
22

33
const { Nuxt, Builder } = require('nuxt-edge')
44
const axios = require('axios')
5+
56
const config = require('./fixture/nuxt.config')
67

7-
const url = path => `http://localhost:3000${path}`
8+
let nuxt, addTemplate
89

9-
describe('axios module', () => {
10-
let nuxt
11-
let addTemplate
10+
const url = path => `http://localhost:3000${path}`
1211

13-
beforeAll(async () => {
14-
nuxt = new Nuxt(config)
12+
const setupNuxt = async (config) => {
13+
nuxt = new Nuxt(config)
1514

16-
// Spy addTemplate
17-
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
18-
nuxt.moduleContainer.addTemplate
19-
)
15+
// Spy addTemplate
16+
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
17+
nuxt.moduleContainer.addTemplate
18+
)
2019

21-
await new Builder(nuxt).build()
22-
await nuxt.listen(3000)
23-
})
20+
const build = new Builder(nuxt)
2421

25-
afterAll(async () => {
26-
await nuxt.close()
27-
})
22+
await build.validatePages()
23+
await build.generateRoutesAndFiles()
24+
await nuxt.listen(3000)
25+
}
2826

27+
const testSuite = () => {
2928
test('baseURL', () => {
3029
expect(addTemplate).toBeDefined()
31-
const call = addTemplate.mock.calls.find(args =>
32-
args[0].src.includes('plugin.js')
33-
)
30+
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
3431
const options = call[0].options
35-
expect(options.baseURL.toString()).toBe(
36-
`http://localhost:3000/test_api`
37-
)
32+
expect(options.baseURL.toString()).toBe('http://localhost:3000/test_api')
3833
expect(options.browserBaseURL.toString()).toBe('/test_api')
3934
})
4035

@@ -60,12 +55,11 @@ describe('axios module', () => {
6055
})
6156

6257
test('ssr', async () => {
63-
const makeReq = login =>
64-
axios
65-
.get(url('/ssr' + (login ? '?login' : '')))
66-
.then(r => r.data)
67-
.then(h => /session-[0-9]+/.exec(h))
68-
.then(m => (m && m[0] ? m[0] : null))
58+
const makeReq = login => axios
59+
.get(url('/ssr' + (login ? '?login' : '')))
60+
.then(r => r.data)
61+
.then(h => /session-[0-9]+/.exec(h))
62+
.then(m => (m && m[0] ? m[0] : null))
6963

7064
const a = await makeReq()
7165
const b = await makeReq(true)
@@ -80,15 +74,96 @@ describe('axios module', () => {
8074
})
8175

8276
test('ssr no brotli', async () => {
83-
const makeReq = login =>
84-
axios
85-
.get(url('/ssr' + (login ? '?login' : '')))
86-
.then(r => r.data)
87-
.then(h => /encoding-\$(.*)\$/.exec(h))
88-
.then(m => (m && m[1] ? m[1] : null))
77+
const makeReq = login => axios
78+
.get(url('/ssr' + (login ? '?login' : '')))
79+
.then(r => r.data)
80+
.then(h => /encoding-\$(.*)\$/.exec(h))
81+
.then(m => (m && m[1] ? m[1] : null))
8982

9083
const result = await makeReq()
9184

9285
expect(result).toBe('gzip, deflate')
9386
})
87+
}
88+
89+
describe('module', () => {
90+
beforeAll(async () => {
91+
nuxt = new Nuxt(config)
92+
93+
// Spy addTemplate
94+
addTemplate = nuxt.moduleContainer.addTemplate = jest.fn(
95+
nuxt.moduleContainer.addTemplate
96+
)
97+
98+
await new Builder(nuxt).build()
99+
await nuxt.listen(3000)
100+
})
101+
102+
afterAll(async () => {
103+
await nuxt.close()
104+
})
105+
106+
testSuite()
107+
})
108+
109+
describe('other options', () => {
110+
beforeAll(async () => {
111+
config.axios = {
112+
prefix: '/test_api',
113+
proxy: {},
114+
credentials: true,
115+
https: true,
116+
retry: false
117+
}
118+
119+
await setupNuxt(config)
120+
})
121+
122+
afterAll(async () => {
123+
await nuxt.close()
124+
})
125+
126+
testSuite()
127+
})
128+
129+
describe('browserBaseURL', () => {
130+
beforeAll(async () => {
131+
config.axios = {
132+
browserBaseURL: '/test_api'
133+
}
134+
135+
await setupNuxt(config)
136+
})
137+
138+
afterAll(async () => {
139+
await nuxt.close()
140+
})
141+
142+
test('custom', () => {
143+
expect(addTemplate).toBeDefined()
144+
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
145+
const options = call[0].options
146+
expect(options.baseURL.toString()).toBe('http://localhost:3000/')
147+
expect(options.browserBaseURL.toString()).toBe('/test_api')
148+
})
149+
})
150+
151+
describe('empty config', () => {
152+
beforeAll(async () => {
153+
config.axios = {}
154+
155+
await setupNuxt(config)
156+
})
157+
158+
afterAll(async () => {
159+
await nuxt.close()
160+
})
161+
162+
test('preset baseURL and browserBaseURL', () => {
163+
expect(addTemplate).toBeDefined()
164+
const call = addTemplate.mock.calls.find(args => args[0].src.includes('plugin.js'))
165+
const options = call[0].options
166+
expect(options.baseURL.toString()).toBe('http://localhost:3000/')
167+
expect(options.browserBaseURL.toString()).toBe('http://localhost:3000/')
168+
})
94169
})

test/fixture/nuxt.config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const { resolve } = require('path')
22

33
module.exports = {
44
rootDir: resolve(__dirname, '../..'),
5+
buildDir: resolve(__dirname, '.nuxt'),
56
srcDir: __dirname,
6-
dev: false,
77
render: {
88
resourceHints: false
99
},
@@ -12,13 +12,11 @@ module.exports = {
1212
],
1313
serverMiddleware: ['~/api.js'],
1414
axios: {
15-
prefix: `/test_api`,
15+
prefix: '/test_api',
1616
proxy: true,
1717
credentials: true,
1818
debug: true,
19-
retry: {
20-
retries: 3
21-
}
19+
retry: true
2220
},
2321
plugins: ['~/plugins/axios']
2422
}

test/fixture/pages/asyncData.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<template>
2-
<div>
2+
<div>
33
Response: {{ res }}
4-
</div>
4+
</div>
55
</template>
66

77
<script>
88
export default {
9-
async asyncData ({ app }) {
9+
async asyncData({ app }) {
1010
let res = await app.$axios.$get('foo/bar')
1111
return {
1212
res

test/fixture/pages/mounted.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
<script>
88
export default {
9-
data () {
9+
data() {
1010
return {
1111
res: ''
1212
}
1313
},
14-
async mounted () {
14+
15+
async mounted() {
1516
// Request with full url becasue we are in JSDom env
1617
this.res = await this.$axios.$get('http://localhost:3000/test_api/foo/bar')
1718
}

test/fixture/pages/ssr.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
let reqCtr = 1
1111
1212
export default {
13-
async fetch ({app, route}) {
13+
async fetch({app, route}) {
1414
let doLogin = route.query.login !== undefined
1515
if (doLogin) {
1616
app.$axios.setHeader('sessionId', reqCtr++)
1717
}
1818
},
1919
computed: {
20-
axiosSessionId () {
20+
axiosSessionId() {
2121
return this.$axios.defaults.headers.common.sessionId
2222
},
23-
axiosEncoding () {
23+
24+
axiosEncoding() {
2425
return this.$axios.defaults.headers.common['Accept-Encoding']
2526
}
2627
}

0 commit comments

Comments
 (0)