debian-mirror-gitlab/spec/frontend/runner/components/runner_filtered_search_bar_spec.js

167 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { GlFilteredSearch, GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import RunnerFilteredSearchBar from '~/runner/components/runner_filtered_search_bar.vue';
2021-11-11 11:23:49 +05:30
import { statusTokenConfig } from '~/runner/components/search_tokens/status_token_config';
2021-09-30 23:02:18 +05:30
import TagToken from '~/runner/components/search_tokens/tag_token.vue';
2021-11-11 11:23:49 +05:30
import { tagTokenConfig } from '~/runner/components/search_tokens/tag_token_config';
2021-12-11 22:18:48 +05:30
import { PARAM_KEY_STATUS, PARAM_KEY_TAG, STATUS_ACTIVE, INSTANCE_TYPE } from '~/runner/constants';
2021-09-04 01:27:46 +05:30
import FilteredSearch from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
2021-09-30 23:02:18 +05:30
import BaseToken from '~/vue_shared/components/filtered_search_bar/tokens/base_token.vue';
2021-09-04 01:27:46 +05:30
describe('RunnerList', () => {
let wrapper;
const findFilteredSearch = () => wrapper.findComponent(FilteredSearch);
const findGlFilteredSearch = () => wrapper.findComponent(GlFilteredSearch);
const findSortOptions = () => wrapper.findAllComponents(GlDropdownItem);
const mockDefaultSort = 'CREATED_DESC';
const mockOtherSort = 'CONTACTED_DESC';
const mockFilters = [
2021-11-11 11:23:49 +05:30
{ type: PARAM_KEY_STATUS, value: { data: STATUS_ACTIVE, operator: '=' } },
2021-09-04 01:27:46 +05:30
{ type: 'filtered-search-term', value: { data: '' } },
];
2021-12-11 22:18:48 +05:30
const expectToHaveLastEmittedInput = (value) => {
const inputs = wrapper.emitted('input');
expect(inputs[inputs.length - 1][0]).toEqual(value);
};
2021-09-04 01:27:46 +05:30
const createComponent = ({ props = {}, options = {} } = {}) => {
wrapper = extendedWrapper(
shallowMount(RunnerFilteredSearchBar, {
propsData: {
2021-09-30 23:02:18 +05:30
namespace: 'runners',
2021-11-11 11:23:49 +05:30
tokens: [],
2021-09-04 01:27:46 +05:30
value: {
2021-12-11 22:18:48 +05:30
runnerType: null,
2021-09-04 01:27:46 +05:30
filters: [],
sort: mockDefaultSort,
},
...props,
},
stubs: {
FilteredSearch,
GlFilteredSearch,
GlDropdown,
GlDropdownItem,
},
...options,
}),
);
};
beforeEach(() => {
createComponent();
});
afterEach(() => {
wrapper.destroy();
});
it('binds a namespace to the filtered search', () => {
expect(findFilteredSearch().props('namespace')).toBe('runners');
});
it('sets sorting options', () => {
const SORT_OPTIONS_COUNT = 2;
expect(findSortOptions()).toHaveLength(SORT_OPTIONS_COUNT);
expect(findSortOptions().at(0).text()).toBe('Created date');
expect(findSortOptions().at(1).text()).toBe('Last contact');
});
2021-11-11 11:23:49 +05:30
it('sets tokens to the filtered search', () => {
createComponent({
props: {
2021-12-11 22:18:48 +05:30
tokens: [statusTokenConfig, tagTokenConfig],
2021-11-11 11:23:49 +05:30
},
});
2021-09-04 01:27:46 +05:30
expect(findFilteredSearch().props('tokens')).toEqual([
expect.objectContaining({
type: PARAM_KEY_STATUS,
2021-09-30 23:02:18 +05:30
token: BaseToken,
2021-09-04 01:27:46 +05:30
options: expect.any(Array),
}),
2021-09-30 23:02:18 +05:30
expect.objectContaining({
type: PARAM_KEY_TAG,
token: TagToken,
}),
2021-09-04 01:27:46 +05:30
]);
});
it('fails validation for v-model with the wrong shape', () => {
expect(() => {
createComponent({ props: { value: { filters: 'wrong_filters', sort: 'sort' } } });
}).toThrow('Invalid prop: custom validator check failed');
expect(() => {
createComponent({ props: { value: { sort: 'sort' } } });
}).toThrow('Invalid prop: custom validator check failed');
});
describe('when a search is preselected', () => {
beforeEach(() => {
createComponent({
props: {
value: {
2021-12-11 22:18:48 +05:30
runnerType: INSTANCE_TYPE,
2021-09-04 01:27:46 +05:30
sort: mockOtherSort,
filters: mockFilters,
},
},
});
});
it('filter values are shown', () => {
expect(findGlFilteredSearch().props('value')).toEqual(mockFilters);
});
it('sort option is selected', () => {
expect(
findSortOptions()
.filter((w) => w.props('isChecked'))
.at(0)
.text(),
).toEqual('Last contact');
});
2021-12-11 22:18:48 +05:30
it('when the user sets a filter, the "search" preserves the other filters', () => {
findGlFilteredSearch().vm.$emit('input', mockFilters);
findGlFilteredSearch().vm.$emit('submit');
expectToHaveLastEmittedInput({
runnerType: INSTANCE_TYPE,
filters: mockFilters,
sort: mockOtherSort,
pagination: { page: 1 },
});
});
2021-09-04 01:27:46 +05:30
});
it('when the user sets a filter, the "search" is emitted with filters', () => {
findGlFilteredSearch().vm.$emit('input', mockFilters);
findGlFilteredSearch().vm.$emit('submit');
2021-12-11 22:18:48 +05:30
expectToHaveLastEmittedInput({
runnerType: null,
filters: mockFilters,
sort: mockDefaultSort,
pagination: { page: 1 },
});
2021-09-04 01:27:46 +05:30
});
it('when the user sets a sorting method, the "search" is emitted with the sort', () => {
findSortOptions().at(1).vm.$emit('click');
2021-12-11 22:18:48 +05:30
expectToHaveLastEmittedInput({
runnerType: null,
filters: [],
sort: mockOtherSort,
pagination: { page: 1 },
});
2021-09-04 01:27:46 +05:30
});
});