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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
import {
GlFilteredSearchSuggestion,
GlFilteredSearchTokenSegment,
GlDropdownDivider,
} from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { OPTIONS_NONE_ANY } from '~/vue_shared/components/filtered_search_bar/constants';
import BaseToken from '~/vue_shared/components/filtered_search_bar/tokens/base_token.vue';
import CrmOrganizationToken from '~/vue_shared/components/filtered_search_bar/tokens/crm_organization_token.vue';
import searchCrmOrganizationsQuery from '~/vue_shared/components/filtered_search_bar/queries/search_crm_organizations.query.graphql';
import {
mockCrmOrganizations,
mockCrmOrganizationToken,
mockGroupCrmOrganizationsQueryResponse,
mockProjectCrmOrganizationsQueryResponse,
} from '../mock_data';
jest.mock('~/alert');
const defaultStubs = {
Portal: true,
BaseToken,
GlFilteredSearchSuggestionList: {
template: '<div></div>',
methods: {
getValue: () => '=',
},
},
};
describe('CrmOrganizationToken', () => {
Vue.use(VueApollo);
let wrapper;
const getBaseToken = () => wrapper.findComponent(BaseToken);
const searchGroupCrmOrganizationsQueryHandler = jest
.fn()
.mockResolvedValue(mockGroupCrmOrganizationsQueryResponse);
const searchProjectCrmOrganizationsQueryHandler = jest
.fn()
.mockResolvedValue(mockProjectCrmOrganizationsQueryResponse);
const mountComponent = ({
config = mockCrmOrganizationToken,
value = { data: '' },
active = false,
stubs = defaultStubs,
listeners = {},
queryHandler = searchGroupCrmOrganizationsQueryHandler,
} = {}) => {
wrapper = mount(CrmOrganizationToken, {
apolloProvider: createMockApollo([[searchCrmOrganizationsQuery, queryHandler]]),
propsData: {
config,
value,
active,
cursorPosition: 'start',
},
provide: {
portalName: 'fake target',
alignSuggestions: function fakeAlignSuggestions() {},
suggestionsListClass: () => 'custom-class',
termsAsTokens: () => false,
},
stubs,
listeners,
});
};
describe('methods', () => {
describe('fetchOrganizations', () => {
describe('for groups', () => {
beforeEach(() => {
mountComponent();
});
it('calls the apollo query providing the searchString when search term is a string', async () => {
getBaseToken().vm.$emit('fetch-suggestions', 'foo');
await waitForPromises();
expect(createAlert).not.toHaveBeenCalled();
expect(searchGroupCrmOrganizationsQueryHandler).toHaveBeenCalledWith({
fullPath: 'group',
isProject: false,
searchString: 'foo',
searchIds: null,
});
expect(getBaseToken().props('suggestions')).toEqual(mockCrmOrganizations);
});
it('calls the apollo query providing the searchId when search term is a number', async () => {
getBaseToken().vm.$emit('fetch-suggestions', '5');
await waitForPromises();
expect(createAlert).not.toHaveBeenCalled();
expect(searchGroupCrmOrganizationsQueryHandler).toHaveBeenCalledWith({
fullPath: 'group',
isProject: false,
searchString: null,
searchIds: ['gid://gitlab/CustomerRelations::Organization/5'],
});
expect(getBaseToken().props('suggestions')).toEqual(mockCrmOrganizations);
});
});
describe('for projects', () => {
beforeEach(() => {
mountComponent({
config: {
fullPath: 'project',
isProject: true,
},
queryHandler: searchProjectCrmOrganizationsQueryHandler,
});
});
it('calls the apollo query providing the searchString when search term is a string', async () => {
getBaseToken().vm.$emit('fetch-suggestions', 'foo');
await waitForPromises();
expect(createAlert).not.toHaveBeenCalled();
expect(searchProjectCrmOrganizationsQueryHandler).toHaveBeenCalledWith({
fullPath: 'project',
isProject: true,
searchString: 'foo',
searchIds: null,
});
expect(getBaseToken().props('suggestions')).toEqual(mockCrmOrganizations);
});
it('calls the apollo query providing the searchId when search term is a number', async () => {
getBaseToken().vm.$emit('fetch-suggestions', '5');
await waitForPromises();
expect(createAlert).not.toHaveBeenCalled();
expect(searchProjectCrmOrganizationsQueryHandler).toHaveBeenCalledWith({
fullPath: 'project',
isProject: true,
searchString: null,
searchIds: ['gid://gitlab/CustomerRelations::Organization/5'],
});
expect(getBaseToken().props('suggestions')).toEqual(mockCrmOrganizations);
});
});
it('calls `createAlert` when request fails', async () => {
mountComponent({ queryHandler: jest.fn().mockRejectedValue({}) });
getBaseToken().vm.$emit('fetch-suggestions');
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({
message: 'There was a problem fetching CRM organizations.',
});
});
it('sets `loading` to false when request completes', async () => {
mountComponent({ queryHandler: jest.fn().mockRejectedValue({}) });
getBaseToken().vm.$emit('fetch-suggestions');
await waitForPromises();
expect(getBaseToken().props('suggestionsLoading')).toBe(false);
});
});
});
describe('template', () => {
const defaultOrganizations = OPTIONS_NONE_ANY;
it('renders base-token component', () => {
mountComponent({
config: { ...mockCrmOrganizationToken, initialOrganizations: mockCrmOrganizations },
value: { data: '1' },
});
expect(getBaseToken().props('suggestions')).toEqual(mockCrmOrganizations);
});
it.each(mockCrmOrganizations)('renders token item when value is selected', (organization) => {
mountComponent({
config: { ...mockCrmOrganizationToken, initialOrganizations: mockCrmOrganizations },
value: { data: `${getIdFromGraphQLId(organization.id)}` },
});
const tokenSegments = wrapper.findAllComponents(GlFilteredSearchTokenSegment);
expect(tokenSegments).toHaveLength(3); // Organization, =, Organization name
expect(tokenSegments.at(2).text()).toBe(organization.name); // Organization name
});
it('renders provided defaultOrganizations as suggestions', async () => {
mountComponent({
active: true,
config: { ...mockCrmOrganizationToken, defaultOrganizations },
stubs: { Portal: true },
});
const tokenSegments = wrapper.findAllComponents(GlFilteredSearchTokenSegment);
const suggestionsSegment = tokenSegments.at(2);
suggestionsSegment.vm.$emit('activate');
await nextTick();
const suggestions = wrapper.findAllComponents(GlFilteredSearchSuggestion);
expect(suggestions).toHaveLength(defaultOrganizations.length);
defaultOrganizations.forEach((organization, index) => {
expect(suggestions.at(index).text()).toBe(organization.text);
});
});
it('does not render divider when no defaultOrganizations', async () => {
mountComponent({
active: true,
config: { ...mockCrmOrganizationToken, defaultOrganizations: [] },
stubs: { Portal: true },
});
const tokenSegments = wrapper.findAllComponents(GlFilteredSearchTokenSegment);
const suggestionsSegment = tokenSegments.at(2);
suggestionsSegment.vm.$emit('activate');
await nextTick();
expect(wrapper.findComponent(GlFilteredSearchSuggestion).exists()).toBe(false);
expect(wrapper.findComponent(GlDropdownDivider).exists()).toBe(false);
});
it('renders `OPTIONS_NONE_ANY` as default suggestions', () => {
mountComponent({
active: true,
config: { ...mockCrmOrganizationToken },
stubs: { Portal: true },
});
const tokenSegments = wrapper.findAllComponents(GlFilteredSearchTokenSegment);
const suggestionsSegment = tokenSegments.at(2);
suggestionsSegment.vm.$emit('activate');
const suggestions = wrapper.findAllComponents(GlFilteredSearchSuggestion);
expect(suggestions).toHaveLength(OPTIONS_NONE_ANY.length);
OPTIONS_NONE_ANY.forEach((organization, index) => {
expect(suggestions.at(index).text()).toBe(organization.text);
});
});
it('emits listeners in the base-token', () => {
const mockInput = jest.fn();
mountComponent({ listeners: { input: mockInput } });
getBaseToken().vm.$emit('input', [{ data: 'mockData', operator: '=' }]);
expect(mockInput).toHaveBeenLastCalledWith([{ data: 'mockData', operator: '=' }]);
});
});
});
|