question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any tip for testing?

See original GitHub issue

I’m using react-scripts and material-ui and my code is something like

const Form = ({onSubmit}) => {
 const { register, handleSubmit, errors } = useForm()
 return (
  <form onSubmit={handleSubmit(onSubmit)}>
     <TextField {...} inputRef={register} />
     .....
      <Button type="submit" />
  </form>
}

i want to test the form doing something like

describe('Form Component', () => {
 it('should submit when data filled', () => {
    const onSubmit = jest.fn()
    const component = mount(<Form onSubmit={onSubmit} />)
    const input = component.find(TextField).at(1)
    input.simulate('change', { target: { value: 'test'} })
    component.find(Button).first().simulate('submit')
    expect(onSubmit).toBeCalled()
 })
})

this throw me 2 errors:

  1. onSubmit is not called
 Attempted to log "Warning: An update to Form inside a test was not wrapped in act(...).
  ....

    When testing, code that causes React state updates should be wrapped into act(...):

    act(() => {
      /* fire events that update state */
    });
....

i put every simulate in an act function and does not work.

Any tip is welcome

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:36 (16 by maintainers)

github_iconTop GitHub Comments

25reactions
julian-linuxcommented, Nov 19, 2019

I have a solution. I had to change to react-testing-library for this. Enzyme does not work as expected.

This is an idea of my form… and i added some props for react-testing-library

const Form = ({onSubmit}) => {
 const { register, handleSubmit, errors } = useForm()
 return (
  <form onSubmit={handleSubmit(onSubmit)} data-testid='form'>
     <TextField 
        {...}
        inputRef={register} 
        inputProps={{
          'aria-label': 'input'
        }}/>
     .....
      <Button type="submit" />
  </form>
}

And this is an idea of my new test

describe('Form Component', () => {
 it('should submit when data filled', async () => {
  const onSubmit = jest.fn()
  const { getByLabelText, getByTestId } = render(<Form onSubmit={onSubmit} />)
  const input = getByLabelText('input')
  await act(async () => {
    await fireEvent.change(input, { target: { value: '123' } })
  })
  await act(async () => {
      fireEvent.submit(getByTestId('form'))
  })
  expect(onSubmit).toBeCalled()
 })
})

Tnx for the ideas guys

14reactions
bluebill1049commented, Aug 10, 2020

we have a testing section now: https://react-hook-form.com/advanced-usage/#TestingForm probably worth to have read

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test-Taking Tips (for Teens) - Nemours KidsHealth
First, be sure you've studied properly. · Get enough sleep the night before the test. · Listen closely to any instructions. · Read...
Read more >
Test Taking Tips | Central Methodist University
Do the homework assignments daily. · Write down all hard to remember formulas, equations, and rules as soon as you get the test....
Read more >
23 Clever Test-Taking Tips To ACE Your Exams
1. Immunise yourself ... One of the most important tips for test-taking success is to: Take a vaccine against exam day nerves by...
Read more >
20 Testing Experts Share Their Best Test-Taking Tips
8. Make sure you bring all required materials. · 9. Don't bring your cell phone into the exam room. · 10. Use the...
Read more >
Test Taking Tips
Before the Test Tips. 1. Get a good night's sleep and eat a high protein breakfast. Drink plenty of water. 2. Practice guided...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found