import * as React from 'react'
import 'jest-styled-components'
import { render } from '@testing-library/react'
import Field from './'
import { TestingProvider } from '@planview/pv-utilities'
import userEvent from '@testing-library/user-event'
describe('Field', () => {
it('Aria attributes should be applied on input', () => {
const { getByRole } = render(
{(props) => }
)
expect(
getByRole('textbox', {
name: 'my label',
description: 'error message',
})
).toBeInTheDocument()
})
it('should use htmlFor when rendering with a label element', () => {
const { getByText, getByLabelText } = render(
{(props) => }
)
expect(getByText('my label')).toHaveAttribute('for')
expect(getByLabelText('my label')).toBeInTheDocument()
})
it('should not use htmlFor when rendering with a span element', () => {
const { getByText } = render(
{(props) => }
)
expect(getByText('my label')).not.toHaveAttribute('for')
})
it('should render label on the left when there is enough room', () => {
const { container } = render(
{(props) => }
)
expect(container.firstChild).toHaveStyleRule(
'grid-template-columns',
'160px auto'
)
})
it('should render label on the top when there is not enough room to show it on the left', () => {
const { container } = render(
{(props) => }
)
expect(container.firstChild).not.toHaveStyleRule(
'grid-template-columns',
'160px auto'
)
})
it('should focus the input when clicking the label', async () => {
const onLabelClick = jest.fn()
const { container, getByRole } = render(
{(props) => }
)
await userEvent.click(
container.querySelector('label')!
)
expect(getByRole('textbox')).toHaveFocus()
expect(onLabelClick).toHaveBeenCalled()
})
})