summaryrefslogtreecommitdiff
path: root/spec/frontend/invite_members/components/invite_members_modal_spec.js
blob: 4ac2a28105c6efe2fcc1a00bfe90b742f3c5891b (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
158
159
160
161
162
163
164
165
166
import { shallowMount } from '@vue/test-utils';
import { GlDropdown, GlDropdownItem, GlDatepicker, GlSprintf, GlLink } from '@gitlab/ui';
import Api from '~/api';
import InviteMembersModal from '~/invite_members/components/invite_members_modal.vue';

const id = '1';
const name = 'testgroup';
const isProject = false;
const accessLevels = { Guest: 10, Reporter: 20, Developer: 30, Maintainer: 40, Owner: 50 };
const defaultAccessLevel = '10';
const helpLink = 'https://example.com';

const createComponent = (data = {}) => {
  return shallowMount(InviteMembersModal, {
    propsData: {
      id,
      name,
      isProject,
      accessLevels,
      defaultAccessLevel,
      helpLink,
    },
    data() {
      return data;
    },
    stubs: {
      'gl-modal': '<div><slot name="modal-footer"></slot><slot></slot></div>',
      'gl-dropdown': true,
      'gl-dropdown-item': true,
      GlSprintf,
    },
  });
};

describe('InviteMembersModal', () => {
  let wrapper;

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

  const findDropdown = () => wrapper.find(GlDropdown);
  const findDropdownItems = () => findDropdown().findAll(GlDropdownItem);
  const findDatepicker = () => wrapper.find(GlDatepicker);
  const findLink = () => wrapper.find(GlLink);
  const findCancelButton = () => wrapper.find({ ref: 'cancelButton' });
  const findInviteButton = () => wrapper.find({ ref: 'inviteButton' });

  describe('rendering the modal', () => {
    beforeEach(() => {
      wrapper = createComponent();
    });

    it('renders the modal with the correct title', () => {
      expect(wrapper.attributes('title')).toBe('Invite team members');
    });

    it('renders the Cancel button text correctly', () => {
      expect(findCancelButton().text()).toBe('Cancel');
    });

    it('renders the Invite button text correctly', () => {
      expect(findInviteButton().text()).toBe('Invite');
    });

    describe('rendering the access levels dropdown', () => {
      it('sets the default dropdown text to the default access level name', () => {
        expect(findDropdown().attributes('text')).toBe('Guest');
      });

      it('renders dropdown items for each accessLevel', () => {
        expect(findDropdownItems()).toHaveLength(5);
      });
    });

    describe('rendering the help link', () => {
      it('renders the correct link', () => {
        expect(findLink().attributes('href')).toBe(helpLink);
      });
    });

    describe('rendering the access expiration date field', () => {
      it('renders the datepicker', () => {
        expect(findDatepicker()).toExist();
      });
    });
  });

  describe('submitting the invite form', () => {
    const postData = {
      user_id: '1',
      access_level: '10',
      expires_at: new Date(),
      format: 'json',
    };

    describe('when the invite was sent successfully', () => {
      beforeEach(() => {
        wrapper = createComponent();

        wrapper.vm.$toast = { show: jest.fn() };
        jest.spyOn(Api, 'inviteGroupMember').mockResolvedValue({ data: postData });

        wrapper.vm.submitForm(postData);
      });

      it('displays the successful toastMessage', () => {
        const toastMessageSuccessful = 'Members were successfully added';

        expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(
          toastMessageSuccessful,
          wrapper.vm.toastOptions,
        );
      });

      it('calls Api inviteGroupMember with the correct params', () => {
        expect(Api.inviteGroupMember).toHaveBeenCalledWith(id, postData);
      });
    });

    describe('when sending the invite for a single member returned an api error', () => {
      const apiErrorMessage = 'Members already exists';

      beforeEach(() => {
        wrapper = createComponent({ newUsersToInvite: '123' });

        wrapper.vm.$toast = { show: jest.fn() };
        jest
          .spyOn(Api, 'inviteGroupMember')
          .mockRejectedValue({ response: { data: { message: apiErrorMessage } } });

        findInviteButton().vm.$emit('click');
      });

      it('displays the api error message for the toastMessage', () => {
        expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(
          apiErrorMessage,
          wrapper.vm.toastOptions,
        );
      });
    });

    describe('when sending the invite for multiple members returned any error', () => {
      const genericErrorMessage = 'Some of the members could not be added';

      beforeEach(() => {
        wrapper = createComponent({ newUsersToInvite: '123' });

        wrapper.vm.$toast = { show: jest.fn() };
        jest
          .spyOn(Api, 'inviteGroupMember')
          .mockRejectedValue({ response: { data: { success: false } } });

        findInviteButton().vm.$emit('click');
      });

      it('displays the expected toastMessage', () => {
        expect(wrapper.vm.$toast.show).toHaveBeenCalledWith(
          genericErrorMessage,
          wrapper.vm.toastOptions,
        );
      });
    });
  });
});