2021-12-07 15:15:03 +00:00
|
|
|
import { GlAlert } from '@gitlab/ui';
|
2021-12-03 12:10:23 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-12-16 00:15:50 +00:00
|
|
|
import ContactForm from '~/crm/components/contact_form.vue';
|
2021-12-03 12:10:23 +00:00
|
|
|
import createContactMutation from '~/crm/components/queries/create_contact.mutation.graphql';
|
2021-12-16 00:15:50 +00:00
|
|
|
import updateContactMutation from '~/crm/components/queries/update_contact.mutation.graphql';
|
2021-12-03 12:10:23 +00:00
|
|
|
import getGroupContactsQuery from '~/crm/components/queries/get_group_contacts.query.graphql';
|
|
|
|
import {
|
|
|
|
createContactMutationErrorResponse,
|
|
|
|
createContactMutationResponse,
|
|
|
|
getGroupContactsQueryResponse,
|
2021-12-16 00:15:50 +00:00
|
|
|
updateContactMutationErrorResponse,
|
|
|
|
updateContactMutationResponse,
|
2021-12-03 12:10:23 +00:00
|
|
|
} from './mock_data';
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
describe('Customer relations contact form component', () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
Vue.use(VueApollo);
|
|
|
|
let wrapper;
|
|
|
|
let fakeApollo;
|
2021-12-16 00:15:50 +00:00
|
|
|
let mutation;
|
2021-12-03 12:10:23 +00:00
|
|
|
let queryHandler;
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
const findSaveContactButton = () => wrapper.findByTestId('save-contact-button');
|
2021-12-03 12:10:23 +00:00
|
|
|
const findCancelButton = () => wrapper.findByTestId('cancel-button');
|
|
|
|
const findForm = () => wrapper.find('form');
|
2021-12-07 15:15:03 +00:00
|
|
|
const findError = () => wrapper.findComponent(GlAlert);
|
2021-12-03 12:10:23 +00:00
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
const mountComponent = ({ mountFunction = shallowMountExtended, editForm = false } = {}) => {
|
|
|
|
fakeApollo = createMockApollo([[mutation, queryHandler]]);
|
2021-12-03 12:10:23 +00:00
|
|
|
fakeApollo.clients.defaultClient.cache.writeQuery({
|
|
|
|
query: getGroupContactsQuery,
|
|
|
|
variables: { groupFullPath: 'flightjs' },
|
|
|
|
data: getGroupContactsQueryResponse.data,
|
|
|
|
});
|
2021-12-16 00:15:50 +00:00
|
|
|
const propsData = { drawerOpen: true };
|
|
|
|
if (editForm)
|
|
|
|
propsData.contact = { firstName: 'First', lastName: 'Last', email: 'email@example.com' };
|
|
|
|
wrapper = mountFunction(ContactForm, {
|
2021-12-03 12:10:23 +00:00
|
|
|
provide: { groupId: 26, groupFullPath: 'flightjs' },
|
|
|
|
apolloProvider: fakeApollo,
|
2021-12-16 00:15:50 +00:00
|
|
|
propsData,
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-12-16 00:15:50 +00:00
|
|
|
mutation = createContactMutation;
|
2021-12-03 12:10:23 +00:00
|
|
|
queryHandler = jest.fn().mockResolvedValue(createContactMutationResponse);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
fakeApollo = null;
|
|
|
|
});
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
describe('Save contact button', () => {
|
|
|
|
it('should be disabled when required fields are empty', () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
mountComponent();
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
expect(findSaveContactButton().props('disabled')).toBe(true);
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
it('should not be disabled when required fields have values', async () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
wrapper.find('#contact-first-name').vm.$emit('input', 'A');
|
|
|
|
wrapper.find('#contact-last-name').vm.$emit('input', 'B');
|
|
|
|
wrapper.find('#contact-email').vm.$emit('input', 'C');
|
|
|
|
await waitForPromises();
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
expect(findSaveContactButton().props('disabled')).toBe(false);
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should emit 'close' when cancel button is clicked", () => {
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findCancelButton().vm.$emit('click');
|
|
|
|
|
|
|
|
expect(wrapper.emitted().close).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
describe('when create mutation is successful', () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
it("should emit 'close'", async () => {
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(wrapper.emitted().close).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-16 00:15:50 +00:00
|
|
|
describe('when create mutation fails', () => {
|
2021-12-07 15:15:03 +00:00
|
|
|
it('should show error on reject', async () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
queryHandler = jest.fn().mockRejectedValue('ERROR');
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
2021-12-07 15:15:03 +00:00
|
|
|
expect(findError().exists()).toBe(true);
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|
|
|
|
|
2021-12-07 15:15:03 +00:00
|
|
|
it('should show error on error response', async () => {
|
2021-12-03 12:10:23 +00:00
|
|
|
queryHandler = jest.fn().mockResolvedValue(createContactMutationErrorResponse);
|
|
|
|
mountComponent();
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
2021-12-07 15:15:03 +00:00
|
|
|
expect(findError().exists()).toBe(true);
|
2022-01-06 18:13:45 +00:00
|
|
|
expect(findError().text()).toBe('create contact is invalid.');
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|
|
|
|
});
|
2021-12-16 00:15:50 +00:00
|
|
|
|
|
|
|
describe('when update mutation is successful', () => {
|
|
|
|
it("should emit 'close'", async () => {
|
|
|
|
mutation = updateContactMutation;
|
|
|
|
queryHandler = jest.fn().mockResolvedValue(updateContactMutationResponse);
|
|
|
|
mountComponent({ editForm: true });
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(wrapper.emitted().close).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when update mutation fails', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mutation = updateContactMutation;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show error on reject', async () => {
|
|
|
|
queryHandler = jest.fn().mockRejectedValue('ERROR');
|
|
|
|
mountComponent({ editForm: true });
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findError().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show error on error response', async () => {
|
|
|
|
queryHandler = jest.fn().mockResolvedValue(updateContactMutationErrorResponse);
|
|
|
|
mountComponent({ editForm: true });
|
|
|
|
|
|
|
|
findForm().trigger('submit');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findError().exists()).toBe(true);
|
2022-01-06 18:13:45 +00:00
|
|
|
expect(findError().text()).toBe('update contact is invalid.');
|
2021-12-16 00:15:50 +00:00
|
|
|
});
|
|
|
|
});
|
2021-12-03 12:10:23 +00:00
|
|
|
});
|