Welcome to mirror list, hosted at ThFree Co, Russian Federation.

invite_groups_modal_spec.js « components « invite_members « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49c55d56080cf1685856d82fee8a5a933973c0d8 (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
import { GlModal, GlSprintf } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import Api from '~/api';
import InviteGroupsModal from '~/invite_members/components/invite_groups_modal.vue';
import InviteModalBase from '~/invite_members/components/invite_modal_base.vue';
import GroupSelect from '~/invite_members/components/group_select.vue';
import { stubComponent } from 'helpers/stub_component';
import { propsData, sharedGroup } from '../mock_data/group_modal';

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

  const createComponent = (props = {}) => {
    wrapper = shallowMountExtended(InviteGroupsModal, {
      propsData: {
        ...propsData,
        ...props,
      },
      stubs: {
        InviteModalBase,
        GlSprintf,
        GlModal: stubComponent(GlModal, {
          template: '<div><slot></slot><slot name="modal-footer"></slot></div>',
        }),
      },
    });
  };

  const createInviteGroupToProjectWrapper = () => {
    createComponent({ isProject: true });
  };

  const createInviteGroupToGroupWrapper = () => {
    createComponent({ isProject: false });
  };

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

  const findGroupSelect = () => wrapper.findComponent(GroupSelect);
  const findIntroText = () => wrapper.findByTestId('modal-base-intro-text').text();
  const findCancelButton = () => wrapper.findByTestId('cancel-button');
  const findInviteButton = () => wrapper.findByTestId('invite-button');
  const findMembersFormGroup = () => wrapper.findByTestId('members-form-group');
  const membersFormGroupInvalidFeedback = () =>
    findMembersFormGroup().attributes('invalid-feedback');
  const clickInviteButton = () => findInviteButton().vm.$emit('click');
  const clickCancelButton = () => findCancelButton().vm.$emit('click');
  const triggerGroupSelect = (val) => findGroupSelect().vm.$emit('input', val);

  describe('displaying the correct introText and form group description', () => {
    describe('when inviting to a project', () => {
      it('includes the correct type, and formatted intro text', () => {
        createInviteGroupToProjectWrapper();

        expect(findIntroText()).toBe("You're inviting a group to the test name project.");
      });
    });

    describe('when inviting to a group', () => {
      it('includes the correct type, and formatted intro text', () => {
        createInviteGroupToGroupWrapper();

        expect(findIntroText()).toBe("You're inviting a group to the test name group.");
      });
    });
  });

  describe('submitting the invite form', () => {
    describe('when sharing the group is successful', () => {
      const groupPostData = {
        group_id: sharedGroup.id,
        group_access: propsData.defaultAccessLevel,
        expires_at: undefined,
        format: 'json',
      };

      beforeEach(() => {
        createComponent();
        triggerGroupSelect(sharedGroup);

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

        clickInviteButton();
      });

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

      it('displays the successful toastMessage', () => {
        expect(wrapper.vm.$toast.show).toHaveBeenCalledWith('Members were successfully added', {
          onComplete: expect.any(Function),
        });
      });
    });

    describe('when sharing the group fails', () => {
      beforeEach(() => {
        createInviteGroupToGroupWrapper();
        triggerGroupSelect(sharedGroup);

        wrapper.vm.$toast = { show: jest.fn() };

        jest
          .spyOn(Api, 'groupShareWithGroup')
          .mockRejectedValue({ response: { data: { success: false } } });

        clickInviteButton();
      });

      it('does not show the toast message on failure', () => {
        expect(wrapper.vm.$toast.show).not.toHaveBeenCalled();
      });

      it('displays the generic error for http server error', () => {
        expect(membersFormGroupInvalidFeedback()).toBe('Something went wrong');
      });

      describe('clearing the invalid state and message', () => {
        it('clears the error when the cancel button is clicked', async () => {
          clickCancelButton();

          await nextTick();

          expect(membersFormGroupInvalidFeedback()).toBe('');
        });

        it('clears the error when the modal is hidden', async () => {
          wrapper.findComponent(GlModal).vm.$emit('hide');

          await nextTick();

          expect(membersFormGroupInvalidFeedback()).toBe('');
        });
      });
    });
  });
});