首页 > 解决方案 > 从 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 } }

标签: reactjsunit-testingreduxredux-saga

解决方案


我会尝试使用redux-saga-testerhttps ://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`
  });

推荐阅读