Any tip for testing?
See original GitHub issueI’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:
- 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:
- Created 4 years ago
- Reactions:1
- Comments:36 (16 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
And this is an idea of my new test
Tnx for the ideas guys
we have a testing section now: https://react-hook-form.com/advanced-usage/#TestingForm probably worth to have read