debian-mirror-gitlab/spec/frontend/search/topbar/components/searchable_dropdown_spec.js

221 lines
7.1 KiB
JavaScript
Raw Normal View History

2021-01-29 00:20:46 +05:30
import { GlDropdown, GlDropdownItem, GlSearchBoxByType, GlSkeletonLoader } from '@gitlab/ui';
2021-09-04 01:27:46 +05:30
import { shallowMount, mount } from '@vue/test-utils';
import Vue from 'vue';
2021-03-11 19:13:27 +05:30
import Vuex from 'vuex';
2021-09-30 23:02:18 +05:30
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
2021-02-22 17:27:13 +05:30
import { MOCK_GROUPS, MOCK_GROUP, MOCK_QUERY } from 'jest/search/mock_data';
import SearchableDropdown from '~/search/topbar/components/searchable_dropdown.vue';
import { ANY_OPTION, GROUP_DATA } from '~/search/topbar/constants';
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
Vue.use(Vuex);
2021-01-29 00:20:46 +05:30
2021-02-22 17:27:13 +05:30
describe('Global Search Searchable Dropdown', () => {
2021-01-29 00:20:46 +05:30
let wrapper;
const defaultProps = {
2021-02-22 17:27:13 +05:30
headerText: GROUP_DATA.headerText,
2021-09-04 01:27:46 +05:30
name: GROUP_DATA.name,
fullName: GROUP_DATA.fullName,
2021-02-22 17:27:13 +05:30
loading: false,
selectedItem: ANY_OPTION,
items: [],
2021-01-29 00:20:46 +05:30
};
2021-02-22 17:27:13 +05:30
const createComponent = (initialState, props, mountFn = shallowMount) => {
2021-01-29 00:20:46 +05:30
const store = new Vuex.Store({
state: {
query: MOCK_QUERY,
...initialState,
},
});
2021-09-30 23:02:18 +05:30
wrapper = extendedWrapper(
mountFn(SearchableDropdown, {
store,
propsData: {
...defaultProps,
...props,
},
}),
);
2021-01-29 00:20:46 +05:30
};
afterEach(() => {
wrapper.destroy();
});
2021-09-04 01:27:46 +05:30
const findGlDropdown = () => wrapper.findComponent(GlDropdown);
const findGlDropdownSearch = () => findGlDropdown().findComponent(GlSearchBoxByType);
2021-01-29 00:20:46 +05:30
const findDropdownText = () => findGlDropdown().find('.dropdown-toggle-text');
2021-09-30 23:02:18 +05:30
const findSearchableDropdownItems = () => wrapper.findAllByTestId('searchable-items');
const findFrequentDropdownItems = () => wrapper.findAllByTestId('frequent-items');
2021-09-04 01:27:46 +05:30
const findAnyDropdownItem = () => findGlDropdown().findComponent(GlDropdownItem);
2021-09-30 23:02:18 +05:30
const findFirstSearchableDropdownItem = () => findSearchableDropdownItems().at(0);
const findFirstFrequentDropdownItem = () => findFrequentDropdownItems().at(0);
2021-09-04 01:27:46 +05:30
const findLoader = () => wrapper.findComponent(GlSkeletonLoader);
2021-01-29 00:20:46 +05:30
describe('template', () => {
beforeEach(() => {
createComponent();
});
it('renders GlDropdown', () => {
expect(findGlDropdown().exists()).toBe(true);
});
describe('findGlDropdownSearch', () => {
it('renders always', () => {
expect(findGlDropdownSearch().exists()).toBe(true);
});
it('has debounce prop', () => {
expect(findGlDropdownSearch().attributes('debounce')).toBe('500');
});
describe('onSearch', () => {
2021-02-22 17:27:13 +05:30
const search = 'test search';
2021-01-29 00:20:46 +05:30
beforeEach(() => {
2021-02-22 17:27:13 +05:30
findGlDropdownSearch().vm.$emit('input', search);
2021-01-29 00:20:46 +05:30
});
2021-02-22 17:27:13 +05:30
it('$emits @search when input event is fired from GlSearchBoxByType', () => {
expect(wrapper.emitted('search')[0]).toEqual([search]);
2021-01-29 00:20:46 +05:30
});
});
});
2021-09-30 23:02:18 +05:30
describe('Searchable Dropdown Items', () => {
2021-02-22 17:27:13 +05:30
describe('when loading is false', () => {
2021-01-29 00:20:46 +05:30
beforeEach(() => {
2021-02-22 17:27:13 +05:30
createComponent({}, { items: MOCK_GROUPS });
2021-01-29 00:20:46 +05:30
});
it('does not render loader', () => {
expect(findLoader().exists()).toBe(false);
});
2021-09-04 01:27:46 +05:30
it('renders the Any Dropdown', () => {
expect(findAnyDropdownItem().exists()).toBe(true);
});
2021-09-30 23:02:18 +05:30
it('renders searchable dropdown item for each item', () => {
2021-09-04 01:27:46 +05:30
expect(findSearchableDropdownItems()).toHaveLength(MOCK_GROUPS.length);
2021-01-29 00:20:46 +05:30
});
});
2021-02-22 17:27:13 +05:30
describe('when loading is true', () => {
2021-01-29 00:20:46 +05:30
beforeEach(() => {
2021-02-22 17:27:13 +05:30
createComponent({}, { loading: true, items: MOCK_GROUPS });
2021-01-29 00:20:46 +05:30
});
it('does render loader', () => {
expect(findLoader().exists()).toBe(true);
});
2021-09-04 01:27:46 +05:30
it('renders the Any Dropdown', () => {
expect(findAnyDropdownItem().exists()).toBe(true);
2021-02-22 17:27:13 +05:30
});
2021-09-30 23:02:18 +05:30
it('does not render searchable dropdown items', () => {
2021-09-04 01:27:46 +05:30
expect(findSearchableDropdownItems()).toHaveLength(0);
2021-02-22 17:27:13 +05:30
});
});
2021-01-29 00:20:46 +05:30
});
2021-09-30 23:02:18 +05:30
describe.each`
searchText | frequentItems | length
${''} | ${[]} | ${0}
${''} | ${MOCK_GROUPS} | ${MOCK_GROUPS.length}
${'test'} | ${[]} | ${0}
${'test'} | ${MOCK_GROUPS} | ${0}
`('Frequent Dropdown Items', ({ searchText, frequentItems, length }) => {
describe(`when search is ${searchText} and frequentItems length is ${frequentItems.length}`, () => {
beforeEach(() => {
createComponent({}, { frequentItems });
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2021-09-30 23:02:18 +05:30
wrapper.setData({ searchText });
});
it(`should${length ? '' : ' not'} render frequent dropdown items`, () => {
expect(findFrequentDropdownItems()).toHaveLength(length);
});
});
});
2021-01-29 00:20:46 +05:30
describe('Dropdown Text', () => {
2021-02-22 17:27:13 +05:30
describe('when selectedItem is any', () => {
2021-01-29 00:20:46 +05:30
beforeEach(() => {
createComponent({}, {}, mount);
});
it('sets dropdown text to Any', () => {
2021-02-22 17:27:13 +05:30
expect(findDropdownText().text()).toBe(ANY_OPTION.name);
2021-01-29 00:20:46 +05:30
});
});
2021-02-22 17:27:13 +05:30
describe('selectedItem is set', () => {
2021-01-29 00:20:46 +05:30
beforeEach(() => {
2021-02-22 17:27:13 +05:30
createComponent({}, { selectedItem: MOCK_GROUP }, mount);
2021-01-29 00:20:46 +05:30
});
2021-09-04 01:27:46 +05:30
it('sets dropdown text to the selectedItem name', () => {
expect(findDropdownText().text()).toBe(MOCK_GROUP[GROUP_DATA.name]);
2021-01-29 00:20:46 +05:30
});
});
});
});
describe('actions', () => {
beforeEach(() => {
2021-09-30 23:02:18 +05:30
createComponent({}, { items: MOCK_GROUPS, frequentItems: MOCK_GROUPS });
2021-01-29 00:20:46 +05:30
});
2021-02-22 17:27:13 +05:30
it('clicking "Any" dropdown item $emits @change with ANY_OPTION', () => {
2021-01-29 00:20:46 +05:30
findAnyDropdownItem().vm.$emit('click');
2021-02-22 17:27:13 +05:30
expect(wrapper.emitted('change')[0]).toEqual([ANY_OPTION]);
2021-01-29 00:20:46 +05:30
});
2021-09-30 23:02:18 +05:30
it('on searchable item @change, the wrapper $emits change with the item', () => {
findFirstSearchableDropdownItem().vm.$emit('change', MOCK_GROUPS[0]);
expect(wrapper.emitted('change')[0]).toEqual([MOCK_GROUPS[0]]);
});
it('on frequent item @change, the wrapper $emits change with the item', () => {
findFirstFrequentDropdownItem().vm.$emit('change', MOCK_GROUPS[0]);
2021-01-29 00:20:46 +05:30
2021-02-22 17:27:13 +05:30
expect(wrapper.emitted('change')[0]).toEqual([MOCK_GROUPS[0]]);
2021-01-29 00:20:46 +05:30
});
2021-09-30 23:02:18 +05:30
describe('opening the dropdown', () => {
describe('for the first time', () => {
beforeEach(() => {
findGlDropdown().vm.$emit('show');
});
it('$emits @search and @first-open', () => {
expect(wrapper.emitted('search')[0]).toStrictEqual([wrapper.vm.searchText]);
expect(wrapper.emitted('first-open')[0]).toStrictEqual([]);
});
});
describe('not for the first time', () => {
beforeEach(() => {
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2021-09-30 23:02:18 +05:30
wrapper.setData({ hasBeenOpened: true });
findGlDropdown().vm.$emit('show');
});
it('$emits @search and not @first-open', () => {
expect(wrapper.emitted('search')[0]).toStrictEqual([wrapper.vm.searchText]);
expect(wrapper.emitted('first-open')).toBeUndefined();
});
});
});
2021-01-29 00:20:46 +05:30
});
});