import Field from '../Field'; import { mockMatchMedia, render, screen, fireEvent, userEvent } from '../test-utils'; import { wait } from '../test-utils/wait'; import Typeahead from './Typeahead'; mockMatchMedia(); const scrollIntoView = jest.fn(); describe('Typeahead', () => { beforeEach(() => { HTMLElement.prototype.scrollIntoView = scrollIntoView; scrollIntoView.mockClear(); }); it('supports `Field` for labeling', () => { render( {}} /> , ); expect(screen.getAllByRole('group')[0]).toHaveAccessibleName(/^Tags/); }); describe('when no options are provided', () => { it('does not render a dropdown when no options and no footer are provided', () => { render( {}} /> , ); expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); }); it('does render a dropdown when only a footer is provided', () => { render( hello

} onChange={() => {}} />
, ); expect(screen.getByRole('listbox')).toBeInTheDocument(); }); }); it('renders input with placeholder', () => { render( {}} />, ); expect(screen.getByPlaceholderText('Type here')).toBeInTheDocument(); }); it('renders chips when multiple is true and selected has items', () => { const initialValue = [{ label: 'Chip 1' }, { label: 'Chip 2' }]; render( {}} />, ); expect(screen.getByText('Chip 1')).toBeInTheDocument(); expect(screen.getByText('Chip 2')).toBeInTheDocument(); }); it('calls onChange when selecting an option', () => { const onChange = jest.fn(); render( , ); const input = screen.getByRole('combobox'); fireEvent.change(input, { target: { value: 'Option 1' } }); fireEvent.click(screen.getByText('Option 1')); expect(onChange).toHaveBeenCalled(); }); it('scrolls options into view when navigating with arrow keys', async () => { render( {}} />, ); const input = screen.getByRole('combobox'); await userEvent.click(input); await userEvent.keyboard('{ArrowDown}{ArrowDown}'); expect(input).toHaveFocus(); expect(scrollIntoView).toHaveBeenCalledTimes(2); expect(scrollIntoView).toHaveBeenNthCalledWith(1, { block: 'nearest' }); expect(scrollIntoView).toHaveBeenNthCalledWith(2, { block: 'nearest' }); expect(scrollIntoView.mock.contexts[0]).toBe(screen.getByText('Option 1').closest('li')); expect(scrollIntoView.mock.contexts[1]).toBe(screen.getByText('Option 2').closest('li')); }); it('uses one Tab stop for the suggestions', async () => { render( <> {}} /> , ); const input = screen.getByRole('combobox'); await userEvent.click(input); await userEvent.tab(); expect(screen.getByRole('listbox')).toHaveFocus(); await userEvent.keyboard('{ArrowDown}'); expect(screen.getByRole('option', { name: 'Option 1' })).toHaveClass( 'tw-dropdown-item--focused', ); expect(screen.getByRole('listbox')).toHaveFocus(); await userEvent.tab(); expect(screen.getByRole('button', { name: 'Next control' })).toHaveFocus(); }); it('uses the active option style when hovering a suggestion', async () => { render( {}} />, ); await userEvent.click(screen.getByRole('combobox')); await userEvent.hover(screen.getByRole('option', { name: 'Option 2' })); expect(screen.getByRole('option', { name: 'Option 2' })).toHaveClass( 'tw-dropdown-item--focused', ); }); it('shows clear button when there is a value', () => { render( {}} />, ); expect(screen.getByRole('button', { name: /clear/i })).toBeInTheDocument(); }); it('clears value when clear button is clicked', () => { const onChange = jest.fn(); render( , ); fireEvent.click(screen.getByRole('button', { name: /clear/i })); expect(onChange).toHaveBeenCalledWith([]); }); it('shows InlinePrompt when alert prop is provided', () => { render( {}} />, ); expect(screen.getByText('Something went wrong')).toBeInTheDocument(); }); it('calls onFocus when input is focused', () => { const onFocus = jest.fn(); render( {}} onFocus={onFocus} />); fireEvent.focus(screen.getByRole('combobox')); expect(onFocus).toHaveBeenCalled(); }); it('calls onInputChange when input value changes', () => { const onInputChange = jest.fn(); render( {}} onInputChange={onInputChange} />, ); fireEvent.change(screen.getByRole('combobox'), { target: { value: 'abc' } }); expect(onInputChange).toHaveBeenCalledWith('abc'); }); it('calls onSearch when input value changes', async () => { const onSearch = jest.fn(); render( {}} onSearch={onSearch} />, ); fireEvent.change(screen.getByRole('combobox'), { target: { value: 'abc' } }); // wait for debounce await wait(500); expect(onSearch).toHaveBeenCalledWith('abc'); }); it('adds a new chip when allowNew and multiple are true and separator is pasted', () => { const onChange = jest.fn(); render( , ); const input = screen.getByRole('combobox'); fireEvent.paste(input, { clipboardData: { getData: () => 'foo,bar', }, }); expect(onChange).toHaveBeenCalledWith( expect.arrayContaining([{ label: 'foo' }, { label: 'bar' }]), ); }); describe('when disabled', () => { it('disables the input', () => { render( {}} />); expect(screen.getByRole('combobox')).toBeDisabled(); }); it('does not show the clear button when there is a value', () => { render( {}} />, ); expect(screen.queryByRole('button', { name: /clear/i })).not.toBeInTheDocument(); }); it('keeps chips but does not let them be removed', () => { render( {}} />, ); expect(screen.getByText('Chip').closest('.np-chip')).toHaveClass('disabled'); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('does not open the suggestions when the input is clicked', async () => { render( {}} />, ); await userEvent.click(screen.getByRole('combobox')); expect(screen.getByRole('combobox')).toHaveAttribute('aria-expanded', 'false'); }); it('closes the open menu when it becomes disabled', async () => { const onChange = jest.fn(); const props = { id: 'test', name: 'test', options: [], allowNew: true, minQueryLength: 0, onChange, }; const { rerender } = render(); await userEvent.type(screen.getByRole('combobox'), 'New thing'); rerender(); await userEvent.click(document.body); expect(onChange).not.toHaveBeenCalled(); }); }); });