123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- import {Fragment} from 'react';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import {CompactSelect} from 'sentry/components/compactSelect';
- describe('CompactSelect', function () {
- it('renders', function () {
- const {container} = render(
- <CompactSelect
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- expect(container).toSnapshot();
- });
- it('renders disabled', function () {
- render(
- <CompactSelect
- disabled
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- expect(screen.getByRole('button')).toBeDisabled();
- });
- it('renders with menu title', async function () {
- render(
- <CompactSelect
- menuTitle="Menu title"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- expect(screen.getByText('Menu title')).toBeInTheDocument();
- });
- it('can be dismissed', async function () {
- render(
- <Fragment>
- <CompactSelect
- value="opt_one"
- menuTitle="Menu A"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- <CompactSelect
- value="opt_three"
- menuTitle="Menu B"
- options={[
- {value: 'opt_three', label: 'Option Three'},
- {value: 'opt_four', label: 'Option Four'},
- ]}
- />
- </Fragment>
- );
- // Can be dismissed by clicking outside
- await userEvent.click(screen.getByRole('button', {name: 'Option One'}));
- expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
- await userEvent.click(document.body);
- expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
- // Can be dismissed by pressing Escape
- await userEvent.click(screen.getByRole('button', {name: 'Option One'}));
- expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
- await userEvent.keyboard('{Escape}');
- expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
- // When menu A is open, clicking once on menu B's trigger button closes menu A and
- // then opens menu B
- await userEvent.click(screen.getByRole('button', {name: 'Option One'}));
- expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
- await userEvent.click(screen.getByRole('button', {name: 'Option Three'}));
- expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
- expect(screen.getByRole('option', {name: 'Option Three'})).toBeInTheDocument();
- });
- describe('ListBox', function () {
- it('updates trigger label on selection', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One
- await userEvent.click(screen.getByRole('option', {name: 'Option One'}));
- expect(mock).toHaveBeenCalledWith({value: 'opt_one', label: 'Option One'});
- expect(screen.getByRole('button', {name: 'Option One'})).toBeInTheDocument();
- });
- it('can select multiple options', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- multiple
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One & Option Two
- await userEvent.click(screen.getByRole('option', {name: 'Option One'}));
- await userEvent.click(screen.getByRole('option', {name: 'Option Two'}));
- expect(mock).toHaveBeenCalledWith([
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]);
- expect(screen.getByRole('button', {name: 'Option One +1'})).toBeInTheDocument();
- });
- it('can select options with values containing quotes', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- multiple
- options={[
- {value: '"opt_one"', label: 'Option One'},
- {value: '"opt_two"', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One & Option Two
- await userEvent.click(screen.getByRole('option', {name: 'Option One'}));
- await userEvent.click(screen.getByRole('option', {name: 'Option Two'}));
- expect(mock).toHaveBeenCalledWith([
- {value: '"opt_one"', label: 'Option One'},
- {value: '"opt_two"', label: 'Option Two'},
- ]);
- });
- it('displays trigger button with prefix', function () {
- render(
- <CompactSelect
- triggerProps={{prefix: 'Prefix'}}
- value="opt_one"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- expect(screen.getByRole('button', {name: 'Prefix Option One'})).toBeInTheDocument();
- });
- it('can search', async function () {
- render(
- <CompactSelect
- searchable
- searchPlaceholder="Search here…"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // type 'Two' into the search box
- await userEvent.click(screen.getByPlaceholderText('Search here…'));
- await userEvent.keyboard('Two');
- // only Option Two should be available, Option One should be filtered out
- expect(screen.getByRole('option', {name: 'Option Two'})).toBeInTheDocument();
- expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
- });
- it('can limit the number of options', async function () {
- render(
- <CompactSelect
- sizeLimit={2}
- sizeLimitMessage="Use search for more options…"
- searchable
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- {value: 'opt_three', label: 'Option Three'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // only the first two options should be visible due to `sizeLimit`
- expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
- expect(screen.getByRole('option', {name: 'Option Two'})).toBeInTheDocument();
- expect(
- screen.queryByRole('option', {name: 'Option Three'})
- ).not.toBeInTheDocument();
- // there's a message prompting the user to use search to find more options
- expect(screen.getByText('Use search for more options…')).toBeInTheDocument();
- // Option Three is not reachable via keyboard, focus wraps back to Option One
- await userEvent.keyboard(`{ArrowDown}`);
- expect(screen.getByRole('option', {name: 'Option One'})).toHaveFocus();
- await userEvent.keyboard(`{ArrowDown>2}`);
- expect(screen.getByRole('option', {name: 'Option One'})).toHaveFocus();
- // Option Three is still available via search
- await userEvent.type(screen.getByPlaceholderText('Search…'), 'three');
- expect(screen.getByRole('option', {name: 'Option Three'})).toBeInTheDocument();
- // the size limit message is gone during search
- expect(screen.queryByText('Use search for more options…')).not.toBeInTheDocument();
- });
- it('can toggle sections', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- multiple
- onSectionToggle={mock}
- options={[
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ],
- },
- {
- key: 'section-2',
- label: 'Section 2',
- showToggleAllButton: true,
- options: [
- {value: 'opt_three', label: 'Option Three'},
- {value: 'opt_four', label: 'Option Four'},
- ],
- },
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button', {expanded: false}));
- await waitFor(() =>
- expect(screen.getByRole('option', {name: 'Option One'})).toHaveFocus()
- );
- // move focus to Section 1's toggle button and press it to select all
- await userEvent.keyboard('{Tab}');
- expect(screen.getByRole('button', {name: 'Select All in Section 1'})).toHaveFocus();
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('option', {name: 'Option One'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(screen.getByRole('option', {name: 'Option Two'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {key: 'opt_one', value: 'opt_one', label: 'Option One'},
- {key: 'opt_two', value: 'opt_two', label: 'Option Two'},
- ],
- },
- 'select'
- );
- // press Section 1's toggle button again to unselect all
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('option', {name: 'Option One'})).toHaveAttribute(
- 'aria-selected',
- 'false'
- );
- expect(screen.getByRole('option', {name: 'Option Two'})).toHaveAttribute(
- 'aria-selected',
- 'false'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {key: 'opt_one', value: 'opt_one', label: 'Option One'},
- {key: 'opt_two', value: 'opt_two', label: 'Option Two'},
- ],
- },
- 'unselect'
- );
- // move to Section 2's toggle button and select all
- await userEvent.keyboard('{Tab}');
- expect(screen.getByRole('button', {name: 'Select All in Section 2'})).toHaveFocus();
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('option', {name: 'Option Three'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(screen.getByRole('option', {name: 'Option Four'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-2',
- label: 'Section 2',
- showToggleAllButton: true,
- options: [
- {key: 'opt_three', value: 'opt_three', label: 'Option Three'},
- {key: 'opt_four', value: 'opt_four', label: 'Option Four'},
- ],
- },
- 'select'
- );
- });
- it('triggers onClose when the menu is closed if provided', async function () {
- const onCloseMock = jest.fn();
- render(
- <CompactSelect
- onClose={onCloseMock}
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- expect(onCloseMock).not.toHaveBeenCalled();
- // close the menu
- await userEvent.click(document.body);
- expect(onCloseMock).toHaveBeenCalled();
- });
- });
- describe('GridList', function () {
- it('updates trigger label on selection', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- grid
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One
- await userEvent.click(screen.getByRole('row', {name: 'Option One'}));
- expect(mock).toHaveBeenCalledWith({value: 'opt_one', label: 'Option One'});
- expect(screen.getByRole('button', {name: 'Option One'})).toBeInTheDocument();
- });
- it('can select multiple options', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- grid
- multiple
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One & Option Two
- await userEvent.click(screen.getByRole('row', {name: 'Option One'}));
- await userEvent.click(screen.getByRole('row', {name: 'Option Two'}));
- expect(mock).toHaveBeenCalledWith([
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]);
- expect(screen.getByRole('button', {name: 'Option One +1'})).toBeInTheDocument();
- });
- it('can select options with values containing quotes', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- grid
- multiple
- options={[
- {value: '"opt_one"', label: 'Option One'},
- {value: '"opt_two"', label: 'Option Two'},
- ]}
- onChange={mock}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // select Option One & Option Two
- await userEvent.click(screen.getByRole('row', {name: 'Option One'}));
- await userEvent.click(screen.getByRole('row', {name: 'Option Two'}));
- expect(mock).toHaveBeenCalledWith([
- {value: '"opt_one"', label: 'Option One'},
- {value: '"opt_two"', label: 'Option Two'},
- ]);
- });
- it('displays trigger button with prefix', function () {
- render(
- <CompactSelect
- grid
- triggerProps={{prefix: 'Prefix'}}
- value="opt_one"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- expect(screen.getByRole('button', {name: 'Prefix Option One'})).toBeInTheDocument();
- });
- it('can search', async function () {
- render(
- <CompactSelect
- grid
- searchable
- searchPlaceholder="Search here…"
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // type 'Two' into the search box
- await userEvent.click(screen.getByPlaceholderText('Search here…'));
- await userEvent.keyboard('Two');
- // only Option Two should be available, Option One should be filtered out
- expect(screen.getByRole('row', {name: 'Option Two'})).toBeInTheDocument();
- expect(screen.queryByRole('row', {name: 'Option One'})).not.toBeInTheDocument();
- });
- it('can limit the number of options', async function () {
- render(
- <CompactSelect
- grid
- sizeLimit={2}
- sizeLimitMessage="Use search for more options…"
- searchable
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- {value: 'opt_three', label: 'Option Three'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- // only the first two options should be visible due to `sizeLimit`
- expect(screen.getByRole('row', {name: 'Option One'})).toBeInTheDocument();
- expect(screen.getByRole('row', {name: 'Option Two'})).toBeInTheDocument();
- expect(screen.queryByRole('row', {name: 'Option Three'})).not.toBeInTheDocument();
- // there's a message prompting the user to use search to find more options
- expect(screen.getByText('Use search for more options…')).toBeInTheDocument();
- // Option Three is not reachable via keyboard, focus wraps back to Option One
- await userEvent.keyboard(`{ArrowDown}`);
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveFocus();
- await userEvent.keyboard(`{ArrowDown>2}`);
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveFocus();
- // Option Three is still available via search
- await userEvent.type(screen.getByPlaceholderText('Search…'), 'three');
- expect(screen.getByRole('row', {name: 'Option Three'})).toBeInTheDocument();
- // the size limit message is gone during search
- expect(screen.queryByText('Use search for more options…')).not.toBeInTheDocument();
- });
- it('can toggle sections', async function () {
- const mock = jest.fn();
- render(
- <CompactSelect
- grid
- multiple
- onSectionToggle={mock}
- options={[
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ],
- },
- {
- key: 'section-2',
- label: 'Section 2',
- showToggleAllButton: true,
- options: [
- {value: 'opt_three', label: 'Option Three'},
- {value: 'opt_four', label: 'Option Four'},
- ],
- },
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button', {expanded: false}));
- await waitFor(() =>
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveFocus()
- );
- // move focus to Section 1's toggle button and press it to select all
- await userEvent.keyboard('{Tab}');
- expect(screen.getByRole('button', {name: 'Select All in Section 1'})).toHaveFocus();
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(screen.getByRole('row', {name: 'Option Two'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {key: 'opt_one', value: 'opt_one', label: 'Option One'},
- {key: 'opt_two', value: 'opt_two', label: 'Option Two'},
- ],
- },
- 'select'
- );
- // press Section 1's toggle button again to unselect all
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveAttribute(
- 'aria-selected',
- 'false'
- );
- expect(screen.getByRole('row', {name: 'Option Two'})).toHaveAttribute(
- 'aria-selected',
- 'false'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-1',
- label: 'Section 1',
- showToggleAllButton: true,
- options: [
- {key: 'opt_one', value: 'opt_one', label: 'Option One'},
- {key: 'opt_two', value: 'opt_two', label: 'Option Two'},
- ],
- },
- 'unselect'
- );
- // move to Section 2's toggle button and select all
- await userEvent.keyboard('{Tab}');
- expect(screen.getByRole('button', {name: 'Select All in Section 2'})).toHaveFocus();
- await userEvent.keyboard('{Enter}');
- expect(screen.getByRole('row', {name: 'Option Three'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(screen.getByRole('row', {name: 'Option Four'})).toHaveAttribute(
- 'aria-selected',
- 'true'
- );
- expect(mock).toHaveBeenCalledWith(
- {
- key: 'section-2',
- label: 'Section 2',
- showToggleAllButton: true,
- options: [
- {key: 'opt_three', value: 'opt_three', label: 'Option Three'},
- {key: 'opt_four', value: 'opt_four', label: 'Option Four'},
- ],
- },
- 'select'
- );
- });
- it('triggers onClose when the menu is closed if provided', async function () {
- const onCloseMock = jest.fn();
- render(
- <CompactSelect
- grid
- onClose={onCloseMock}
- options={[
- {value: 'opt_one', label: 'Option One'},
- {value: 'opt_two', label: 'Option Two'},
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- expect(onCloseMock).not.toHaveBeenCalled();
- // close the menu
- await userEvent.click(document.body);
- expect(onCloseMock).toHaveBeenCalled();
- });
- it('allows keyboard navigation to nested buttons', async function () {
- const onPointerUpMock = jest.fn();
- const onKeyUpMock = jest.fn();
- render(
- <CompactSelect
- grid
- options={[
- {
- value: 'opt_one',
- label: 'Option One',
- trailingItems: (
- <button onPointerUp={onPointerUpMock} onKeyUp={onKeyUpMock}>
- Trailing Button One
- </button>
- ),
- },
- {
- value: 'opt_two',
- label: 'Option Two',
- trailingItems: (
- <button onPointerUp={onPointerUpMock} onKeyUp={onKeyUpMock}>
- Trailing Button Two
- </button>
- ),
- },
- ]}
- />
- );
- // click on the trigger button
- await userEvent.click(screen.getByRole('button'));
- await waitFor(() =>
- expect(screen.getByRole('row', {name: 'Option One'})).toHaveFocus()
- );
- // press Arrow Right, focus should be moved to the trailing button
- await userEvent.keyboard('{ArrowRight}');
- expect(screen.getByRole('button', {name: 'Trailing Button One'})).toHaveFocus();
- // press Enter, onKeyUpMock is called
- await userEvent.keyboard('{ArrowRight}');
- expect(onKeyUpMock).toHaveBeenCalled();
- // click on Trailing Button Two, onPointerUpMock is called
- await userEvent.click(screen.getByRole('button', {name: 'Trailing Button Two'}));
- expect(onPointerUpMock).toHaveBeenCalled();
- });
- });
- });
|