Skip to content

Instantly share code, notes, and snippets.

@leandrocrs
Created April 7, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandrocrs/8c9f29861df866800f9025a024dda720 to your computer and use it in GitHub Desktop.
Save leandrocrs/8c9f29861df866800f9025a024dda720 to your computer and use it in GitHub Desktop.
react-input-mask + user-events + @testing-library/react
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