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

group_select_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: bd90832f497ea434b0b797a21db7c95be4fb3ab3 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { nextTick } from 'vue';
import { GlAvatarLabeled, GlCollapsibleListbox } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { getGroups } from '~/api/groups_api';
import GroupSelect from '~/invite_members/components/group_select.vue';

jest.mock('~/api/groups_api');

const group1 = { id: 1, full_name: 'Group One', avatar_url: 'test' };
const group2 = { id: 2, full_name: 'Group Two', avatar_url: 'test' };
const allGroups = [group1, group2];
const headers = {
  'X-Next-Page': 2,
  'X-Page': 1,
  'X-Per-Page': 20,
  'X-Prev-Page': '',
  'X-Total': 40,
  'X-Total-Pages': 2,
};

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

  const createComponent = (props = {}) => {
    wrapper = mount(GroupSelect, {
      propsData: {
        selectedGroup: {},
        invalidGroups: [],
        ...props,
      },
    });
  };

  beforeEach(() => {
    getGroups.mockResolvedValueOnce({ data: allGroups, headers });
  });

  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findListboxToggle = () => findListbox().find('button[aria-haspopup="listbox"]');
  const findAvatarByLabel = (text) =>
    wrapper
      .findAllComponents(GlAvatarLabeled)
      .wrappers.find((dropdownItemWrapper) => dropdownItemWrapper.props('label') === text);

  describe('when user types in the search input', () => {
    beforeEach(async () => {
      createComponent();
      await waitForPromises();
      getGroups.mockClear();
      getGroups.mockReturnValueOnce(new Promise(() => {}));
      findListbox().vm.$emit('search', group1.name);
      await nextTick();
    });

    it('calls the API', () => {
      expect(getGroups).toHaveBeenCalledWith(group1.name, {
        exclude_internal: true,
        active: true,
        order_by: 'similarity',
      });
    });

    it('displays loading icon while waiting for API call to resolve', () => {
      expect(findListbox().props('searching')).toBe(true);
    });
  });

  describe('avatar label', () => {
    it('includes the correct attributes with name and avatar_url', async () => {
      createComponent();
      await waitForPromises();

      expect(findAvatarByLabel(group1.full_name).attributes()).toMatchObject({
        src: group1.avatar_url,
        'entity-id': `${group1.id}`,
        'entity-name': group1.full_name,
        size: '32',
      });
    });

    describe('when filtering out the group from results', () => {
      beforeEach(async () => {
        createComponent({ invalidGroups: [group1.id] });
        await waitForPromises();
      });

      it('does not find an invalid group', () => {
        expect(findAvatarByLabel(group1.full_name)).toBe(undefined);
      });

      it('finds a group that is valid', () => {
        expect(findAvatarByLabel(group2.full_name).exists()).toBe(true);
      });
    });
  });

  describe('when group is selected from the dropdown', () => {
    beforeEach(async () => {
      createComponent({
        selectedGroup: {
          value: group1.id,
          id: group1.id,
          name: group1.full_name,
          path: group1.path,
          avatarUrl: group1.avatar_url,
        },
      });
      await waitForPromises();
      findListbox().vm.$emit('select', group1.id);
      await nextTick();
    });

    it('emits `input` event used by `v-model`', () => {
      expect(wrapper.emitted('input')).toMatchObject([
        [
          {
            value: group1.id,
            id: group1.id,
            name: group1.full_name,
            path: group1.path,
            avatarUrl: group1.avatar_url,
          },
        ],
      ]);
    });

    it('sets dropdown toggle text to selected item', () => {
      expect(findListboxToggle().text()).toBe(group1.full_name);
    });
  });

  describe('infinite scroll', () => {
    it('sets infinite scroll related props', async () => {
      createComponent();
      await waitForPromises();

      expect(findListbox().props()).toMatchObject({
        infiniteScroll: true,
        infiniteScrollLoading: false,
        totalItems: 40,
      });
    });

    describe('when `bottom-reached` event is fired', () => {
      it('indicates new groups are loading and adds them to the listbox', async () => {
        createComponent();
        await waitForPromises();

        const infiniteScrollGroup = {
          id: 3,
          full_name: 'Infinite scroll group',
          avatar_url: 'test',
        };

        getGroups.mockResolvedValueOnce({ data: [infiniteScrollGroup], headers });

        findListbox().vm.$emit('bottom-reached');
        await nextTick();

        expect(findListbox().props('infiniteScrollLoading')).toBe(true);

        await waitForPromises();

        expect(findListbox().props('items')[2]).toMatchObject({
          value: infiniteScrollGroup.id,
          id: infiniteScrollGroup.id,
          name: infiniteScrollGroup.full_name,
          avatarUrl: infiniteScrollGroup.avatar_url,
        });
      });

      describe('when API request fails', () => {
        it('emits `error` event', async () => {
          createComponent();
          await waitForPromises();

          getGroups.mockRejectedValueOnce();

          findListbox().vm.$emit('bottom-reached');
          await waitForPromises();

          expect(wrapper.emitted('error')).toEqual([[GroupSelect.i18n.errorMessage]]);
        });
      });
    });
  });
});