import * as React from 'react'
import { render } from '../utils/test-utils'
import { ComboboxTree } from '.'
import userEvent from '@testing-library/user-event'
describe('Combobox', () => {
it('should render the combobox', () => {
const { getByRole } = render()
expect(getByRole('combobox')).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('combobox')).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 prevent clearing selection when clearable is false', async () => {
const { getByLabelText, queryByRole } = render(
)
expect(getByLabelText('My input')).not.toBeRequired()
await userEvent.click(getByLabelText('My input'))
expect(
queryByRole('option', { name: /No selection/ })
).not.toBeInTheDocument()
})
it('should allow clearing selection when withAsterisk is provided', async () => {
const { getByLabelText, queryByRole } = render(
)
expect(getByLabelText('My input')).toBeRequired()
await userEvent.click(getByLabelText('My input'))
expect(
queryByRole('treeitem', { name: /No selection/ })
).toBeInTheDocument()
})
it('should open list on label click', async () => {
const { getByText, queryByRole } = render(
)
await userEvent.click(getByText('My input'))
expect(queryByRole('combobox')).toHaveFocus()
expect(queryByRole('tree')).toBeInTheDocument()
})
it('should not open list on label click if input is disabled', async () => {
const { getByText, queryByRole } = render(
)
await userEvent.click(getByText('My input'), {
pointerEventsCheck: 0,
})
expect(queryByRole('combobox')).not.toHaveFocus()
expect(queryByRole('tree')).not.toBeInTheDocument()
})
it('should render error state correctly', () => {
const { getByRole } = render(
)
expect(
getByRole('combobox', {
name: 'My input',
description: 'This is an error',
})
).toBeInTheDocument()
})
it('should render warning state correctly', () => {
const { getByRole } = render(
)
expect(
getByRole('combobox', {
name: 'My input',
description: 'This is a warning',
})
).toBeInTheDocument()
})
})