Skip to content

Instantly share code, notes, and snippets.

@jcarroll2007
Created November 10, 2021 13:28
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 jcarroll2007/237f082a28f54db506addd4141a667bd to your computer and use it in GitHub Desktop.
Save jcarroll2007/237f082a28f54db506addd4141a667bd to your computer and use it in GitHub Desktop.
Creating scalable, testable, and readable React apps: Part 3 Test examples
/**
* Component Test
*/
describe('CharacterCreateForm', () => {
it('should not submit form and show error message if name is null', async () => {
const onSubmitMock = jest.fn()
const { getByTestId, queryByTestId } = render(
<CharacterCreateForm onSubmit={onSubmitMock} />
)
const submitButton = getByTestId('form-submit-button')
expect(queryByTestId('name-error-message')).not.toBeInTheDocument()
userEvent.click(submitButton)
expect(onSubmitMock).not.toHaveBeenCalled()
expect(getByTestId('name-error-message')).toBeInTheDocument()
})
})
/**
* Container Test
*/
describe('CharacterListContainer', () => {
beforeEach(() => mock.setup());
afterEach(() => mock.teardown());
it('fetches information about characters on render', async () => {
const mockGetCharacters = jest.fn((_, res) => {
return res.status(200).body(mockedCharacters);
});
mock.get('/characters', mockGetCharacters);
renderConnectedComponent(<CharacterListContainer createCharacterPath="/characters/new" />);
await waitFor(() => expect(mockGetCharacters).toHaveBeenCalledTimes(1));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment