summaryrefslogtreecommitdiff
path: root/spec/frontend/crm/contact_form_wrapper_spec.js
blob: 6307889a7aaecd0763e9723c81a3ac29e24f5cf4 (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
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContactFormWrapper from '~/crm/contacts/components/contact_form_wrapper.vue';
import ContactForm from '~/crm/components/form.vue';
import getGroupContactsQuery from '~/crm/contacts/components/graphql/get_group_contacts.query.graphql';
import createContactMutation from '~/crm/contacts/components/graphql/create_contact.mutation.graphql';
import updateContactMutation from '~/crm/contacts/components/graphql/update_contact.mutation.graphql';

describe('Customer relations contact form wrapper', () => {
  let wrapper;

  const findContactForm = () => wrapper.findComponent(ContactForm);

  const $apollo = {
    queries: {
      contacts: {
        loading: false,
      },
    },
  };
  const $route = {
    params: {
      id: 7,
    },
  };
  const contacts = [{ id: 'gid://gitlab/CustomerRelations::Contact/7' }];

  const mountComponent = ({ isEditMode = false } = {}) => {
    wrapper = shallowMountExtended(ContactFormWrapper, {
      propsData: {
        isEditMode,
      },
      provide: {
        groupFullPath: 'flightjs',
        groupId: 26,
      },
      mocks: {
        $apollo,
        $route,
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  describe('in edit mode', () => {
    it('should render contact form with correct props', () => {
      mountComponent({ isEditMode: true });

      const contactForm = findContactForm();
      expect(contactForm.props('fields')).toHaveLength(5);
      expect(contactForm.props('title')).toBe('Edit contact');
      expect(contactForm.props('successMessage')).toBe('Contact has been updated.');
      expect(contactForm.props('mutation')).toBe(updateContactMutation);
      expect(contactForm.props('getQuery')).toMatchObject({
        query: getGroupContactsQuery,
        variables: { groupFullPath: 'flightjs' },
      });
      expect(contactForm.props('getQueryNodePath')).toBe('group.contacts');
      expect(contactForm.props('existingId')).toBe(contacts[0].id);
      expect(contactForm.props('additionalCreateParams')).toMatchObject({
        groupId: 'gid://gitlab/Group/26',
      });
    });
  });

  describe('in create mode', () => {
    it('should render contact form with correct props', () => {
      mountComponent();

      const contactForm = findContactForm();
      expect(contactForm.props('fields')).toHaveLength(5);
      expect(contactForm.props('title')).toBe('New contact');
      expect(contactForm.props('successMessage')).toBe('Contact has been added.');
      expect(contactForm.props('mutation')).toBe(createContactMutation);
      expect(contactForm.props('getQuery')).toMatchObject({
        query: getGroupContactsQuery,
        variables: { groupFullPath: 'flightjs' },
      });
      expect(contactForm.props('getQueryNodePath')).toBe('group.contacts');
      expect(contactForm.props('existingId')).toBeNull();
      expect(contactForm.props('additionalCreateParams')).toMatchObject({
        groupId: 'gid://gitlab/Group/26',
      });
    });
  });
});