From e04431d29efaf17dda9dfbfbd0c5001693b25ee4 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 16 Dec 2021 00:15:50 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/frontend/crm/contact_form_spec.js | 157 +++++++++++++++++++++++++++++ spec/frontend/crm/contacts_root_spec.js | 58 ++++++++--- spec/frontend/crm/mock_data.js | 28 +++++ spec/frontend/crm/new_contact_form_spec.js | 110 -------------------- 4 files changed, 230 insertions(+), 123 deletions(-) create mode 100644 spec/frontend/crm/contact_form_spec.js delete mode 100644 spec/frontend/crm/new_contact_form_spec.js (limited to 'spec/frontend/crm') diff --git a/spec/frontend/crm/contact_form_spec.js b/spec/frontend/crm/contact_form_spec.js new file mode 100644 index 00000000000..b2753ad8cf5 --- /dev/null +++ b/spec/frontend/crm/contact_form_spec.js @@ -0,0 +1,157 @@ +import { GlAlert } from '@gitlab/ui'; +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'; +import ContactForm from '~/crm/components/contact_form.vue'; +import createContactMutation from '~/crm/components/queries/create_contact.mutation.graphql'; +import updateContactMutation from '~/crm/components/queries/update_contact.mutation.graphql'; +import getGroupContactsQuery from '~/crm/components/queries/get_group_contacts.query.graphql'; +import { + createContactMutationErrorResponse, + createContactMutationResponse, + getGroupContactsQueryResponse, + updateContactMutationErrorResponse, + updateContactMutationResponse, +} from './mock_data'; + +describe('Customer relations contact form component', () => { + Vue.use(VueApollo); + let wrapper; + let fakeApollo; + let mutation; + let queryHandler; + + const findSaveContactButton = () => wrapper.findByTestId('save-contact-button'); + const findCancelButton = () => wrapper.findByTestId('cancel-button'); + const findForm = () => wrapper.find('form'); + const findError = () => wrapper.findComponent(GlAlert); + + const mountComponent = ({ mountFunction = shallowMountExtended, editForm = false } = {}) => { + fakeApollo = createMockApollo([[mutation, queryHandler]]); + fakeApollo.clients.defaultClient.cache.writeQuery({ + query: getGroupContactsQuery, + variables: { groupFullPath: 'flightjs' }, + data: getGroupContactsQueryResponse.data, + }); + const propsData = { drawerOpen: true }; + if (editForm) + propsData.contact = { firstName: 'First', lastName: 'Last', email: 'email@example.com' }; + wrapper = mountFunction(ContactForm, { + provide: { groupId: 26, groupFullPath: 'flightjs' }, + apolloProvider: fakeApollo, + propsData, + }); + }; + + beforeEach(() => { + mutation = createContactMutation; + queryHandler = jest.fn().mockResolvedValue(createContactMutationResponse); + }); + + afterEach(() => { + wrapper.destroy(); + fakeApollo = null; + }); + + describe('Save contact button', () => { + it('should be disabled when required fields are empty', () => { + mountComponent(); + + expect(findSaveContactButton().props('disabled')).toBe(true); + }); + + it('should not be disabled when required fields have values', async () => { + 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(); + + expect(findSaveContactButton().props('disabled')).toBe(false); + }); + }); + + it("should emit 'close' when cancel button is clicked", () => { + mountComponent(); + + findCancelButton().vm.$emit('click'); + + expect(wrapper.emitted().close).toBeTruthy(); + }); + + describe('when create mutation is successful', () => { + it("should emit 'close'", async () => { + mountComponent(); + + findForm().trigger('submit'); + await waitForPromises(); + + expect(wrapper.emitted().close).toBeTruthy(); + }); + }); + + describe('when create mutation fails', () => { + it('should show error on reject', async () => { + queryHandler = jest.fn().mockRejectedValue('ERROR'); + mountComponent(); + + findForm().trigger('submit'); + await waitForPromises(); + + expect(findError().exists()).toBe(true); + }); + + it('should show error on error response', async () => { + queryHandler = jest.fn().mockResolvedValue(createContactMutationErrorResponse); + mountComponent(); + + findForm().trigger('submit'); + await waitForPromises(); + + expect(findError().exists()).toBe(true); + expect(findError().text()).toBe('Phone is invalid.'); + }); + }); + + 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); + expect(findError().text()).toBe('Email is invalid.'); + }); + }); +}); diff --git a/spec/frontend/crm/contacts_root_spec.js b/spec/frontend/crm/contacts_root_spec.js index 18bd2d7c45b..b30349305a3 100644 --- a/spec/frontend/crm/contacts_root_spec.js +++ b/spec/frontend/crm/contacts_root_spec.js @@ -6,8 +6,10 @@ import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_help import createMockApollo from 'helpers/mock_apollo_helper'; import waitForPromises from 'helpers/wait_for_promises'; import ContactsRoot from '~/crm/components/contacts_root.vue'; -import NewContactForm from '~/crm/components/new_contact_form.vue'; +import ContactForm from '~/crm/components/contact_form.vue'; import getGroupContactsQuery from '~/crm/components/queries/get_group_contacts.query.graphql'; +import { NEW_ROUTE_NAME, EDIT_ROUTE_NAME } from '~/crm/constants'; +import routes from '~/crm/routes'; import { getGroupContactsQueryResponse } from './mock_data'; describe('Customer relations contacts root app', () => { @@ -21,7 +23,8 @@ describe('Customer relations contacts root app', () => { const findRowByName = (rowName) => wrapper.findAllByRole('row', { name: rowName }); const findIssuesLinks = () => wrapper.findAllByTestId('issues-link'); const findNewContactButton = () => wrapper.findByTestId('new-contact-button'); - const findNewContactForm = () => wrapper.findComponent(NewContactForm); + const findEditContactButton = () => wrapper.findByTestId('edit-contact-button'); + const findContactForm = () => wrapper.findComponent(ContactForm); const findError = () => wrapper.findComponent(GlAlert); const successQueryHandler = jest.fn().mockResolvedValue(getGroupContactsQueryResponse); @@ -49,7 +52,7 @@ describe('Customer relations contacts root app', () => { router = new VueRouter({ base: basePath, mode: 'history', - routes: [], + routes, }); }); @@ -79,12 +82,12 @@ describe('Customer relations contacts root app', () => { }); }); - describe('new contact form', () => { + describe('contact form', () => { it('should not exist by default', async () => { mountComponent(); await waitForPromises(); - expect(findNewContactForm().exists()).toBe(false); + expect(findContactForm().exists()).toBe(false); }); it('should exist when user clicks new contact button', async () => { @@ -93,25 +96,54 @@ describe('Customer relations contacts root app', () => { findNewContactButton().vm.$emit('click'); await waitForPromises(); - expect(findNewContactForm().exists()).toBe(true); + expect(findContactForm().exists()).toBe(true); }); - it('should exist when user navigates directly to /new', async () => { - router.replace({ path: '/new' }); + it('should exist when user navigates directly to `new` route', async () => { + router.replace({ name: NEW_ROUTE_NAME }); mountComponent(); await waitForPromises(); - expect(findNewContactForm().exists()).toBe(true); + expect(findContactForm().exists()).toBe(true); }); - it('should not exist when form emits close', async () => { - router.replace({ path: '/new' }); + it('should exist when user clicks edit contact button', async () => { + mountComponent({ mountFunction: mountExtended }); + await waitForPromises(); + + findEditContactButton().vm.$emit('click'); + await waitForPromises(); + + expect(findContactForm().exists()).toBe(true); + }); + + it('should exist when user navigates directly to `edit` route', async () => { + router.replace({ name: EDIT_ROUTE_NAME, params: { id: 16 } }); mountComponent(); + await waitForPromises(); + + expect(findContactForm().exists()).toBe(true); + }); + + it('should not exist when new form emits close', async () => { + router.replace({ name: NEW_ROUTE_NAME }); + mountComponent(); + + findContactForm().vm.$emit('close'); + await waitForPromises(); + + expect(findContactForm().exists()).toBe(false); + }); + + it('should not exist when edit form emits close', async () => { + router.replace({ name: EDIT_ROUTE_NAME, params: { id: 16 } }); + mountComponent(); + await waitForPromises(); - findNewContactForm().vm.$emit('close'); + findContactForm().vm.$emit('close'); await waitForPromises(); - expect(findNewContactForm().exists()).toBe(false); + expect(findContactForm().exists()).toBe(false); }); }); diff --git a/spec/frontend/crm/mock_data.js b/spec/frontend/crm/mock_data.js index e784ac3764d..3abbc488081 100644 --- a/spec/frontend/crm/mock_data.js +++ b/spec/frontend/crm/mock_data.js @@ -106,3 +106,31 @@ export const createContactMutationErrorResponse = { }, }, }; + +export const updateContactMutationResponse = { + data: { + customerRelationsContactUpdate: { + __typeName: 'CustomerRelationsContactCreatePayload', + contact: { + __typename: 'CustomerRelationsContact', + id: 'gid://gitlab/CustomerRelations::Contact/1', + firstName: 'First', + lastName: 'Last', + email: 'email@example.com', + phone: null, + description: null, + organization: null, + }, + errors: [], + }, + }, +}; + +export const updateContactMutationErrorResponse = { + data: { + customerRelationsContactUpdate: { + contact: null, + errors: ['Email is invalid.'], + }, + }, +}; diff --git a/spec/frontend/crm/new_contact_form_spec.js b/spec/frontend/crm/new_contact_form_spec.js deleted file mode 100644 index 1497c348676..00000000000 --- a/spec/frontend/crm/new_contact_form_spec.js +++ /dev/null @@ -1,110 +0,0 @@ -import { GlAlert } from '@gitlab/ui'; -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'; -import NewContactForm from '~/crm/components/new_contact_form.vue'; -import createContactMutation from '~/crm/components/queries/create_contact.mutation.graphql'; -import getGroupContactsQuery from '~/crm/components/queries/get_group_contacts.query.graphql'; -import { - createContactMutationErrorResponse, - createContactMutationResponse, - getGroupContactsQueryResponse, -} from './mock_data'; - -describe('Customer relations contacts root app', () => { - Vue.use(VueApollo); - let wrapper; - let fakeApollo; - let queryHandler; - - const findCreateNewContactButton = () => wrapper.findByTestId('create-new-contact-button'); - const findCancelButton = () => wrapper.findByTestId('cancel-button'); - const findForm = () => wrapper.find('form'); - const findError = () => wrapper.findComponent(GlAlert); - - const mountComponent = ({ mountFunction = shallowMountExtended } = {}) => { - fakeApollo = createMockApollo([[createContactMutation, queryHandler]]); - fakeApollo.clients.defaultClient.cache.writeQuery({ - query: getGroupContactsQuery, - variables: { groupFullPath: 'flightjs' }, - data: getGroupContactsQueryResponse.data, - }); - wrapper = mountFunction(NewContactForm, { - provide: { groupId: 26, groupFullPath: 'flightjs' }, - apolloProvider: fakeApollo, - propsData: { drawerOpen: true }, - }); - }; - - beforeEach(() => { - queryHandler = jest.fn().mockResolvedValue(createContactMutationResponse); - }); - - afterEach(() => { - wrapper.destroy(); - fakeApollo = null; - }); - - describe('Create new contact button', () => { - it('should be disabled by default', () => { - mountComponent(); - - expect(findCreateNewContactButton().attributes('disabled')).toBeTruthy(); - }); - - it('should not be disabled when first, last and email have values', async () => { - 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(); - - expect(findCreateNewContactButton().attributes('disabled')).toBeFalsy(); - }); - }); - - it("should emit 'close' when cancel button is clicked", () => { - mountComponent(); - - findCancelButton().vm.$emit('click'); - - expect(wrapper.emitted().close).toBeTruthy(); - }); - - describe('when query is successful', () => { - it("should emit 'close'", async () => { - mountComponent(); - - findForm().trigger('submit'); - await waitForPromises(); - - expect(wrapper.emitted().close).toBeTruthy(); - }); - }); - - describe('when query fails', () => { - it('should show error on reject', async () => { - queryHandler = jest.fn().mockRejectedValue('ERROR'); - mountComponent(); - - findForm().trigger('submit'); - await waitForPromises(); - - expect(findError().exists()).toBe(true); - }); - - it('should show error on error response', async () => { - queryHandler = jest.fn().mockResolvedValue(createContactMutationErrorResponse); - mountComponent(); - - findForm().trigger('submit'); - await waitForPromises(); - - expect(findError().exists()).toBe(true); - expect(findError().text()).toBe('Phone is invalid.'); - }); - }); -}); -- cgit v1.2.3