import {mountWithTheme} from 'sentry-test/enzyme';
import DropdownAutoCompleteMenu from 'app/components/dropdownAutoComplete/menu';
describe('DropdownAutoCompleteMenu', function () {
const routerContext = TestStubs.routerContext();
const items = [
{
value: 'apple',
label:
Apple
,
},
{
value: 'bacon',
label: Bacon
,
},
{
value: 'corn',
label: Corn
,
},
];
it('renders without a group', function () {
const wrapper = mountWithTheme(
{() => 'Click Me!'}
,
routerContext
);
expect(wrapper).toSnapshot();
});
it('renders with a group', function () {
const wrapper = mountWithTheme(
New Zealand,
},
{
value: 'australia',
label: Australia
,
},
],
},
]}
>
{() => 'Click Me!'}
,
routerContext
);
expect(wrapper).toSnapshot();
});
it('selects', function () {
const mock = jest.fn();
const countries = [
{
value: 'new zealand',
label: New Zealand
,
},
{
value: 'australia',
label: Australia
,
},
];
const wrapper = mountWithTheme(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
,
routerContext
);
wrapper.find('AutoCompleteItem').last().simulate('click');
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith(
{index: 1, ...countries[1]},
{highlightedIndex: 0, inputValue: '', isOpen: true, selectedItem: undefined},
expect.anything()
);
});
it('shows empty message when there are no items', function () {
const wrapper = mountWithTheme(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
,
routerContext
);
expect(wrapper.find('EmptyMessage')).toHaveLength(1);
expect(wrapper.find('EmptyMessage').text()).toBe('No items!');
// No input because there are no items
expect(wrapper.find('StyledInput')).toHaveLength(0);
});
it('shows default empty results message when there are no items found in search', function () {
const wrapper = mountWithTheme(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
,
routerContext
);
wrapper.find('StyledInput').simulate('change', {target: {value: 'U-S-A'}});
expect(wrapper.find('EmptyMessage')).toHaveLength(1);
expect(wrapper.find('EmptyMessage').text()).toBe('No items! found');
});
it('overrides default empty results message', function () {
const wrapper = mountWithTheme(
{({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
,
routerContext
);
wrapper.find('StyledInput').simulate('change', {target: {value: 'U-S-A'}});
expect(wrapper.find('EmptyMessage').text()).toBe('No search results');
});
it('hides filter with `hideInput` prop', function () {
const wrapper = mountWithTheme(
{() => 'Click Me!'}
,
routerContext
);
expect(wrapper.find('StyledInput')).toHaveLength(0);
});
it('filters using a value from prop instead of input', function () {
const wrapper = mountWithTheme(
{() => 'Click Me!'}
,
routerContext
);
wrapper.find('StyledInput').simulate('change', {target: {value: 'U-S-A'}});
expect(wrapper.find('EmptyMessage')).toHaveLength(0);
expect(wrapper.find('AutoCompleteItem')).toHaveLength(1);
expect(wrapper.find('AutoCompleteItem').text()).toBe('Apple');
});
});