Skip to content

Commit 2608139

Browse files
committed
add axios.spec.ts to show the problem when overriding axios.defaults.baseUrl by ourselves
Use mock-xmlhttprequest to mock the XMLHttpRequests axios uses internally. Write the test in typescript, because we should also move the tests to typescript.
1 parent e6bb3d5 commit 2608139

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = {
44
transform: {
55
'^.+\\.ts?$': 'ts-jest',
66
'^.+\\.js?$': 'babel-jest'
7-
}
7+
},
8+
transformIgnorePatterns: [
9+
'node_modules/(?!(mock-xmlhttprequest))'
10+
]
811
}

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@babel/preset-env": "7.16.11",
5252
"@babel/preset-typescript": "7.16.7",
5353
"@nuxt/types": "2.15.8",
54+
"@types/jest": "27.5.1",
5455
"@typescript-eslint/eslint-plugin": "5.10.1",
5556
"@typescript-eslint/parser": "5.10.1",
5657
"@vue/eslint-config-standard": "6.1.0",
@@ -70,6 +71,7 @@
7071
"eslint-plugin-vue": "7.20.0",
7172
"jest": "27.5.1",
7273
"lodash": "4.17.21",
74+
"mock-xmlhttprequest": "8.1.0",
7375
"rimraf": "3.0.2",
7476
"source-map-loader": "3.0.1",
7577
"ts-jest": "27.1.3",

tests/axios.spec.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import HalJsonVuex from '../src/index'
2+
import Vue from 'vue'
3+
import Vuex from 'vuex'
4+
import axios from 'axios'
5+
import { cloneDeep } from 'lodash'
6+
import { newServer } from 'mock-xmlhttprequest'
7+
8+
let store
9+
let stateCopy
10+
let halJsonVuex
11+
let server
12+
13+
describe('When using baseUrl with axios', () => {
14+
beforeAll(() => {
15+
Vue.use(Vuex)
16+
store = new Vuex.Store({
17+
modules: {},
18+
strict: process.env.NODE_ENV !== 'production'
19+
})
20+
stateCopy = cloneDeep(store.state)
21+
})
22+
23+
beforeEach(() => {
24+
server = newServer()
25+
server.install()
26+
27+
store.replaceState(cloneDeep(stateCopy))
28+
halJsonVuex = HalJsonVuex(store, axios, { forceRequestedSelfLink: true })
29+
})
30+
31+
afterEach(() => {
32+
server.remove()
33+
})
34+
35+
const baseUrlParams = [
36+
{
37+
baseUrl: 'http://localhost:3000',
38+
api: {
39+
entities: {
40+
href: '/entities'
41+
},
42+
_links: {
43+
self: {
44+
href: 'http://localhost:3000/'
45+
}
46+
}
47+
},
48+
expectedFetches: [
49+
'http://localhost:3000',
50+
'http://localhost:3000/entities'
51+
]
52+
},
53+
{
54+
baseUrl: 'http://localhost:3000/api',
55+
api: {
56+
entities: {
57+
href: '/api/entities'
58+
},
59+
_links: {
60+
self: {
61+
href: 'http://localhost:3000/api'
62+
}
63+
}
64+
},
65+
expectedFetches: [
66+
'http://localhost:3000/api',
67+
'http://localhost:3000/api/api/entities'
68+
]
69+
},
70+
{
71+
baseUrl: '/api',
72+
api: {
73+
entities: {
74+
href: '/api/entities'
75+
},
76+
_links: {
77+
self: {
78+
href: '/api'
79+
}
80+
}
81+
},
82+
expectedFetches: [
83+
'/api/api',
84+
'/api/api/entities'
85+
]
86+
}
87+
]
88+
baseUrlParams.forEach(({ baseUrl, api, expectedFetches }) => {
89+
it(`uses [${expectedFetches}] for get when baseUrl is ${baseUrl}`, async () => {
90+
axios.defaults.baseURL = baseUrl
91+
server.get(() => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
92+
93+
const url = await halJsonVuex.href(halJsonVuex.get(), 'entities')
94+
await halJsonVuex.get(url).load
95+
96+
const requestLog = server.getRequestLog()
97+
expect(requestLog.map(entry => entry.url)).toEqual(expectedFetches)
98+
})
99+
})
100+
101+
baseUrlParams.forEach(({ baseUrl, api, expectedFetches }) => {
102+
it(`uses [${expectedFetches}] for post when baseUrl is ${baseUrl}`, async () => {
103+
axios.defaults.baseURL = baseUrl
104+
server.get(() => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
105+
server.post(() => true, { status: 201, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
106+
107+
const url = await halJsonVuex.href(halJsonVuex.get(), 'entities')
108+
await halJsonVuex.post(url, {}).load
109+
110+
const requestLog = server.getRequestLog()
111+
expect(requestLog.map(entry => entry.url)).toEqual(expectedFetches)
112+
})
113+
})
114+
115+
baseUrlParams.forEach(({ baseUrl, api, expectedFetches }) => {
116+
it(`uses [${expectedFetches}] for patch when baseUrl is ${baseUrl}`, async () => {
117+
axios.defaults.baseURL = baseUrl
118+
server.get(() => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
119+
server.addHandler('PATCH', () => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
120+
121+
const url = await halJsonVuex.href(halJsonVuex.get(), 'entities')
122+
await halJsonVuex.patch(url, {}).load
123+
124+
const requestLog = server.getRequestLog()
125+
expect(requestLog.map(entry => entry.url)).toEqual(expectedFetches)
126+
})
127+
})
128+
129+
baseUrlParams.forEach(({ baseUrl, api, expectedFetches }) => {
130+
it(`uses [${expectedFetches}] for delete when baseUrl is ${baseUrl}`, async () => {
131+
axios.defaults.baseURL = baseUrl
132+
server.get(() => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
133+
server.delete(() => true, { status: 200, headers: {}, body: JSON.stringify(api), statusText: 'OK' })
134+
135+
const url = await halJsonVuex.href(halJsonVuex.get(), 'entities')
136+
await halJsonVuex.del(url, {}).load
137+
138+
const requestLog = server.getRequestLog()
139+
expect(requestLog.map(entry => entry.url)).toEqual(expectedFetches)
140+
})
141+
})
142+
})

0 commit comments

Comments
 (0)