Jac*_*cob 5 javascript unit-testing reactjs jestjs react-native
我有一个在渲染时多次调用 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 调用的返回值而不更改调用的钩子的顺序。任何帮助表示赞赏。
多次调用mockReturnValueOnce是不行的,因为顺序可能会改变。useSelector无论调用多少次,下面的代码都只会返回存根状态。
import * as redux from 'react-redux'
const user = {
id: 1,
name: 'User',
}
const state = { user }
jest
.spyOn(redux, 'useSelector')
.mockImplementation((callback) => callback(state))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6097 次 |
| 最近记录: |