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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
import { GlAlert, GlFormCheckbox, GlFormInput, GlFormSelect, GlFormGroup } from '@gitlab/ui';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import VueRouter from 'vue-router';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import CrmForm from '~/crm/components/crm_form.vue';
import routes from '~/crm/contacts/routes';
import createContactMutation from '~/crm/contacts/components/graphql/create_contact.mutation.graphql';
import updateContactMutation from '~/crm/contacts/components/graphql/update_contact.mutation.graphql';
import getGroupContactsQuery from '~/crm/contacts/components/graphql/get_group_contacts.query.graphql';
import createOrganizationMutation from '~/crm/organizations/components/graphql/create_organization.mutation.graphql';
import getGroupOrganizationsQuery from '~/crm/organizations/components/graphql/get_group_organizations.query.graphql';
import {
createContactMutationErrorResponse,
createContactMutationResponse,
getGroupContactsQueryResponse,
updateContactMutationErrorResponse,
updateContactMutationResponse,
createOrganizationMutationErrorResponse,
createOrganizationMutationResponse,
getGroupOrganizationsQueryResponse,
} from './mock_data';
const FORM_CREATE_CONTACT = 'create contact';
const FORM_UPDATE_CONTACT = 'update contact';
const FORM_CREATE_ORG = 'create organization';
describe('Reusable form component', () => {
Vue.use(VueApollo);
Vue.use(VueRouter);
const DEFAULT_RESPONSES = {
createContact: Promise.resolve(createContactMutationResponse),
updateContact: Promise.resolve(updateContactMutationResponse),
createOrg: Promise.resolve(createOrganizationMutationResponse),
};
let wrapper;
let handler;
let fakeApollo;
let router;
beforeEach(() => {
router = new VueRouter({
base: '',
mode: 'history',
routes,
});
router.push('/test');
handler = jest.fn().mockImplementation((key) => DEFAULT_RESPONSES[key]);
const hanlderWithKey = (key) => (...args) => handler(key, ...args);
fakeApollo = createMockApollo([
[createContactMutation, hanlderWithKey('createContact')],
[updateContactMutation, hanlderWithKey('updateContact')],
[createOrganizationMutation, hanlderWithKey('createOrg')],
]);
fakeApollo.clients.defaultClient.cache.writeQuery({
query: getGroupContactsQuery,
variables: { groupFullPath: 'flightjs' },
data: getGroupContactsQueryResponse.data,
});
fakeApollo.clients.defaultClient.cache.writeQuery({
query: getGroupOrganizationsQuery,
variables: { groupFullPath: 'flightjs' },
data: getGroupOrganizationsQueryResponse.data,
});
});
const mockToastShow = jest.fn();
const findSaveButton = () => wrapper.findByTestId('save-button');
const findForm = () => wrapper.find('form');
const findError = () => wrapper.findComponent(GlAlert);
const findFormGroup = (at) => wrapper.findAllComponents(GlFormGroup).at(at);
const mountComponent = (propsData) => {
wrapper = shallowMountExtended(CrmForm, {
router,
apolloProvider: fakeApollo,
propsData: { drawerOpen: true, ...propsData },
mocks: {
$toast: {
show: mockToastShow,
},
},
});
};
const mountContact = ({ propsData, extraFields = [] } = {}) => {
mountComponent({
fields: [
{ name: 'firstName', label: 'First name', required: true },
{ name: 'lastName', label: 'Last name', required: true },
{ name: 'email', label: 'Email', required: true },
{ name: 'phone', label: 'Phone' },
{ name: 'description', label: 'Description' },
{
name: 'organizationId',
label: 'Organization',
values: [
{ key: 'gid://gitlab/CustomerRelations::Organization/1', value: 'GitLab' },
{ key: 'gid://gitlab/CustomerRelations::Organization/2', value: 'ABC Corp' },
],
},
...extraFields,
],
getQuery: {
query: getGroupContactsQuery,
variables: { groupFullPath: 'flightjs' },
},
getQueryNodePath: 'group.contacts',
...propsData,
});
};
const mountContactCreate = () => {
const propsData = {
title: 'New contact',
successMessage: 'Contact has been added.',
buttonLabel: 'Create contact',
mutation: createContactMutation,
additionalCreateParams: { groupId: 'gid://gitlab/Group/26' },
};
mountContact({ propsData });
};
const mountContactUpdate = () => {
const propsData = {
title: 'Edit contact',
successMessage: 'Contact has been updated.',
mutation: updateContactMutation,
existingId: 'gid://gitlab/CustomerRelations::Contact/12',
};
const extraFields = [{ name: 'active', label: 'Active', required: true, bool: true }];
mountContact({ propsData, extraFields });
};
const mountOrganization = ({ propsData } = {}) => {
mountComponent({
fields: [
{ name: 'name', label: 'Name', required: true },
{ name: 'defaultRate', label: 'Default rate', input: { type: 'number', step: '0.01' } },
{ name: 'description', label: 'Description' },
],
getQuery: {
query: getGroupOrganizationsQuery,
variables: { groupFullPath: 'flightjs' },
},
getQueryNodePath: 'group.organizations',
...propsData,
});
};
const mountOrganizationCreate = () => {
const propsData = {
title: 'New organization',
successMessage: 'Organization has been added.',
buttonLabel: 'Create organization',
mutation: createOrganizationMutation,
additionalCreateParams: { groupId: 'gid://gitlab/Group/26' },
};
mountOrganization({ propsData });
};
const forms = {
[FORM_CREATE_CONTACT]: {
mountFunction: mountContactCreate,
mutationErrorResponse: createContactMutationErrorResponse,
toastMessage: 'Contact has been added.',
},
[FORM_UPDATE_CONTACT]: {
mountFunction: mountContactUpdate,
mutationErrorResponse: updateContactMutationErrorResponse,
toastMessage: 'Contact has been updated.',
},
[FORM_CREATE_ORG]: {
mountFunction: mountOrganizationCreate,
mutationErrorResponse: createOrganizationMutationErrorResponse,
toastMessage: 'Organization has been added.',
},
};
const asTestParams = (...keys) => keys.map((name) => [name, forms[name]]);
describe.each(asTestParams(FORM_CREATE_CONTACT, FORM_UPDATE_CONTACT))(
'%s form save button',
(name, { mountFunction }) => {
beforeEach(() => {
mountFunction();
});
it('should be disabled when required fields are empty', async () => {
wrapper.find('#firstName').vm.$emit('input', '');
await waitForPromises();
expect(findSaveButton().props('disabled')).toBe(true);
});
it('should not be disabled when required fields have values', async () => {
wrapper.find('#firstName').vm.$emit('input', 'A');
wrapper.find('#lastName').vm.$emit('input', 'B');
wrapper.find('#email').vm.$emit('input', 'C');
await waitForPromises();
expect(findSaveButton().props('disabled')).toBe(false);
});
},
);
describe.each(asTestParams(FORM_CREATE_ORG))('%s form save button', (name, { mountFunction }) => {
beforeEach(() => {
mountFunction();
});
it('should be disabled when required field is empty', async () => {
wrapper.find('#name').vm.$emit('input', '');
await waitForPromises();
expect(findSaveButton().props('disabled')).toBe(true);
});
it('should not be disabled when required field has a value', async () => {
wrapper.find('#name').vm.$emit('input', 'A');
await waitForPromises();
expect(findSaveButton().props('disabled')).toBe(false);
});
});
describe.each(asTestParams(FORM_CREATE_CONTACT, FORM_UPDATE_CONTACT, FORM_CREATE_ORG))(
'when %s mutation is successful',
(name, { mountFunction, toastMessage }) => {
it('form should display correct toast message', async () => {
mountFunction();
findForm().trigger('submit');
await waitForPromises();
expect(mockToastShow).toHaveBeenCalledWith(toastMessage);
});
},
);
describe.each(asTestParams(FORM_CREATE_CONTACT, FORM_UPDATE_CONTACT, FORM_CREATE_ORG))(
'when %s mutation fails',
(formName, { mutationErrorResponse, mountFunction }) => {
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation();
});
it('should show error on reject', async () => {
handler.mockRejectedValue('ERROR');
mountFunction();
findForm().trigger('submit');
await waitForPromises();
expect(findError().text()).toBe('Something went wrong. Please try again.');
});
it('should show error on error response', async () => {
handler.mockResolvedValue(mutationErrorResponse);
mountFunction();
findForm().trigger('submit');
await waitForPromises();
expect(findError().text()).toBe(`${formName} is invalid.`);
});
},
);
describe('edit form', () => {
beforeEach(() => {
mountContactUpdate();
});
it.each`
index | id | component | value
${0} | ${'firstName'} | ${GlFormInput} | ${'Marty'}
${1} | ${'lastName'} | ${GlFormInput} | ${'McFly'}
${2} | ${'email'} | ${GlFormInput} | ${'example@gitlab.com'}
${4} | ${'description'} | ${GlFormInput} | ${undefined}
${3} | ${'phone'} | ${GlFormInput} | ${undefined}
${5} | ${'organizationId'} | ${GlFormSelect} | ${'gid://gitlab/CustomerRelations::Organization/2'}
`(
'should render the correct component for #$id with the value "$value"',
({ index, id, component, value }) => {
const findFormElement = () => findFormGroup(index).findComponent(component);
expect(findFormElement().attributes('id')).toBe(id);
expect(findFormElement().attributes('value')).toBe(value);
},
);
it('should render a checked GlFormCheckbox for #active', () => {
const activeCheckboxIndex = 6;
const findFormElement = () =>
findFormGroup(activeCheckboxIndex).findComponent(GlFormCheckbox);
expect(findFormElement().attributes('id')).toBe('active');
expect(findFormElement().attributes('checked')).toBe('true');
});
it('should include updated values in update mutation', () => {
wrapper.find('#firstName').vm.$emit('input', 'Michael');
wrapper
.find('#organizationId')
.vm.$emit('input', 'gid://gitlab/CustomerRelations::Organization/1');
findForm().trigger('submit');
expect(handler).toHaveBeenCalledWith('updateContact', {
input: {
active: true,
description: null,
email: 'example@gitlab.com',
firstName: 'Michael',
id: 'gid://gitlab/CustomerRelations::Contact/12',
lastName: 'McFly',
organizationId: 'gid://gitlab/CustomerRelations::Organization/1',
phone: null,
},
});
});
});
});
|