【发布时间】:2022-11-11 11:13:51
【问题描述】:
我正在尝试将我的 redux 存储迁移到 RTK 和 RTK 查询 - 并使我的 RTL 测试与它一起工作
我打算逐步重写 - 将每个 reducer 1 逐 1 转换。
我重新创建了我在应用程序中使用的商店 - 用 configureStore 替换 createStore - 并添加了用于 api 处理的中间件,如下所示:
import { vendorPaymentsApiSlice } from './vendorPaymentsAPISlice';
const rootReducer = combineReducers({
users: userReducer,
[vendorPaymentsApiSlice.reducerPath]: vendorPaymentsApiSlice.reducer
})
//vendorPaymentsApiSlice.reducerPath is set to "vendorpaymentsApi"
const store = configureStore({
reducer: rootReducer,
middleware: [
...getDefaultMiddleware(),
sagaMiddleware,
vendorPaymentsApiSlice.middleware,
],
});
此设置在实际应用程序中运行良好。 api 的中间件链接按预期工作。
对于我的测试,我使用了一个模拟商店——它用 mockHistory 替换了历史,但它几乎是相同的代码——相同的 rootReducer 和相同的中间件数组。
在我的测试中,我为 api 调用编写了一个 msw 拦截器 - 拦截器被调用:
但一旦发生这种情况 - 我收到此错误消息:
Warning: Middleware for RTK-Query API at reducerPath "vendorpaymentsApi" has not been added to the store.
Features like automatic cache collection, automatic refetching etc. will not be available.
因此 - 来自 api 的 redux 中的数据设置失败。
在阅读this 并尝试过之后,我尝试了许多添加中间件的变体
getDefaultMiddleware().concat([
sagaMiddleware,
vendorPaymentsApiSlice.middleware,
])
但它仍然无法正常工作。
想要一些帮助来理解为什么这个 api 的链接没有发生在测试中。
非常感谢您阅读本文
【问题讨论】:
标签: reactjs api redux react-testing-library redux-toolkit