我有一个在渲染时多次调用 useSelector 的函数。在模拟选择器时,我使用了:
jest.spyOn(Redux, 'useSelector').mockReturnValueOnce(data).mockReturnValueOnce(moreData);
Run Code Online (Sandbox Code Playgroud)
这改变了我的组件中调用钩子的顺序。我还尝试创建一个模拟商店并将其发送到测试中的渲染组件中,如下所示:
const state = { userGuid: 'testGuid', user };
const store = mockStore(state);
jest.spyOn(Redux, 'useSelector').mockImplementation(() => store.getState());
const { getByTestId } = wrappedRender(ProfileScreen, mockProps, { store });
Run Code Online (Sandbox Code Playgroud)
但这将数据包装在一个额外的对象中,我的组件无法对其进行解构。
截至目前,我无法找到任何其他方法来模拟多个 useSelector 调用的返回值而不更改调用的钩子的顺序。任何帮助表示赞赏。