import userEvent from '@testing-library/user-event'
import * as React from 'react'
import renderWithTheme from '../../tests/helpers/renderWithTheme'
import TextInputField from './TextInputField'
describe('component: TextInputField', () => {
test('Should render a disabled TextInputField', () => {
const { getByLabelText } = renderWithTheme(
)
expect(getByLabelText('Come on, type something')).toBeDisabled()
})
test('Should render a TextInputField with a placeholder', () => {
const { getByPlaceholderText } = renderWithTheme(
)
expect(getByPlaceholderText(`I won't disable it again, promise`)).toBeInTheDocument()
})
test('Should render a success TextInputField', () => {
const { getByText, getByLabelText } = renderWithTheme(
)
expect(
getByLabelText('No seriously, type something').getAttribute('aria-describedby')
).toContain(getByText('Great! you typed something!').id)
})
test('Should render a warning TextInputField', () => {
const { getByText, getByLabelText } = renderWithTheme(
)
expect(getByLabelText('Do it again').getAttribute('aria-describedby')).toContain(
getByText(`Really? That's all you got?`).id
)
})
test('Should render an error TextInputField', () => {
const { getByText, getByLabelText } = renderWithTheme(
)
expect(getByLabelText('Try again').getAttribute('aria-describedby')).toContain(
getByText(`That's it, we're done here`).id
)
})
test('Should support margin space props', () => {
const { getByTestId } = renderWithTheme(
<>
>
)
const blank = { marginTop: '', marginRight: '', marginBottom: '', marginLeft: '' }
expect(getByTestId('first').parentElement?.parentElement).toHaveStyle(blank)
expect(getByTestId('default').parentElement?.parentElement).toHaveStyle({
...blank,
marginTop: '16px',
marginBottom: '4px',
})
expect(getByTestId('override').parentElement?.parentElement).toHaveStyle({
...blank,
marginTop: '2px',
})
})
test('Should support flex item props', () => {
const { container } = renderWithTheme(
)
expect(container.firstElementChild).toHaveStyle('flex: 1')
expect(container.firstElementChild).toHaveStyle('flex-grow: 1')
expect(container.firstElementChild).toHaveStyle('flex-shrink: 0')
expect(container.firstElementChild).toHaveStyle('flex-basis: 0')
})
test('Should support ref prop', () => {
const ref = React.createRef()
const { getByLabelText } = renderWithTheme(
)
expect(getByLabelText('Referred')).toBe(ref.current)
expect(ref.current).toHaveValue('with-ref')
})
test('Should trigger onChange handler', () => {
const onChange = jest.fn()
const { getByPlaceholderText } = renderWithTheme(
)
userEvent.type(getByPlaceholderText('Type here!'), "Alright fine I'm typing, jeez")
expect(onChange).toHaveBeenCalled()
})
})