【发布时间】:2020-05-03 02:09:54
【问题描述】:
组件:
export const fetchList = () => {
return API.get(AMPLIFY_ENPOINTS.default, API_URLS.list, { response: true });
}
const List: React.FC = () => {
const dispatch = useDispatch();
const setError = useError();
useEffect(() => {
fetchList()
.then((response) => {
if (response && response.data?.length) {
dispatch(setList(response.data));
}
})
.catch((error) => {
setError(error);
});
}, [])
}
测试:
it('should fetch list', async () => {
const wrapper = mount(
<Provider store={store}>
<List />
</Provider>
);
API.get = jest.fn().mockImplementation(() => Promise.resolve({ data: mockList }));
const response = await fetchList();
console.log(store.getActions(), response); // HERE IS THE ISSUE
});
所以store.getActions() 从catch 块返回setError,这是为什么呢?它应该从then 块返回setList。我究竟做错了什么? response 变量返回 mockList 就好了。
编辑
它返回的错误是API not configured,我正在使用aws amplify。
【问题讨论】:
标签: reactjs redux jestjs enzyme aws-amplify