gpt4 book ai didi

javascript - 模拟内部 axios.create()

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:54 25 4
gpt4 key购买 nike

我正在使用 jestaxios-mock-adapterredux 异步中测试 axios API 调用行动创造者。

当我使用通过 axios.create() 创建的 axios 实例时,我无法让它们工作:

import axios from 'axios';

const { REACT_APP_BASE_URL } = process.env;

export const ajax = axios.create({
baseURL: REACT_APP_BASE_URL,
});

我会在我的 async action creator 中使用它,例如:

import { ajax } from '../../api/Ajax'

export function reportGet(data) {
return async (dispatch, getState) => {
dispatch({ type: REQUEST_TRANSACTION_DATA })

try {
const result = await ajax.post(
END_POINT_MERCHANT_TRANSACTIONS_GET,
data,
)
dispatch({ type: RECEIVE_TRANSACTION_DATA, data: result.data })
return result.data
} catch (e) {
throw new Error(e);
}
}
}

这是我的测试文件:

import {
reportGet,
REQUEST_TRANSACTION_DATA,
RECEIVE_TRANSACTION_DATA,
} from '../redux/TransactionRedux'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { END_POINT_MERCHANT_TRANSACTIONS_GET } from 'src/utils/apiHandler'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore({ transactions: {} })

test('get report data', async () => {
let mock = new MockAdapter(axios)

const mockData = {
totalSalesAmount: 0
}

mock.onPost(END_POINT_MERCHANT_TRANSACTIONS_GET).reply(200, mockData)

const expectedActions = [
{ type: REQUEST_TRANSACTION_DATA },
{ type: RECEIVE_TRANSACTION_DATA, data: mockData },
]

await store.dispatch(reportGet())
expect(store.getActions()).toEqual(expectedActions)
})

我只得到一个操作 Received: [{"type": "REQUEST_TRANSACTION_DATA"}] 因为 ajax.post 有错误。

我已经尝试了很多方法来模拟 axios.create,但在不知道自己在做什么的情况下无济于事。感谢任何帮助。

最佳答案

好的,我知道了。这是我修复它的方法!我最终没有 axios 的任何模拟库!

src/__mocks__ 中为 axios 创建一个模拟:

// src/__mocks__/axios.ts

const mockAxios = jest.genMockFromModule('axios')

// this is the key to fix the axios.create() undefined error!
mockAxios.create = jest.fn(() => mockAxios)

export default mockAxios

然后在你的测试文件中,要点看起来像:

import mockAxios from 'axios'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

// for some reason i need this to fix reducer keys undefined errors..
jest.mock('../../store/rootStore.ts')

// you need the 'async'!
test('Retrieve transaction data based on a date range', async () => {
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore()

const mockData = {
'data': 123
}

/**
* SETUP
* This is where you override the 'post' method of your mocked axios and return
* mocked data in an appropriate data structure-- {data: YOUR_DATA} -- which
* mirrors the actual API call, in this case, the 'reportGet'
*/
mockAxios.post.mockImplementationOnce(() =>
Promise.resolve({ data: mockData }),
)

const expectedActions = [
{ type: REQUEST_TRANSACTION_DATA },
{ type: RECEIVE_TRANSACTION_DATA, data: mockData },
]

// work
await store.dispatch(reportGet())

// assertions / expects
expect(store.getActions()).toEqual(expectedActions)
expect(mockAxios.post).toHaveBeenCalledTimes(1)
})

关于javascript - 模拟内部 axios.create(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com