提交表单时,我将用户重定向到另一条路线。
我正在使用反应测试库,它抱怨:
Cannot read property 'push' of undefined
即使我已经将组件包装在MemoryRouter
我还尝试了BrowserRoter
,HashRouter
,但仍然存在相同的问题...如果不是push
就是他想念的history
。
有什么想法吗?
test('creates a reminder when submiting the form if all fields are filled', async () => {
const { getByTestId } = render(
<BrowserRouter>
<ReminderProvider>
<AddReminder />
</ReminderProvider>
</BrowserRouter>
)
waitFor(() => expect(getByTestId('categories-menu')).toBeTruthy())
waitFor(() => fireEvent.change(getByTestId('categories-menu'), { target: { value: categories[0].id } }))
waitFor(() => fireEvent.change(getByTestId('providers-menu'), { target: { value: providers[0].company.companyName } }))
fireEvent.change(getByTestId('title'), { target: 'My reminder' })
waitFor(() => fireEvent.change(getByTestId('date'), { target: '04/01/2020' }))
fireEvent.change(getByTestId('notice-period'), { target: 2 })
fireEvent.click(getByTestId('submit'));
waitFor(() => expect(getByTestId('overview')).toBeTruthy())
})
})
在我的组件中:
const onSubmit = e => {
e.preventDefault()
history.push('/overview')
createReminder()
}