我正在学习如何编写测试,并查看redux文档以编写类似于docs的测试。 我的项目是通过create-react-app创建的。
我模仿的是: https://redux.js.org/recipes/writing-tests
我的代码:
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import * as actions from "../app/store/account/actions";
import * as actionTypes from "../app/store/account/actionTypes";
import fetchMock from "fetch-mock-jest";
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe("async actions", () => {
it("create GET_ACCOUNT_IMAGE when fetching images has been done", () => {
fetchMock.get("https://aws.random.cat/meow", 200);
const expectedActions = [
{ type: actionTypes.START_FETCH_ACCOUNT_IMAGE },
{ type: actionTypes.GET_ACCOUNT_IMAGE },
];
const store = mockStore({});
return store
.dispatch(actions.getAccountImage("https://aws.random.cat/meow"))
.then(() => {
// return of async actions
expect(store.getActions()).toEqual(expectedActions);
});
});
});