我想正确测试异步操作更改存储状态。
但是,当我进行配置存储并使用 store.dispatch 时,它会导致无限循环并且测试以超时错误结束。
这是我的代码。
import { configureStore } from "@reduxjs/toolkit";
import * as APIs from "../../common/APIs";
import { IRepository, repositoryFactory } from "../../common/Interfaces";
import reposReducer, { fetchRepositories, reposInitialState, toBeLoaded } from "./reposSlice";
import store from "../../app/store";
import authReducer from "../auth/authSlice";
jest.mock("../../common/APIs");
const mockedAPIs = APIs as jest.Mocked<typeof APIs>;
describe("reposSlice", () => {
const store = configureStore({
reducer: {
repos: reposReducer,
},
});
it("Should fetch repositories correctly", (done) => {
const repository : IRepository = repositoryFactory();
mockedAPIs.getRepositories.mockResolvedValue([repository]);
store.dispatch(fetchRepositories("")).then(() => {
expect(store.getState().repos.repoList.length).toBe(1);
});
});
});
我认为我的异步操作“fetchRepositories”效果很好。
因为当我写
console.log(current(state));
在 builder.addCase 中,它打印正确的状态。
谢谢。