Writing tests
Last updated
import React from 'react'
import { render, waitFor } from '@testing-library/react'
import user from '@testing-library/user-event'
import Input from '../Input'
describe('Input', () => {
test('Should render', () => {
const { getByLabelText } = render(<Input name="user" />)
expect(getByLabelText('user')).toBeInTheDocument()
expect(getByLabelText('user').classList.contains('c-Input')).toBe(true)
})
// Example of a test with user interaction
test('Should fire on change event when typing', async () => {
const spy = jest.spy()
const { getByLabelText } = render(<Input name="user" onChange={spy} />)
const input = getByLabelText('user')
user.type(input, 'hola')
// You don't always need to "waitFor" after the event simulation
await waitFor(() => {
expect(input.value).toBe('hola')
expect(spy).toHaveBeenCalled()
})
})
})npm run devnpm run testhsds-react/
└── coverage/
└── lcov-report/
└── index.html