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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
// eslint-disable-next-line no-restricted-imports
import Vuex from 'vuex';
import Item from '~/ide/components/merge_requests/item.vue';
import List from '~/ide/components/merge_requests/list.vue';
import TokenedInput from '~/ide/components/shared/tokened_input.vue';
import { mergeRequests as mergeRequestsMock } from '../../mock_data';
Vue.use(Vuex);
describe('IDE merge requests list', () => {
let wrapper;
let fetchMergeRequestsMock;
const findSearchTypeButtons = () => wrapper.findAll('button');
const findTokenedInput = () => wrapper.findComponent(TokenedInput);
const createComponent = (state = {}) => {
const { mergeRequests = {}, ...restOfState } = state;
const fakeStore = new Vuex.Store({
state: {
currentMergeRequestId: '1',
currentProjectId: 'project/main',
...restOfState,
},
modules: {
mergeRequests: {
namespaced: true,
state: {
isLoading: false,
mergeRequests: [],
...mergeRequests,
},
actions: {
fetchMergeRequests: fetchMergeRequestsMock,
},
},
},
});
wrapper = shallowMount(List, {
store: fakeStore,
});
};
beforeEach(() => {
fetchMergeRequestsMock = jest.fn();
});
it('calls fetch on mounted', () => {
createComponent();
expect(fetchMergeRequestsMock).toHaveBeenCalledWith(expect.any(Object), {
search: '',
type: '',
});
});
it('renders loading icon when merge request is loading', () => {
createComponent({ mergeRequests: { isLoading: true } });
expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
});
it('renders no search results text when search is not empty', async () => {
createComponent();
findTokenedInput().vm.$emit('input', 'something');
await nextTick();
expect(wrapper.text()).toContain('No merge requests found');
});
it('clicking on search type, sets currentSearchType and loads merge requests', async () => {
createComponent();
findTokenedInput().vm.$emit('focus');
await nextTick();
findSearchTypeButtons().at(0).trigger('click');
await nextTick();
const searchType = wrapper.vm.$options.searchTypes[0];
expect(findTokenedInput().props('tokens')).toEqual([searchType]);
expect(fetchMergeRequestsMock).toHaveBeenCalledWith(expect.any(Object), {
type: searchType.type,
search: '',
});
});
describe('with merge requests', () => {
let defaultStateWithMergeRequests;
beforeAll(() => {
defaultStateWithMergeRequests = {
mergeRequests: {
isLoading: false,
mergeRequests: [
{ ...mergeRequestsMock[0], projectPathWithNamespace: 'gitlab-org/gitlab-foss' },
],
},
};
});
it('renders list', () => {
createComponent(defaultStateWithMergeRequests);
expect(wrapper.findAllComponents(Item).length).toBe(1);
expect(wrapper.findComponent(Item).props('item')).toBe(
defaultStateWithMergeRequests.mergeRequests.mergeRequests[0],
);
});
describe('when searching merge requests', () => {
it('calls `loadMergeRequests` on input in search field', async () => {
createComponent(defaultStateWithMergeRequests);
const input = findTokenedInput();
input.vm.$emit('input', 'something');
await nextTick();
expect(fetchMergeRequestsMock).toHaveBeenCalledWith(expect.any(Object), {
search: 'something',
type: '',
});
});
});
});
describe('on search focus', () => {
let input;
beforeEach(() => {
createComponent();
input = findTokenedInput();
});
describe('without search value', () => {
beforeEach(async () => {
input.vm.$emit('focus');
await nextTick();
});
it('shows search types', () => {
const buttons = findSearchTypeButtons();
expect(buttons.wrappers.map((x) => x.text().trim())).toEqual(
wrapper.vm.$options.searchTypes.map((x) => x.label),
);
});
it('hides search types when search changes', async () => {
input.vm.$emit('input', 'something');
await nextTick();
expect(findSearchTypeButtons().exists()).toBe(false);
});
describe('with search type', () => {
beforeEach(async () => {
findSearchTypeButtons().at(0).trigger('click');
await nextTick();
await input.vm.$emit('focus');
await nextTick();
});
it('does not show search types', () => {
expect(findSearchTypeButtons().exists()).toBe(false);
});
});
});
describe('with search value', () => {
beforeEach(async () => {
input.vm.$emit('input', 'something');
input.vm.$emit('focus');
await nextTick();
});
it('does not show search types', () => {
expect(findSearchTypeButtons().exists()).toBe(false);
});
});
});
});
|