Created
April 7, 2021 22:06
-
-
Save leandrocrs/8c9f29861df866800f9025a024dda720 to your computer and use it in GitHub Desktop.
react-input-mask + user-events + @testing-library/react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import userEvent from '@testing-library/user-event'; | |
import TestUtils from 'react-dom/test-utils'; | |
function changeInputMaskValue(element: HTMLInputElement, value: string) { | |
element.value = value; | |
element.selectionStart = element.selectionEnd = value.length; | |
TestUtils.Simulate.change(element); | |
}; | |
it('example test', async () => { | |
render( | |
<MyComponent | |
amount="100.00" | |
/>, | |
); | |
act(() => { | |
// Both lines of codes are required | |
userEvent.type(screen.getByLabelText('Amount'), '300'); | |
changeInputMaskValue(screen.getByLabelText('Amount') as HTMLInputElement, '300'); | |
}); | |
act(() => { | |
// Do not move the form submitting to the previous `act`, it must be in two | |
// separate `act` calls. | |
userEvent.click(screen.getByText('Next')); | |
}); | |
// You must use `findByText` | |
const error = await screen.findByText(/\$100.00 to redeem/); | |
expect(error).toBeInTheDocument(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment