【发布时间】:2021-07-13 16:53:13
【问题描述】:
所以我有一个创建对 openWeather 网站的 API 调用的动作创建者,我想测试这个创建者,但是在设置模拟测试时,存储和 thunk;我收到一个我不确定的类型错误。
我的动作创建者:
type Effect = ThunkAction<void, State, unknown, WeatherActions>
export const fetchWeatherData = (city: string): Effect => async (dispatch) => {
try {
return await axios.get(`${URL}${city}&appid=${API}`)
.then((resp) => {
console.log('res', resp)
dispatch(weatherSuccess(resp.data))
})
.catch((err) => {
console.log('err', err)
dispatch(loadingError(err))
})
}
catch(e) {
throw Error(`Network Error: ${e}`)
}
}
我的动作创建者测试设置:
jest.mock('axios')
const initialState = {}
type State = typeof initialState
const middleware = [thunk]
const mockStore = configureStore<State, ThunkDispatch<State, unknown, AnyAction>>(middleware)
const store = mockStore(initialState)
describe('Testing api action creator', () => {
afterEach(() => {
store.clearActions()
})
it('#loading', async () => {
await store.dispatch(fetchWeatherData('peterborough'))
})
})
【问题讨论】:
标签: reactjs typescript react-redux redux-thunk react-typescript