summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/group_select/group_select_spec.js
blob: c10b32c6acc3e8ffe5796a3eadc8fc48850370a4 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { nextTick } from 'vue';
import { GlCollapsibleListbox } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import axios from '~/lib/utils/axios_utils';
import { createAlert } from '~/flash';
import GroupSelect from '~/vue_shared/components/group_select/group_select.vue';
import {
  TOGGLE_TEXT,
  FETCH_GROUPS_ERROR,
  FETCH_GROUP_ERROR,
  QUERY_TOO_SHORT_MESSAGE,
} from '~/vue_shared/components/group_select/constants';
import waitForPromises from 'helpers/wait_for_promises';

jest.mock('~/flash');

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

  // Mocks
  const groupMock = {
    full_name: 'selectedGroup',
    id: '1',
  };
  const groupEndpoint = `/api/undefined/groups/${groupMock.id}`;

  // Props
  const inputName = 'inputName';
  const inputId = 'inputId';

  // Finders
  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findInput = () => wrapper.findByTestId('input');

  // Helpers
  const createComponent = ({ props = {} } = {}) => {
    wrapper = shallowMountExtended(GroupSelect, {
      propsData: {
        inputName,
        inputId,
        ...props,
      },
    });
  };
  const openListbox = () => findListbox().vm.$emit('shown');
  const search = (searchString) => findListbox().vm.$emit('search', searchString);
  const createComponentWithGroups = () => {
    mock.onGet('/api/undefined/groups.json').reply(200, [groupMock]);
    createComponent();
    openListbox();
    return waitForPromises();
  };
  const selectGroup = () => {
    findListbox().vm.$emit('select', groupMock.id);
    return nextTick();
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
  });

  afterEach(() => {
    mock.restore();
  });

  describe('on mount', () => {
    it('fetches groups when the listbox is opened', async () => {
      createComponent();
      await waitForPromises();

      expect(mock.history.get).toHaveLength(0);

      openListbox();
      await waitForPromises();

      expect(mock.history.get).toHaveLength(1);
    });

    describe('with an initial selection', () => {
      it('if the selected group is not part of the fetched list, fetches it individually', async () => {
        mock.onGet(groupEndpoint).reply(200, groupMock);
        createComponent({ props: { initialSelection: groupMock.id } });
        await waitForPromises();

        expect(mock.history.get).toHaveLength(1);
        expect(findListbox().props('toggleText')).toBe(groupMock.full_name);
      });

      it('show an error if fetching the individual group fails', async () => {
        mock
          .onGet('/api/undefined/groups.json')
          .reply(200, [{ full_name: 'notTheSelectedGroup', id: '2' }]);
        mock.onGet(groupEndpoint).reply(500);
        createComponent({ props: { initialSelection: groupMock.id } });
        await waitForPromises();

        expect(createAlert).toHaveBeenCalledWith({
          message: FETCH_GROUP_ERROR,
          error: expect.any(Error),
          parent: wrapper.vm.$el,
        });
      });
    });
  });

  it('shows an error when fetching groups fails', async () => {
    mock.onGet('/api/undefined/groups.json').reply(500);
    createComponent();
    openListbox();
    await waitForPromises();

    expect(createAlert).toHaveBeenCalledWith({
      message: FETCH_GROUPS_ERROR,
      error: expect.any(Error),
      parent: wrapper.vm.$el,
    });
  });

  describe('selection', () => {
    it('uses the default toggle text while no group is selected', async () => {
      await createComponentWithGroups();

      expect(findListbox().props('toggleText')).toBe(TOGGLE_TEXT);
    });

    describe('once a group is selected', () => {
      it(`uses the selected group's name as the toggle text`, async () => {
        await createComponentWithGroups();
        await selectGroup();

        expect(findListbox().props('toggleText')).toBe(groupMock.full_name);
      });

      it(`uses the selected group's ID as the listbox' and input value`, async () => {
        await createComponentWithGroups();
        await selectGroup();

        expect(findListbox().attributes('selected')).toBe(groupMock.id);
        expect(findInput().attributes('value')).toBe(groupMock.id);
      });

      it(`on reset, falls back to the default toggle text`, async () => {
        await createComponentWithGroups();
        await selectGroup();

        findListbox().vm.$emit('reset');
        await nextTick();

        expect(findListbox().props('toggleText')).toBe(TOGGLE_TEXT);
      });
    });
  });

  describe('search', () => {
    it('sets `searching` to `true` when first opening the dropdown', async () => {
      createComponent();

      expect(findListbox().props('searching')).toBe(false);

      openListbox();
      await nextTick();

      expect(findListbox().props('searching')).toBe(true);
    });

    it('sets `searching` to `true` while searching', async () => {
      await createComponentWithGroups();

      expect(findListbox().props('searching')).toBe(false);

      search('foo');
      await nextTick();

      expect(findListbox().props('searching')).toBe(true);
    });

    it('fetches groups matching the search string', async () => {
      const searchString = 'searchString';
      await createComponentWithGroups();

      expect(mock.history.get).toHaveLength(1);

      search(searchString);
      await waitForPromises();

      expect(mock.history.get).toHaveLength(2);
      expect(mock.history.get[1].params).toStrictEqual({ search: searchString });
    });

    it('shows a notice if the search query is too short', async () => {
      const searchString = 'a';
      await createComponentWithGroups();
      search(searchString);
      await waitForPromises();

      expect(mock.history.get).toHaveLength(1);
      expect(findListbox().props('noResultsText')).toBe(QUERY_TOO_SHORT_MESSAGE);
    });
  });
});