我想使用 @testing-library/react-hooks 中的 renderHook 来测试组件的状态更新,这让我们可以像在 React 功能组件中一样渲染钩子。只是想知道,这是否仅适用于自定义挂钩,因为当我尝试使用此方法来测试组件时状态没有改变
it('test count update', () => {
const { result } = renderHook(() => useState({ count: 0 }));
const [state, setState] = result.current;
const wrapper = mount(<ComponentToTest />);
act(() => {
wrapper.find('button').simulate('click');
})
expect(state).toBe({ count: 1 });
})
Run Code Online (Sandbox Code Playgroud)
由于计数未更新且仍为 0,因此出现错误
有人可以帮忙吗
