1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import { GlButtonGroup, GlButton, GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import { MOCK_QUERY, MOCK_SORT_OPTIONS } from 'jest/search/mock_data';
import GlobalSearchSort from '~/search/sort/components/app.vue';
import { SORT_DIRECTION_UI } from '~/search/sort/constants';
Vue.use(Vuex);
describe('GlobalSearchSort', () => {
let wrapper;
const actionSpies = {
setQuery: jest.fn(),
applyQuery: jest.fn(),
};
const defaultProps = {
searchSortOptions: MOCK_SORT_OPTIONS,
};
const createComponent = (initialState, props) => {
const store = new Vuex.Store({
state: {
query: MOCK_QUERY,
...initialState,
},
actions: actionSpies,
});
wrapper = shallowMount(GlobalSearchSort, {
store,
propsData: {
...defaultProps,
...props,
},
});
};
const findSortButtonGroup = () => wrapper.findComponent(GlButtonGroup);
const findSortDropdown = () => wrapper.findComponent(GlDropdown);
const findSortDirectionButton = () => wrapper.findComponent(GlButton);
const findDropdownItems = () => findSortDropdown().findAllComponents(GlDropdownItem);
const findDropdownItemsText = () => findDropdownItems().wrappers.map((w) => w.text());
describe('template', () => {
beforeEach(() => {
createComponent();
});
it('renders Sort Button Group', () => {
expect(findSortButtonGroup().exists()).toBe(true);
});
it('renders Sort Dropdown', () => {
expect(findSortDropdown().exists()).toBe(true);
});
it('renders Sort Direction Button', () => {
expect(findSortDirectionButton().exists()).toBe(true);
});
});
describe('Sort Dropdown Items', () => {
describe('renders', () => {
beforeEach(() => {
createComponent();
});
it('an instance for each namespace', () => {
expect(findDropdownItemsText()).toStrictEqual(
MOCK_SORT_OPTIONS.map((option) => option.title),
);
});
});
describe.each`
sortQuery | value
${null} | ${MOCK_SORT_OPTIONS[0].title}
${'asdf'} | ${MOCK_SORT_OPTIONS[0].title}
${MOCK_SORT_OPTIONS[0].sortParam} | ${MOCK_SORT_OPTIONS[0].title}
${MOCK_SORT_OPTIONS[1].sortParam.desc} | ${MOCK_SORT_OPTIONS[1].title}
${MOCK_SORT_OPTIONS[1].sortParam.asc} | ${MOCK_SORT_OPTIONS[1].title}
`('selected', ({ sortQuery, value }) => {
describe(`when sort option is ${sortQuery}`, () => {
beforeEach(() => {
createComponent({ query: { sort: sortQuery } });
});
it('is set correctly', () => {
expect(findSortDropdown().attributes('text')).toBe(value);
});
});
});
});
describe.each`
description | sortQuery | sortUi | disabled
${'non-sortable'} | ${MOCK_SORT_OPTIONS[0].sortParam} | ${SORT_DIRECTION_UI.disabled} | ${'true'}
${'descending sortable'} | ${MOCK_SORT_OPTIONS[1].sortParam.desc} | ${SORT_DIRECTION_UI.desc} | ${undefined}
${'ascending sortable'} | ${MOCK_SORT_OPTIONS[1].sortParam.asc} | ${SORT_DIRECTION_UI.asc} | ${undefined}
`('Sort Direction Button', ({ description, sortQuery, sortUi, disabled }) => {
describe(`when sort option is ${description}`, () => {
beforeEach(() => {
createComponent({ query: { sort: sortQuery } });
});
it('sets the UI correctly', () => {
expect(findSortDirectionButton().attributes('disabled')).toBe(disabled);
expect(findSortDirectionButton().attributes('title')).toBe(sortUi.tooltip);
expect(findSortDirectionButton().attributes('icon')).toBe(sortUi.icon);
});
});
});
describe('actions', () => {
describe.each`
description | index | value
${'non-sortable'} | ${0} | ${MOCK_SORT_OPTIONS[0].sortParam}
${'sortable'} | ${1} | ${MOCK_SORT_OPTIONS[1].sortParam.desc}
`('handleSortChange', ({ description, index, value }) => {
describe(`when clicking a ${description} option`, () => {
beforeEach(() => {
createComponent();
findDropdownItems().at(index).vm.$emit('click');
});
it('calls setQuery and applyQuery correctly', () => {
expect(actionSpies.setQuery).toHaveBeenCalledTimes(1);
expect(actionSpies.applyQuery).toHaveBeenCalledTimes(1);
expect(actionSpies.setQuery).toHaveBeenCalledWith(expect.any(Object), {
key: 'sort',
value,
});
});
});
});
describe.each`
description | sortQuery | value
${'descending'} | ${MOCK_SORT_OPTIONS[1].sortParam.desc} | ${MOCK_SORT_OPTIONS[1].sortParam.asc}
${'ascending'} | ${MOCK_SORT_OPTIONS[1].sortParam.asc} | ${MOCK_SORT_OPTIONS[1].sortParam.desc}
`('handleSortDirectionChange', ({ description, sortQuery, value }) => {
describe(`when toggling a ${description} option`, () => {
beforeEach(() => {
createComponent({ query: { sort: sortQuery } });
findSortDirectionButton().vm.$emit('click');
});
it('calls setQuery and applyQuery correctly', () => {
expect(actionSpies.setQuery).toHaveBeenCalledTimes(1);
expect(actionSpies.applyQuery).toHaveBeenCalledTimes(1);
expect(actionSpies.setQuery).toHaveBeenCalledWith(expect.any(Object), {
key: 'sort',
value,
});
});
});
});
});
});
|