import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
import DropdownAutoCompleteMenu from 'sentry/components/dropdownAutoComplete/menu';
describe('DropdownAutoCompleteMenu', function () {
const items = [
{
value: 'apple',
label:
Apple
,
},
{
value: 'bacon',
label: Bacon
,
},
{
value: 'corn',
label: Corn
,
},
];
it('renders without a group', function () {
const {container} = render(
{() => 'Click Me!'}
);
expect(container).toSnapshot();
});
it('renders with a group', function () {
const {container} = render(
New Zealand,
},
{
value: 'australia',
label: Australia
,
},
],
},
]}
>
{() => 'Click Me!'}
);
expect(container).toSnapshot();
});
it('can select an item by clicking', function () {
const mock = jest.fn();
const countries = [
{
value: 'new zealand',
label: New Zealand
,
},
{
value: 'australia',
label: Australia
,
},
];
render(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
);
userEvent.click(screen.getByRole('option', {name: 'Australia'}));
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith(
{index: 1, ...countries[1]},
{highlightedIndex: 1, inputValue: '', isOpen: true, selectedItem: undefined},
expect.anything()
);
});
it('shows empty message when there are no items', function () {
render(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
);
expect(screen.getByText('No items!')).toBeInTheDocument();
// No input because there are no items
expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
});
it('shows default empty results message when there are no items found in search', function () {
render(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
);
userEvent.type(screen.getByRole('textbox'), 'U-S-A');
expect(screen.getByText('No items! found')).toBeInTheDocument();
});
it('overrides default empty results message', function () {
render(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
);
userEvent.type(screen.getByRole('textbox'), 'U-S-A');
expect(screen.getByText('No search results')).toBeInTheDocument();
expect(screen.queryByRole('option')).not.toBeInTheDocument();
});
it('hides filter with `hideInput` prop', function () {
render(
{() => 'Click Me!'}
);
expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
});
it('filters using a value from prop instead of input', function () {
render(
{() => 'Click Me!'}
);
userEvent.type(screen.getByRole('textbox'), 'U-S-A');
expect(screen.getByRole('option', {name: 'Apple'})).toBeInTheDocument();
expect(screen.queryByRole('option', {name: 'Corn'})).not.toBeInTheDocument();
expect(screen.queryByRole('option', {name: 'Bacon'})).not.toBeInTheDocument();
});
});