import * as React from 'react'
import { render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import Input, { InputNumeric, InputTime } from './'
describe('Input', () => {
it('should render the input', () => {
const { getByRole } = render()
expect(getByRole('textbox')).toBeInTheDocument()
})
it('should render with a label when provided', () => {
const { getByLabelText } = render()
expect(getByLabelText('My input')).toBeInTheDocument()
})
it('should focus the input when clicking the label', async () => {
const { container, getByRole } = render()
await userEvent.click(
container.querySelector('label')!
)
expect(getByRole('textbox')).toHaveFocus()
})
it('should add aria-required when withAsterisk is provided', () => {
const { getByLabelText } = render(
)
expect(getByLabelText('My input')).toBeRequired()
})
it('should add aria-required when label is required', () => {
const { getByLabelText } = render(
)
expect(getByLabelText('My input')).toBeRequired()
})
it('should add aria-required when label has requiredTooltip', () => {
const { getByLabelText } = render(
)
expect(getByLabelText('My input')).toBeRequired()
})
it('should be described by the error', () => {
const { getByRole } = render(
)
expect(
getByRole('textbox', {
name: 'My input',
description: 'This is an error',
})
).toBeInTheDocument()
})
it('should be described by the warning', () => {
const { getByRole } = render(
)
expect(
getByRole('textbox', {
name: 'My input',
description: 'This is a warning',
})
).toBeInTheDocument()
})
describe('counter', () => {
it('should not show a counter without maxLength', () => {
const { queryByText } = render(
)
expect(queryByText('/')).not.toBeInTheDocument()
})
it('should show a counter when maxLength is provided', () => {
const { getByText } = render(
)
expect(getByText('0/25')).toBeInTheDocument()
})
it('should not show a counter when limit is provided and value is not near limit', () => {
const { queryByText } = render(
)
expect(queryByText('0/25')).not.toBeInTheDocument()
})
it('should show a counter when limit is provided and value is near limit', () => {
const { getByText } = render(
)
expect(getByText('22/25')).toBeInTheDocument()
})
it('should not allow exceeding limit by default', async () => {
const { getByText, getByRole } = render(
)
await userEvent.keyboard('{Tab}')
await userEvent.keyboard(
"This is something I'm typing that is quite long"
)
expect(getByText('25/25')).toBeInTheDocument()
expect(getByRole('textbox', { name: 'My input' })).toHaveValue(
"This is something I'm typ"
)
})
it('should allow exceeding limit when configured', async () => {
const { getByText, getByRole } = render(
)
await userEvent.keyboard('{Tab}')
await userEvent.keyboard(
"This is something I'm typing that is quite long"
)
expect(getByText('47/25')).toBeInTheDocument()
expect(getByRole('textbox', { name: 'My input' })).toHaveValue(
"This is something I'm typing that is quite long"
)
})
it('should show an accurate counter when using default value', async () => {
const { getByText } = render(
)
expect(getByText('4/25')).toBeInTheDocument()
await userEvent.keyboard('{Tab}Another')
expect(getByText('7/25')).toBeInTheDocument()
})
it('should show an accurate counter when using value', async () => {
const { getByText } = render(
)
expect(getByText('4/25')).toBeInTheDocument()
await userEvent.keyboard('{Tab}Another')
expect(getByText('4/25')).toBeInTheDocument()
})
})
})
describe('InputNumeric', () => {
it('should only contain appropriate characters', async () => {
const { getByLabelText } = render(
)
await userEvent.keyboard('{Tab}An23other')
expect(getByLabelText('My input')).toHaveValue('23')
})
})
describe('InputTime', () => {
it('should only contain appropriate characters', async () => {
const { getByLabelText } = render(
)
await userEvent.keyboard('{Tab}An23ot:10her')
expect(getByLabelText('My input')).toHaveValue('23:10')
})
})
describe('label interaction', () => {
it('should focus input when label is clicked', async () => {
const { getByLabelText, getByText } = render(
)
await userEvent.click(getByText('My input'))
expect(getByLabelText('My input')).toHaveFocus()
})
it('should not focus input when label is clicked if disabled', async () => {
const { getByLabelText, getByText } = render(
)
await userEvent.click(getByText('My input'), {
pointerEventsCheck: 0,
})
expect(getByLabelText('My input')).not.toHaveFocus()
})
})