Trying to mock an API call which accessed by apisauce.
basically API below retrieve list of some object, and I need to test this api call via jest
here what I tried;
TagApi:
import { ApiResponse, ApisauceInstance } from 'apisauce'
import { Instance } from 'mobx-state-tree'
export const TagApi = {
apisauce: {} as ApisauceInstance,
getUserTags: async (userId: number) => {
const response: ApiResponse<Array<Instance<typeof UserTag>>> = await TagApi.apisauce.get(
`/users/${userId}/tags/`,
)
return handleResponse(response)
},
}
users-tab.test.ts:
import axios, { AxiosResponse } from 'axios'
import { TagApi } from '@/core/src/services/api-objects/TagApi'
import MockAdapter from 'axios-mock-adapter'
test('mock adapter', async () => {
const mock = new MockAdapter(TagApi.apisauce.axiosInstance)
mock.onGet('/users/2/tags/').reply(201, {
x: 'hello',
})
const response = await TagApi.getUserTags(2)
expect(response.ok).toBe(true)
expect(response.status).toBe(201)
expect(response.data).toEqual({ x: 'hello' })
})
I expect it to run, but it fails exception with the message:
how to fix it?