Last active
April 20, 2022 17:23
-
-
Save thiporia/18ce721cdf66fc9c278ad1cc0f37d44b to your computer and use it in GitHub Desktop.
jest mocking in axios.create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import MockAdapter from 'axios-mock-adapter' | |
import ApiController from '@common/helpers/ApiController' | |
// ApiController return { api = axios.create, get, post, del, patch } | |
describe('ApiController', () => { | |
const apiController = ApiController({ isAuth: true }) | |
const axiosMock = new MockAdapter(apiController.api) | |
beforeEach(() => { | |
axiosMock | |
.onGet(`${process.env.NEXT_PUBLIC_HOSTNAME}/api/user/list`, { | |
headers: { | |
Authorization: 'Bearer undefined', | |
}, | |
}) | |
.reply(200, { | |
list: [ | |
{ id: 1, name: 'seon' }, | |
{ id: 2, name: 'yeong' }, | |
], | |
}) | |
axiosMock.onPost(`/api/admin/check`).reply(200, { isAdmin: true }) | |
axiosMock.onPost(`/api/admin/login`).reply(200, { msg: 'Post complete' }) | |
axiosMock | |
.onGet(`/api/admin/list`, { params: { page: 1, length: 3 } }) | |
.reply(200, { | |
list: [ | |
{ id: 1, name: 'seon' }, | |
{ id: 2, name: 'yeong' }, | |
{ id: 3, name: 'jang' }, | |
], | |
}) | |
axios.patch = jest | |
.fn() | |
.mockResolvedValueOnce({ status: 200, msg: 'Patch complete' }) | |
axiosMock | |
.onDelete(`/api/admin/info`) | |
.reply(200, { isDelete: true, msg: 'Delete complete.', msgId: 'D001' }) | |
}) | |
it('get Success', async (done) => { | |
const successResponse = await apiController.get(`/api/user/list`) | |
expect(successResponse.status).toBe(200) | |
expect(successResponse.data.list.length).toBe(2) | |
done() | |
}) | |
it.only('has auth get test', async (done) => { | |
console.log('---------------------------------------------------') | |
console.log(apiController.api) | |
const res = await apiController.get(`/api/user/list`) | |
expect(res.status).toBe(200) | |
expect(res.data.list.length).toBe(2) | |
done() | |
}) | |
it('get Failure', async (done) => { | |
axiosMock | |
.onGet(`/api/user/list`) | |
.reply(400, { status: 404, message: 'Not Found' }) | |
try { | |
await apiController.get(`/api/user/list`) | |
} catch (err) { | |
expect(err).toEqual(new Error('Request failed with status code 400')) | |
} | |
done() | |
}) | |
it('getAll', async (done) => { | |
const apis = [ | |
{ url: 'api/admin/list', payload: { page: 1, length: 3 } }, | |
{ url: 'api/user/list' }, | |
] | |
const responses = await apiController.getAll(apis) | |
expect(responses.length).toBe(apis.length) | |
done() | |
}) | |
it('getAll default params', async (done) => { | |
const responses = await apiController.getAll() | |
expect(responses.length).toBe(0) | |
done() | |
}) | |
it('post Success', async (done) => { | |
const res = await apiController.post(`/api/admin/login`) | |
expect(res.data.msg).toBe('Post complete') | |
done() | |
}) | |
it('post Failure', async (done) => { | |
axiosMock | |
.onPost(`/api/admin/login`) | |
.reply(400, { status: 404, message: 'Not Found' }) | |
try { | |
await apiController.post(`/api/admin/login`) | |
} catch (err) { | |
expect(err).toEqual(new Error('Request failed with status code 400')) | |
} | |
done() | |
}) | |
it('patch Success', async () => { | |
const res = await apiController.patch(`/api/admin/list`) | |
expect(res.msg).toEqual('Patch complete') | |
}) | |
it('patch Failure', async () => { | |
axios.patch = jest | |
.fn() | |
.mockRejectedValueOnce({ status: 404, msg: 'Not Found' }) | |
try { | |
await apiController.patch(`/api/admin/list`) | |
} catch (err) { | |
expect(err.status).toBe(404) | |
expect(err.msg).toEqual('Not Found') | |
} | |
}) | |
it('delete Success', async () => { | |
const response = await apiController.del('/api/admin/info') | |
expect(response.status).toBe(200) | |
expect(response.data).toEqual({ | |
isDelete: true, | |
msg: 'Delete complete.', | |
msgId: 'D001', | |
}) | |
}) | |
it('delete Failure', async () => { | |
axiosMock | |
.onDelete(`/api/admin/info`) | |
.reply(400, { status: 404, message: 'Not Found' }) | |
try { | |
await apiController.del('/api/admin/info') | |
} catch (err) { | |
expect(err).toEqual(new Error('Request failed with status code 400')) | |
} | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First, the Axios.create object should be put in the return item so that the axios-mock-adapter can mock the value.