import React from 'react' import { snapshot, render } from '../utils/test-utils' import { NaturalLanguageSelect } from './select' import userEvent from '@testing-library/user-event' describe('NaturalLanguageSelect', () => { describe('snapshots', () => { snapshot( 'basic', ) }) describe('rendering', () => { it('should render a flat list of options', async () => { const { getByRole, findByRole, findAllByRole } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) expect( await findByRole('listbox', { name: 'Team Options' }) ).toBeInTheDocument() expect(await findAllByRole('option')).toHaveLength(4) }) it('should render a groups of options', async () => { const { getByRole, findAllByRole, findByRole } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) expect( await findByRole('listbox', { name: 'Team Options' }) ).toBeInTheDocument() expect(await findAllByRole('option')).toHaveLength(6) expect( await findByRole('group', { name: 'Internal Teams' }) ).toBeInTheDocument() expect( await findByRole('group', { name: 'External Teams' }) ).toBeInTheDocument() }) it('should render search results', async () => { const { getByRole, findByRole, findAllByRole, findByText } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) expect( await findByRole('listbox', { name: 'Team Options' }) ).toBeInTheDocument() expect(await findAllByRole('option')).toHaveLength(4) expect( await findByText(/Please refine your search/) ).toBeInTheDocument() }) it('should render search results not found', async () => { const { getByRole, findByText } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) expect(await findByText(/No matches found/)).toBeInTheDocument() }) }) describe('interaction', () => { it('should enable selecting options from the list', async () => { const onChange = jest.fn() const { getByRole, findByRole } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) await userEvent.click( await findByRole('option', { name: 'Team 1' }) ) expect(onChange).toHaveBeenCalledWith({ value: 1, label: 'Team 1' }) }) it('should enable controlling expanded state of groups', async () => { const onExpandedChange = jest.fn() const { getByRole, findAllByRole } = render( ) await userEvent.click( getByRole('combobox', { name: 'Team Options' }) ) expect(await findAllByRole('option')).toHaveLength(3) await userEvent.click( getByRole('button', { name: 'Internal Teams' }) ) expect(onExpandedChange).toHaveBeenCalledWith(true) }) }) })