summaryrefslogtreecommitdiff
path: root/spec/frontend/crm/contact_form_spec.js
blob: b2753ad8cf5f79334811db9a7fd26190d3d6d1cc (plain)
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
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.');
    });
  });
});