【发布时间】:2021-03-13 13:41:35
【问题描述】:
我正在将一个类组件重构为一个功能组件。
在我的测试文件中,我使用了酶,但我正在迁移到 react-testing-library
这是我用酶做的测试
it('should change module when clicked button login', () => {
const wrapper = mount(<SignUp setModuleCallback={jest.fn()} />)
const instance = wrapper.instance()
jest.spyOn(instance, 'setModule')
wrapper.find('button#login-button').simulate('click')
expect(instance.setModule).toHaveBeenCalled()
})
这就是我正在尝试使用 react-testing-library 做的事情
it('should change module when clicked button login', async () => {
const { getByTestId } = render(<SignUp setModuleCallback={jest.fn()} />)
const instance = getByTestId('submit')
jest.spyOn(instance, 'setModule')
const button = await waitFor(() => getByTestId('submit'))
act(() => {
fireEvent.click(button)
})
expect(instance.setModule).toHaveBeenCalled()
})
这是我遇到的错误
【问题讨论】:
-
React 测试库专注于测试 DOM 节点。因此,您的测试将使用实际的 DOM 节点,而不是处理渲染的 React 组件的实例。
标签: reactjs enzyme react-testing-library