Skip to content

Instantly share code, notes, and snippets.

@vercetti11
Created November 5, 2020 15:06
Show Gist options
  • Save vercetti11/c4c6318705dbaf9fab39f02d81dd4df6 to your computer and use it in GitHub Desktop.
Save vercetti11/c4c6318705dbaf9fab39f02d81dd4df6 to your computer and use it in GitHub Desktop.
import React from 'react'
import {render, screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import faker from 'faker'
import Login from '../../components/login'
function buildLoginForm(overrides) {
return {
username: faker.internet.userName(),
password: faker.internet.password(),
...overrides,
}
}
test('submitting the form calls onSubmit with username and password', () => {
const handleSubmit = jest.fn()
render(<Login onSubmit={handleSubmit} />)
const {username, password} = buildLoginForm()
userEvent.type(screen.getByLabelText(/username/i), username)
userEvent.type(screen.getByLabelText(/password/i), password)
userEvent.click(screen.getByRole('button', {name: /submit/i}))
expect(handleSubmit).toHaveBeenCalledWith({
username,
password,
})
expect(handleSubmit).toHaveBeenCalledTimes(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment