import React, { createRef, useState } from 'react'; import { act, fireEvent, render, RenderResult, waitFor, waitForElementToBeRemoved, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import BeakerIcon from '@leafygreen-ui/icon/dist/Beaker'; import * as LeafyGreenProviderModule from '@leafygreen-ui/leafygreen-provider'; import { PopoverProvider } from '@leafygreen-ui/leafygreen-provider'; import { keyMap } from '@leafygreen-ui/lib'; import { RenderMode } from '@leafygreen-ui/popover'; import { Context, jest as Jest } from '@leafygreen-ui/testing-lib'; import { getTestUtils, type GetTestUtilsReturnType } from '../testing'; import { Option, OptionGroup, Select } from '..'; import { SelectProps, State } from './Select.types'; const Color = { Red: 'Explicit value: Red', Blue: 'Blue', Green: 'Green', Yellow: 'Yellow', Orange: 'Orange', Indigo: 'Indigo', Violet: 'Violet', } as const; const errorMessage = 'This is the error message'; const enabledOptions = ['Select', 'Red', 'Blue', 'Green', 'Yellow'] as const; const defaultProps = { label: 'Label', name: 'pet', description: 'Description', children: [ , , , , <> , ], } as const; function Controller({ children, initialValue, }: { children: ( value: string, setValue: (value: React.SetStateAction) => void, ) => React.ReactNode; initialValue: string; }) { const [value, setValue] = useState(initialValue); return <>{children(value, setValue)}; } function renderSelect(props = {}) { const renderUtils = render(); }; return { ...renderUtils, ...utils, rerenderSelect, }; } describe('packages/select', () => { test('renders label and description', () => { const { getLabel, getDescription } = renderSelect(); expect(getLabel()).toBeVisible(); expect(getDescription()).toBeVisible(); }); test('renders placeholder', async () => { const { getInput, getOptionByValue, rerenderSelect } = renderSelect(); expect(getInput()).toHaveTextContent('Select'); rerenderSelect({ placeholder: 'Explicit placeholder' }); expect(getInput()).toHaveTextContent('Explicit placeholder'); userEvent.click(getInput()); expect(getOptionByValue('Explicit placeholder')).toBeInTheDocument(); }); test('accepts a ref', () => { const ref = createRef(); const { container } = render(', () => { Context.within(Jest.spyContext(console, 'error'), spy => { spy.mockImplementation(); expect(() => { render(); }).toThrow('`Option` must be a child of a `Select` instance'); expect(() => { render( , ); }).toThrow('`OptionGroup` must be a child of a `Select` instance'); }); }); test('console errors if missing label, aria-label, or aria-labelledby', () => { Context.within(Jest.spyContext(console, 'error'), spy => { spy.mockImplementation(); render( // @ts-ignore React18-expect-error - label missing , ); expect(console.error).toHaveBeenCalled(); }); }); test('does not console error when one of: label, aria-label, or aria-labelledby is set', () => { Context.within(Jest.spyContext(console, 'error'), spy => { spy.mockImplementation(); render( , ); expect(console.error).not.toHaveBeenCalled(); render( , ); expect(console.error).not.toHaveBeenCalled(); render( , ); expect(console.error).not.toHaveBeenCalled(); }); }); test('composes aria-label with selected value for screen readers', () => { const { getInput } = renderSelect({ label: undefined, 'aria-label': 'Color', defaultValue: Color.Blue, }); const button = getInput(); expect(button).toHaveAttribute('aria-label', 'Color, Blue'); }); test('composes aria-label with placeholder when no value is selected', () => { const { getInput } = renderSelect({ label: undefined, 'aria-label': 'Color', placeholder: 'Choose a color', }); const button = getInput(); expect(button).toHaveAttribute('aria-label', 'Color, Choose a color'); }); test('does not compose aria-label when a visible label is present', () => { const { getInput } = renderSelect({ label: 'Color', 'aria-label': 'Color', defaultValue: Color.Blue, }); const button = getInput(); expect(button).not.toHaveAttribute('aria-label'); }); test('does not compose aria-label when aria-labelledby is present', () => { const { getInput } = renderSelect({ label: undefined, 'aria-label': 'Color', 'aria-labelledby': 'custom-label', defaultValue: Color.Blue, }); const button = getInput(); expect(button).not.toHaveAttribute('aria-label'); }); test('option & group passes through data props', async () => { const { queryByTestId } = render( , ); const { getInput } = getTestUtils(); userEvent.click(getInput()); const group = queryByTestId('lg-group'); const option = queryByTestId('lg-option'); expect(group).toBeInTheDocument(); expect(option).toBeInTheDocument(); }); describe('tab order', () => { test('contains component when enabled', () => { const { getInput } = renderSelect(); expect(document.body).toHaveFocus(); userEvent.tab(); expect(getInput()).toHaveFocus(); userEvent.tab(); expect(document.body).toHaveFocus(); }); // Select should still be focusable when disabled test('and contains component when disabled', () => { const { getInput } = renderSelect({ disabled: true }); expect(document.body).toHaveFocus(); userEvent.tab(); expect(getInput()).toHaveFocus(); userEvent.tab(); expect(document.body).toHaveFocus(); }); }); describe.each([ ['enabled', true], ['disabled', false], ])('has initially selected option when %p', (_, enabled) => { const isDisabled = !enabled; test('when uncontrolled', () => { const { getInput, isDisabled: utilsIsDisabled, getInputValue, } = renderSelect({ disabled: isDisabled, defaultValue: Color.Blue, }); expect(getInput()).toBeVisible(); expect(getInputValue()).toBe('Blue'); expect(utilsIsDisabled()).toBe(isDisabled); }); test('when controlled', () => { const onChangeSpy = jest.fn(); const { getInput, isDisabled: utilsIsDisabled, getInputValue, } = renderSelect({ disabled: isDisabled, value: Color.Blue, onChange: onChangeSpy, }); expect(getInput()).toBeVisible(); expect(getInputValue()).toBe('Blue'); expect(utilsIsDisabled()).toBe(isDisabled); expect(onChangeSpy).not.toHaveBeenCalled(); }); }); test('warns when controlled component has no `onChange` handler', () => { Context.within(Jest.spyContext(console, 'warn'), spy => { spy.mockImplementation(); render(); render( {defaultProps.children}
Invalid element
, ); const { getInput } = getTestUtils(); userEvent.click(getInput()); expect(spy).toHaveBeenCalledWith( '`Select` instance received child that is not `Option` or `OptionGroup`.', ); return result; }, ); expect(queryByText('Invalid element')).not.toBeInTheDocument(); }); test('omitting empty elements', () => { Context.within(Jest.spyContext(console, 'error'), spy => { spy.mockImplementation(); render( , ); const { getInput } = getTestUtils(); act(() => { userEvent.click(getInput()); }); expect(spy).not.toHaveBeenCalled(); }); }); test('glyph', async () => { const { getAllByLabelText } = render( , ); const { getPopover, getInput } = getTestUtils(); userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeVisible(); getAllByLabelText('Beaker Icon').forEach(element => expect(element).toBeVisible(), ); }); }); test('invalid glyph', () => { Context.within(Jest.spyContext(console, 'error'), spy => { spy.mockImplementation(); render( , ); const { getInput } = getTestUtils(); userEvent.click(getInput()); expect(spy).toHaveBeenCalledWith( '`Option` instance did not render icon because it is not a known glyph element.', ); }); }); describe('opening', () => { test('by clicking', async () => { const { getPopover, getInput } = renderSelect(); userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeVisible(); }); expect(getInput()).toHaveFocus(); }); test('by arrow down key', async () => { const { getOptionByValue, getPopover, getInput } = renderSelect(); // focus on button element userEvent.tab(); userEvent.type(getInput(), '{arrowdown}'); await waitFor(() => { expect(getPopover()).toBeVisible(); }); const firstOption = getOptionByValue('Select'); // First option is focused expect(firstOption).toHaveFocus(); }); test('by arrow up key', async () => { const { getOptionByValue, getPopover, getInput } = renderSelect(); // focus on button element userEvent.tab(); userEvent.type(getInput(), '{arrowup}'); await waitFor(() => { expect(getPopover()).toBeVisible(); }); const lastOption = getOptionByValue('Yellow'); // Last enabled option is focused expect(lastOption).toHaveFocus(); }); describe('is not allowed when disabled', () => { test('by clicking', async () => { const { getPopover, getInput } = renderSelect({ disabled: true, }); userEvent.click(getInput()); expect(getPopover()).not.toBeInTheDocument(); }); test('by arrow down key', () => { const { getPopover, getInput } = renderSelect({ disabled: true, }); // console.error // Error: unable to click element as it has or inherits pointer-events set to "none". userEvent.type(getInput(), '{arrowdown}'); expect(getPopover()).not.toBeInTheDocument(); }); test('by arrow up key', () => { const { getPopover, getInput } = renderSelect({ disabled: true, }); // console.error // Error: unable to click element as it has or inherits pointer-events set to "none". userEvent.type(getInput(), '{arrowup}'); expect(getPopover()).not.toBeInTheDocument(); }); }); }); describe('fires Popover callbacks', () => { test('opening the select fires the `onEnter*` callbacks', async () => { const onEnter = jest.fn(); const onEntering = jest.fn(); const onEntered = jest.fn(); const { getInput } = renderSelect({ onEnter: onEnter, onEntering: onEntering, onEntered: onEntered, }); userEvent.click(getInput()); expect(onEnter).toHaveBeenCalled(); expect(onEntering).toHaveBeenCalled(); await waitFor(() => expect(onEntered).toHaveBeenCalled()); }); test('closing the select fires the `onExit*` callbacks', async () => { const onExit = jest.fn(); const onExiting = jest.fn(); const onExited = jest.fn(); const { getInput } = renderSelect({ onExit: onExit, onExiting: onExiting, onExited: onExited, }); userEvent.click(getInput()); userEvent.click(getInput()); expect(onExit).toHaveBeenCalled(); expect(onExiting).toHaveBeenCalled(); await waitFor(() => expect(onExited).toHaveBeenCalled()); }); }); describe('closing', () => { describe('selecting an option closes menu', () => { test('on mouse click', async () => { const { getInput, getPopover, getOptionByValue } = renderSelect(); userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeVisible(); }); userEvent.click(getOptionByValue('Red')!); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('on enter', async () => { const { getInput, getPopover } = renderSelect(); userEvent.type(getInput(), '{arrowdown}'); await waitFor(() => { expect(getPopover()).toBeVisible(); }); // first option is focused by default userEvent.keyboard('{enter}'); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('on space', async () => { const { getInput, getPopover } = renderSelect(); userEvent.type(getInput(), '{arrowdown}'); await waitFor(() => { expect(getPopover()).toBeVisible(); }); // first option is focused by default userEvent.keyboard('{space}'); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); }); }); describe.each([ ['menu button', 'button'], ['list menu', 'listbox'], ] as const)('closing when %p is focused', (_, focusedElementRole) => { let getByRole: RenderResult['getByRole']; let getByText: RenderResult['getByText']; let getPopover: () => HTMLDivElement | null; let getInput: () => HTMLButtonElement; beforeEach(async () => { ({ getPopover, getByRole, getByText, getInput } = renderSelect()); userEvent.click(getInput()); await waitFor(() => { // eslint-disable-next-line jest/no-standalone-expect expect(getPopover()).toBeVisible(); }); const focusedElement = getByRole(focusedElementRole); act(() => focusedElement.focus()); }); test('by clicking outside', async () => { userEvent.click(document.body); expect(document.body).toHaveFocus(); await waitForElementToBeRemoved(getPopover()); }); test('by clicking menu button', async () => { const button = getInput(); userEvent.click(button); expect(button).toHaveFocus(); await waitForElementToBeRemoved(getPopover()); }); test('by escape key', async () => { userEvent.type(getByRole(focusedElementRole), '{esc}'); const button = getInput(); expect(button).toHaveFocus(); await waitForElementToBeRemoved(getPopover()); }); test('does not occur by clicking on option group label', async () => { userEvent.click(getByText('Enabled group')); await waitFor(() => { expect(getPopover()).toBeVisible(); }); expect(getPopover()).toHaveFocus(); }); }); describe.each([ ['uncontrolled', false], ['controlled', true], ])('when %p selecting', (_, controlled) => { let button: HTMLElement; let onChangeSpy: jest.MockedFunction< ( value: string, event: React.MouseEvent | KeyboardEvent | React.KeyboardEvent, ) => void >; let selectUtils: GetTestUtilsReturnType; beforeEach(() => { onChangeSpy = jest.fn(); render( controlled ? ( {(value, setValue) => ( ), ); const utils = getTestUtils(); button = utils.getInput(); selectUtils = utils; }); describe.each([ ['option', 'Red', Color.Red], ['option in group', 'Green', Color.Green], ])('%p', (_, optionText, optionValue) => { test('by Enter key', async () => { userEvent.click(button); const listbox = await waitFor(() => { const listbox = selectUtils.getPopover(); return listbox; }); const targetOption = selectUtils.getOptionByValue(optionText); expect(targetOption).not.toBe(null); act(() => targetOption!.focus()); userEvent.keyboard('[Enter]'); expect(onChangeSpy).toHaveBeenCalledTimes(1); expect(onChangeSpy).toHaveBeenCalledWith( optionValue, expect.anything(), ); await waitForElementToBeRemoved(listbox); expect(button).toBeVisible(); expect(button).toHaveFocus(); expect(button).toHaveTextContent(optionText); expect(button).toHaveValue(optionValue); }); test('by space key', async () => { userEvent.click(button); const listbox = await waitFor(() => { const listbox = selectUtils.getPopover(); return listbox; }); const targetOption = selectUtils.getOptionByValue(optionText); expect(targetOption).not.toBe(null); act(() => targetOption!.focus()); userEvent.keyboard('[Space]'); expect(onChangeSpy).toHaveBeenCalledTimes(1); expect(onChangeSpy).toHaveBeenCalledWith( optionValue, expect.anything(), ); await waitForElementToBeRemoved(listbox); expect(button).toBeVisible(); expect(button).toHaveFocus(); expect(button).toHaveTextContent(optionText); expect(button).toHaveValue(optionValue); }); test('by clicking', async () => { userEvent.click(button); const listbox = await waitFor(() => { const listbox = selectUtils.getPopover(); return listbox; }); const targetOption = selectUtils.getOptionByValue(optionText); userEvent.click(targetOption!); expect(onChangeSpy).toHaveBeenCalledTimes(1); expect(onChangeSpy).toHaveBeenCalledWith( optionValue, expect.anything(), ); await waitForElementToBeRemoved(listbox); expect(button).toBeVisible(); expect(button).toHaveFocus(); expect(button).toHaveTextContent(optionText); expect(button).toHaveValue(optionValue); }); }); describe.each([ ['disabled option', 'Orange'], ['option in disabled group', 'Indigo'], ])('does not occur for %p', (_, optionText) => { test('by Enter key', async () => { userEvent.click(button); const listbox = await waitFor(() => { const listbox = selectUtils.getPopover(); return listbox; }); const targetOption = selectUtils.getOptionByValue(optionText); expect(targetOption).not.toBe(null); act(() => targetOption!.focus()); fireEvent.keyDown(targetOption!, { key: keyMap.Enter, }); expect(onChangeSpy).not.toHaveBeenCalled(); expect(listbox).toBeInTheDocument(); expect(button).toBeVisible(); expect(targetOption).toHaveFocus(); expect(button).toHaveTextContent('Select'); expect(button).toHaveValue(''); }); test('by clicking', async () => { userEvent.click(button); const listbox = await waitFor(() => { const listbox = selectUtils.getPopover(); return listbox; }); const targetOption = selectUtils.getOptionByValue(optionText); userEvent.click(targetOption!); expect(onChangeSpy).not.toHaveBeenCalled(); expect(listbox).toBeInTheDocument(); expect(button).toBeVisible(); expect(button).toHaveTextContent('Select'); expect(button).toHaveValue(''); }); }); }); describe('focus', () => { test('moves to next option by arrow down key', async () => { const { getPopover, getOptionByValue, getOptions, getInput } = renderSelect(); userEvent.click(getInput()); const listbox = (await waitFor(() => { const listbox = getPopover(); expect(listbox).toBeInTheDocument(); return listbox; })) as HTMLDivElement; enabledOptions.forEach(expectedOptionText => { userEvent.type(listbox, '{arrowdown}'); expect(getOptionByValue(expectedOptionText)).toHaveFocus(); }); // Moves to first option when the end is reached userEvent.type(listbox, '{arrowdown}'); expect(getOptions()[0]).toHaveFocus(); }); test('moves to previous option by arrow down key', async () => { const { getInput, getPopover, getOptionByValue } = renderSelect(); userEvent.click(getInput()); const listbox = (await waitFor(() => { const listbox = getPopover(); expect(listbox).toBeInTheDocument(); return listbox; })) as HTMLDivElement; [...enabledOptions].reverse().forEach(expectedOptionText => { userEvent.type(listbox, '{arrowup}'); expect(getOptionByValue(expectedOptionText)).toHaveFocus(); }); // Moves to last option when the top is reached userEvent.type(listbox, '{arrowup}'); expect(getOptionByValue('Yellow')).toHaveFocus(); }); }); }); describe('selected option is stable', () => { const selected: JSX.Element = ( ); const nonselected: JSX.Element = ( ); let rerender: RenderResult['rerender']; let button: HTMLElement; let selectUtils: GetTestUtilsReturnType; beforeEach(async () => { ({ rerender } = render( , )); const utils = getTestUtils(); button = utils.getInput(); selectUtils = utils; userEvent.click(button); const targetOption = selectUtils.getOptionByValue('Selected Option'); userEvent.click(targetOption!); }); test('when selected option is removed', () => { rerender( , ); expect(button).toHaveTextContent('Select'); }); test('when options are re-ordered', () => { rerender( , ); expect(button).toHaveTextContent('Selected Option'); }); describe('when selected option is replaced', () => { test('with different object identity', () => { rerender( , ); expect(button).toHaveTextContent('Selected Option'); }); test('with same explicit value and different text', () => { rerender( , ); expect(button).toHaveTextContent('Different text'); }); test('with same computed value', () => { rerender( , ); expect(button).toHaveTextContent('selected'); }); }); }); describe('when the "state" is "error"', () => { test('error message is present', () => { const { isError, getErrorMessage } = renderSelect({ state: State.Error, errorMessage: errorMessage, }); expect(isError).toBeTruthy(); expect(getErrorMessage()).toBeInTheDocument(); }); }); describe('when the "state" is "none"', () => { test('error message is not present', () => { const { getErrorMessage } = renderSelect(); expect(getErrorMessage()).not.toBeInTheDocument(); }); }); describe.each([RenderMode.Inline, RenderMode.TopLayer])( `when renderMode=%p`, renderMode => { test('menu opens', async () => { const { getInput, getPopover } = renderSelect({ renderMode }); userEvent.click(getInput()); await waitFor(() => expect(getPopover()).toBeVisible()); }); describe('fires onChange when selecting an option', () => { test('on mouse click', async () => { const onChange = jest.fn(); const { getInput, getOptionByValue } = renderSelect({ renderMode, onChange: onChange, }); userEvent.click(getInput()); userEvent.click(getOptionByValue('Red')!); expect(onChange).toHaveBeenCalled(); }); test('on enter', async () => { const onChange = jest.fn(); const { getInput } = renderSelect({ renderMode, onChange: onChange, }); const button = getInput(); userEvent.type(button, '{arrowdown}'); // first option is focused by default userEvent.keyboard('{enter}'); expect(onChange).toHaveBeenCalled(); }); test('on space', async () => { const onChange = jest.fn(); const { getInput } = renderSelect({ renderMode, onChange: onChange, }); const button = getInput(); userEvent.type(button, '{arrowdown}'); // first option is focused by default userEvent.keyboard('{space}'); expect(onChange).toHaveBeenCalled(); }); }); describe('closing', () => { describe('selecting an option closes menu', () => { test('on mouse click', async () => { const { getInput, getPopover, getOptionByValue } = renderSelect({ renderMode, }); userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeVisible(); }); userEvent.click(getOptionByValue('Red')!); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('on enter', async () => { const { getInput, getPopover } = renderSelect({ renderMode, }); const button = getInput(); userEvent.type(button, '{arrowdown}'); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); // first option is focused by default userEvent.keyboard('{enter}'); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('on space', async () => { const { getInput, getPopover } = renderSelect({ renderMode, }); const button = getInput(); userEvent.type(button, '{arrowdown}'); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); // first option is focused by default userEvent.keyboard('{space}'); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); }); }); describe('fires Popover callbacks', () => { test('opening the select fires the `onEnter*` callbacks', async () => { const onEnter = jest.fn(); const onEntering = jest.fn(); const onEntered = jest.fn(); const { getInput } = renderSelect({ onEnter: onEnter, onEntering: onEntering, onEntered: onEntered, renderMode, }); userEvent.click(getInput()); expect(onEnter).toHaveBeenCalled(); expect(onEntering).toHaveBeenCalled(); await waitFor(() => expect(onEntered).toHaveBeenCalled()); }); test('closing the select fires the `onExit*` callbacks', async () => { const onExit = jest.fn(); const onExiting = jest.fn(); const onExited = jest.fn(); const { getInput } = renderSelect({ onExit: onExit, onExiting: onExiting, onExited: onExited, renderMode, }); userEvent.click(getInput()); userEvent.click(getInput()); expect(onExit).toHaveBeenCalled(); expect(onExiting).toHaveBeenCalled(); await waitFor(() => expect(onExited).toHaveBeenCalled()); }); }); }, ); describe('with PopoverContext', () => { const mockSetIsPopoverOpen = jest.fn(); afterEach(() => { mockSetIsPopoverOpen.mockReset(); }); test('calls `setIsPopoverOpen`', async () => { jest .spyOn(LeafyGreenProviderModule, 'usePopoverContext') .mockImplementation(() => ({ isPopoverOpen: false, setIsPopoverOpen: mockSetIsPopoverOpen, })); render( , ); const { getInput } = getTestUtils(); userEvent.click(getInput()); await waitFor(() => expect(mockSetIsPopoverOpen).toHaveBeenCalledWith(true), ); userEvent.click(getInput()); await waitFor(() => expect(mockSetIsPopoverOpen).toHaveBeenCalledWith(false), ); }); }); describe('controlled open state', () => { function ControlledOpenSelect({ initialOpen = false, }: { initialOpen?: boolean; }) { const [open, setOpen] = useState(initialOpen); return ( ); } test('opens and closes when controlled externally', async () => { const { getPopover, rerender } = renderSelect({}); // Initially closed expect(getPopover()).not.toBeInTheDocument(); // Rerender with open=true rerender( , ); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); // Rerender with open=false rerender( , ); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('calls setOpen when user interacts with component', async () => { const setOpenSpy = jest.fn(); renderSelect({ open: false, setOpen: setOpenSpy }); const { getInput: getUtilsInput } = getTestUtils(); // Click to open userEvent.click(getUtilsInput()); expect(setOpenSpy).toHaveBeenCalledWith(true); setOpenSpy.mockClear(); }); test('calls setOpen when pressing escape key', async () => { const setOpenSpy = jest.fn(); render( , ); const { getInput } = getTestUtils(); // Focus the input and press escape act(() => getInput().focus()); userEvent.keyboard('{escape}'); expect(setOpenSpy).toHaveBeenCalledWith(false); }); test('works with internal state management', async () => { render(); const { getInput, getPopover } = getTestUtils(); // Initially closed expect(getPopover()).not.toBeInTheDocument(); // Click to open userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); // Click to close userEvent.click(getInput()); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); test('starts open when initialOpen is true', async () => { render(); const { getPopover } = getTestUtils(); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); }); test('backwards compatibility - uncontrolled mode still works', async () => { render( , ); const { getPopover } = getTestUtils(); const { getInput } = getTestUtils(); // Initially closed expect(getPopover()).not.toBeInTheDocument(); // Click to open userEvent.click(getInput()); await waitFor(() => { expect(getPopover()).toBeInTheDocument(); }); // Click to close userEvent.click(getInput()); await waitForElementToBeRemoved(getPopover()); expect(getPopover()).not.toBeInTheDocument(); }); }); describe('controlled open state warnings', () => { test('warns when controlled component has no `setOpen` handler', () => { Context.within(Jest.spyContext(console, 'warn'), spy => { spy.mockImplementation(); render(); render( v}> {options} ; ; // Typed controlled & uncontrolled usage label="Label" value="apple" onChange={(v: Fruit) => v}> {options} ; label="Label" defaultValue="banana"> {options} ; // @ts-expect-error - 'cherry' is not assignable to Fruit label="Label" value="cherry"> {options} ; // @ts-expect-error - 'cherry' is not assignable to Fruit label="Label" defaultValue="cherry"> {options} ; // @ts-expect-error - handler param must be assignable from Fruit label="Label" value="apple" onChange={(v: number) => v}> {options} ; // ref still forwards as HTMLDivElement const ref = createRef(); ref={ref} label="Label" value="apple"> {options} ; }); /* eslint-enable jest/no-disabled-tests */ }); });