【发布时间】:2021-01-18 08:15:39
【问题描述】:
我有一个在渲染时多次调用 useSelector 的函数。在模拟我使用的选择器时:
jest.spyOn(Redux, 'useSelector').mockReturnValueOnce(data).mockReturnValueOnce(moreData);
这改变了我的组件中调用钩子的顺序。我也尝试过创建一个模拟商店并将其发送到测试中的渲染组件中:
const state = { userGuid: 'testGuid', user };
const store = mockStore(state);
jest.spyOn(Redux, 'useSelector').mockImplementation(() => store.getState());
const { getByTestId } = wrappedRender(ProfileScreen, mockProps, { store });
但这会将数据包装在我的组件无法解构的额外对象中。
到目前为止,我找不到任何其他方法来模拟多个 useSelector 调用的返回值,而无需更改调用的钩子的顺序。任何帮助表示赞赏。
【问题讨论】:
标签: javascript reactjs react-native unit-testing jestjs