2022-08-27 11:52:29 +05:30
|
|
|
import { GlAlert, GlFormCheckbox, GlFormInput, GlFormSelect, GlFormGroup } from '@gitlab/ui';
|
2022-03-02 08:16:31 +05:30
|
|
|
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';
|
2023-03-04 22:38:38 +05:30
|
|
|
import CrmForm from '~/crm/components/crm_form.vue';
|
2022-06-21 17:19:12 +05:30
|
|
|
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';
|
2022-03-02 08:16:31 +05:30
|
|
|
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);
|
2022-08-27 11:52:29 +05:30
|
|
|
const findFormGroup = (at) => wrapper.findAllComponents(GlFormGroup).at(at);
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
const mountComponent = (propsData) => {
|
2023-03-04 22:38:38 +05:30
|
|
|
wrapper = shallowMountExtended(CrmForm, {
|
2022-03-02 08:16:31 +05:30
|
|
|
router,
|
|
|
|
apolloProvider: fakeApollo,
|
|
|
|
propsData: { drawerOpen: true, ...propsData },
|
|
|
|
mocks: {
|
|
|
|
$toast: {
|
|
|
|
show: mockToastShow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const mountContact = ({ propsData, extraFields = [] } = {}) => {
|
2022-03-02 08:16:31 +05:30
|
|
|
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' },
|
2022-07-16 23:28:13 +05:30
|
|
|
{
|
|
|
|
name: 'organizationId',
|
|
|
|
label: 'Organization',
|
|
|
|
values: [
|
|
|
|
{ key: 'gid://gitlab/CustomerRelations::Organization/1', value: 'GitLab' },
|
|
|
|
{ key: 'gid://gitlab/CustomerRelations::Organization/2', value: 'ABC Corp' },
|
|
|
|
],
|
|
|
|
},
|
2022-08-27 11:52:29 +05:30
|
|
|
...extraFields,
|
2022-03-02 08:16:31 +05:30
|
|
|
],
|
2022-06-21 17:19:12 +05:30
|
|
|
getQuery: {
|
|
|
|
query: getGroupContactsQuery,
|
|
|
|
variables: { groupFullPath: 'flightjs' },
|
|
|
|
},
|
|
|
|
getQueryNodePath: 'group.contacts',
|
2022-03-02 08:16:31 +05:30
|
|
|
...propsData,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const mountContactCreate = () => {
|
|
|
|
const propsData = {
|
|
|
|
title: 'New contact',
|
2022-06-21 17:19:12 +05:30
|
|
|
successMessage: 'Contact has been added.',
|
2022-03-02 08:16:31 +05:30
|
|
|
buttonLabel: 'Create contact',
|
|
|
|
mutation: createContactMutation,
|
|
|
|
additionalCreateParams: { groupId: 'gid://gitlab/Group/26' },
|
|
|
|
};
|
|
|
|
mountContact({ propsData });
|
|
|
|
};
|
|
|
|
|
|
|
|
const mountContactUpdate = () => {
|
|
|
|
const propsData = {
|
|
|
|
title: 'Edit contact',
|
2022-06-21 17:19:12 +05:30
|
|
|
successMessage: 'Contact has been updated.',
|
2022-03-02 08:16:31 +05:30
|
|
|
mutation: updateContactMutation,
|
2022-06-21 17:19:12 +05:30
|
|
|
existingId: 'gid://gitlab/CustomerRelations::Contact/12',
|
2022-03-02 08:16:31 +05:30
|
|
|
};
|
2022-08-27 11:52:29 +05:30
|
|
|
const extraFields = [{ name: 'active', label: 'Active', required: true, bool: true }];
|
|
|
|
mountContact({ propsData, extraFields });
|
2022-03-02 08:16:31 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
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' },
|
|
|
|
],
|
2022-06-21 17:19:12 +05:30
|
|
|
getQuery: {
|
|
|
|
query: getGroupOrganizationsQuery,
|
|
|
|
variables: { groupFullPath: 'flightjs' },
|
|
|
|
},
|
|
|
|
getQueryNodePath: 'group.organizations',
|
2022-03-02 08:16:31 +05:30
|
|
|
...propsData,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const mountOrganizationCreate = () => {
|
|
|
|
const propsData = {
|
|
|
|
title: 'New organization',
|
2022-06-21 17:19:12 +05:30
|
|
|
successMessage: 'Organization has been added.',
|
2022-03-02 08:16:31 +05:30
|
|
|
buttonLabel: 'Create organization',
|
|
|
|
mutation: createOrganizationMutation,
|
|
|
|
additionalCreateParams: { groupId: 'gid://gitlab/Group/26' },
|
|
|
|
};
|
|
|
|
mountOrganization({ propsData });
|
|
|
|
};
|
|
|
|
|
|
|
|
const forms = {
|
|
|
|
[FORM_CREATE_CONTACT]: {
|
|
|
|
mountFunction: mountContactCreate,
|
|
|
|
mutationErrorResponse: createContactMutationErrorResponse,
|
2022-06-21 17:19:12 +05:30
|
|
|
toastMessage: 'Contact has been added.',
|
2022-03-02 08:16:31 +05:30
|
|
|
},
|
|
|
|
[FORM_UPDATE_CONTACT]: {
|
|
|
|
mountFunction: mountContactUpdate,
|
|
|
|
mutationErrorResponse: updateContactMutationErrorResponse,
|
2022-06-21 17:19:12 +05:30
|
|
|
toastMessage: 'Contact has been updated.',
|
2022-03-02 08:16:31 +05:30
|
|
|
},
|
|
|
|
[FORM_CREATE_ORG]: {
|
|
|
|
mountFunction: mountOrganizationCreate,
|
|
|
|
mutationErrorResponse: createOrganizationMutationErrorResponse,
|
2022-06-21 17:19:12 +05:30
|
|
|
toastMessage: 'Organization has been added.',
|
2022-03-02 08:16:31 +05:30
|
|
|
},
|
|
|
|
};
|
|
|
|
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.`);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
describe('edit form', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mountContactUpdate();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
2022-08-27 11:52:29 +05:30
|
|
|
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'}
|
2022-07-16 23:28:13 +05:30
|
|
|
`(
|
2022-08-27 11:52:29 +05:30
|
|
|
'should render the correct component for #$id with the value "$value"',
|
|
|
|
({ index, id, component, value }) => {
|
2022-10-11 01:57:18 +05:30
|
|
|
const findFormElement = () => findFormGroup(index).findComponent(component);
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
expect(findFormElement().attributes('id')).toBe(id);
|
|
|
|
expect(findFormElement().attributes('value')).toBe(value);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it('should render a checked GlFormCheckbox for #active', () => {
|
|
|
|
const activeCheckboxIndex = 6;
|
2022-10-11 01:57:18 +05:30
|
|
|
const findFormElement = () =>
|
|
|
|
findFormGroup(activeCheckboxIndex).findComponent(GlFormCheckbox);
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
expect(findFormElement().attributes('id')).toBe('active');
|
|
|
|
expect(findFormElement().attributes('checked')).toBe('true');
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
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: {
|
2022-08-27 11:52:29 +05:30
|
|
|
active: true,
|
2022-07-16 23:28:13 +05:30
|
|
|
description: null,
|
|
|
|
email: 'example@gitlab.com',
|
|
|
|
firstName: 'Michael',
|
|
|
|
id: 'gid://gitlab/CustomerRelations::Contact/12',
|
|
|
|
lastName: 'McFly',
|
|
|
|
organizationId: 'gid://gitlab/CustomerRelations::Organization/1',
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-03-02 08:16:31 +05:30
|
|
|
});
|