reactjs - 从 redux store redux saga 测试派生的模拟局部变量
问题描述
我编写了一个 saga,它使用 ayield select
来获取部分 redux 存储状态并将其存储在局部变量中。该变量是一个具有三个不同键的对象,每个键的值是其中的一个对象数组。
我的 redux-saga 测试一直失败,因为我在 saga 中创建了这个局部变量,该变量使用初始数据yield select
和测试中的变量 always undefined
,这导致我在该 saga 中的其余测试失败。我已经看到很多关于如何在 redux saga 中模拟返回状态的示例yield select
,但它总是在下一个 redux-saga 调用的上下文中。如何模拟局部变量的返回状态?
这是我的代码:
export default function* orderSelectionFlow({ payload }) {
try {
const orders = yield select(getOrders); // retrieve relevant part of redux store
const activeOrder = orders[payload.orderStatus].find(order => order.id === payload.orderId); // this variable is always undefined in my tests, because I am not sure how to mock `orders` for a local variable.
const activeOrderId = payload.orderId;
const isConnected = yield select(getIsConnected); // test for this select fails because activeOrder is undefined
我的测试(直到相关点)是
describe('test order selection flow', () => {
const navSpy = jest.spyOn(AppNavigator, 'navigate');
const action = {
type: 'galactica/orders/VIEW',
payload: {
orderStatus: 'OPEN',
orderId: 1
}
};
afterAll(() => {
jest.resetModules();
navSpy.mockRestore();
});
it.next = sagaHelper(orderSelectionFlow(action));
it.next('should get all orders', (result) => {
expect(result).toEqual(select(getOrders));
});
it.next('should use connected state', (result) => {
expect(result).toEqual(select(getIsConnected));
});
订单目前未定义,但如果我可以模拟它的价值
orders: {
'OPEN': {
orderId: 1
}
}
解决方案
我会尝试使用redux-saga-tester
:https ://github.com/wix/redux-saga-tester 。它让您在选项中指定初始状态。我不太确定您的所有代码是什么样的,但我认为您可以执行以下操作。
describe('test order selection flow', () => {
const navSpy = jest.spyOn(AppNavigator, 'navigate');
const action = {
type: 'galactica/orders/VIEW',
payload: [{
orderStatus: 'OPEN',
orderId: 1
}]
};
afterAll(() => {
jest.resetModules();
navSpy.mockRestore();
});
it('should get all orders', (result) => {
const tester = new SagaTester({
initialState: {
orders: {
'OPEN': {
orderId: 1
}
}
}
});
tester.start(saga);
tester.dispatch(action);
// ... do your expects here
// You may need to use `tester.waitFor` to wait for an action to be dispatched
// If you want to expect a certain action was called, use `getCalledActions`
});
推荐阅读
- xslt - 如何在 xslt 中使用 format-number 格式化数字
- java - 如何更改 MapStruct 生成的 *.java 文件的位置?
- python - Type(1,) 返回 int 预期元组
- angular - Angular:在 Angular 中获取我当前激活的路由的请求标头选项
- sql - SQLite 中的 CEIL 和 FLOOR
- javascript - 我的代码调用的 JQuery 事件(带选择器)
- python - Python glob:对格式[int]_[int]的文件进行排序,与windows名称排序相同
- reactjs - 使用 WebStorm 调试具有非标准结构的 NextJS 项目:“附加调试器。> 找不到页面目录”?
- angular - 使用 AWS Amplify 将文件上传到 S3 时获取上传进度状态
- python - Python RE re.split(),结果以空字符串开头