You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having problems testing a component that uses react-hook-form. My component includes a submit button (FormSubmitButton) which, when clicked, should call a submit function.
In my test, I'm simulating a click on the submit button and then waiting for a mock function (useJobConfigsUpdateMock.__muteAsync) to be called. Here is the relevant code from my test:
it(`and the cron expression is NOT defined then updateJob is called`, async () => {
const jobConfig = {
id: '1',
projectId: 1,
jobType:....
}
render(<JobConfigEditorModal jobConfig={jobConfig} />, initializeStateFn(1))
const checkbox = await screen.findByTestId('isPaused')
fireEvent.click(checkbox)
const submitButton = await screen.findByTestId('form-submit-button')
await waitFor(() => expect(submitButton).not.toBeDisabled())
// Simulate the click event
fireEvent.click(submitButton)
// Wait for any async actions to complete
waitFor(() => expect(useJobConfigsUpdateMock.__mutateAsync).toHaveBeenCalledTimes(1))
expect(useJobConfigsUpdateMock.__mutateAsync).toHaveBeenCalledWith(
expect.objectContaining({ ...jobConfig, jobData: JSON.stringify(jobConfig.jobData) })
)
})
The problem is that, although the submit button is clicked in the test, the useJobConfigsUpdateMock.__muteAsync function is never called. I've checked that the submit button isn't disabled before clicking it, and I've tried using waitFor to wait for any asynchronous actions to complete before checking if useJobConfigsUpdateMock.__mutateAsync has been called, but nothing seems to work.
In addition, I've added some console.log statements in my FormSubmitButton component to try to debug the problem. Here is the relevant code from that component:
When I run my test, I see the console messages ‘Button clicked’ and ‘yeeeee [AsyncFunction (anonymous)]’, indicating that the button is being clicked and the handleSubmit(submit) function is being called. However, I don't see the console message ‘handleSubmit called with:’, which should be printed at the beginning of the submit function. This suggests that the submit function is not being called as expected.
Does anyone have any idea why this might be happening and how I can fix it?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm having problems testing a component that uses react-hook-form. My component includes a submit button (FormSubmitButton) which, when clicked, should call a submit function.
Here is the relevant code from my component:
And here is the submit function:
In my test, I'm simulating a click on the submit button and then waiting for a mock function
(useJobConfigsUpdateMock.__muteAsync)
to be called. Here is the relevant code from my test:The problem is that, although the submit button is clicked in the test, the
useJobConfigsUpdateMock.__muteAsync
function is never called. I've checked that the submit button isn't disabled before clicking it, and I've tried using waitFor to wait for any asynchronous actions to complete before checking ifuseJobConfigsUpdateMock.__mutateAsync
has been called, but nothing seems to work.In addition, I've added some console.log statements in my FormSubmitButton component to try to debug the problem. Here is the relevant code from that component:
When I run my test, I see the console messages ‘Button clicked’ and ‘yeeeee [AsyncFunction (anonymous)]’, indicating that the button is being clicked and the handleSubmit(submit) function is being called. However, I don't see the console message ‘handleSubmit called with:’, which should be printed at the beginning of the submit function. This suggests that the submit function is not being called as expected.
Does anyone have any idea why this might be happening and how I can fix it?
Thanks in advance for any help you can provide.
Beta Was this translation helpful? Give feedback.
All reactions