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

entity_select_spec.js « entity_select « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1376133ec37ca2c5c864d009eb9b40ea29d148fd (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { nextTick } from 'vue';
import { GlCollapsibleListbox, GlFormGroup } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import EntitySelect from '~/vue_shared/components/entity_select/entity_select.vue';
import { QUERY_TOO_SHORT_MESSAGE } from '~/vue_shared/components/entity_select/constants';
import waitForPromises from 'helpers/wait_for_promises';

describe('EntitySelect', () => {
  let wrapper;
  let fetchItemsMock;
  let fetchInitialSelectionTextMock;

  // Mocks
  const itemMock = {
    text: 'selectedGroup',
    value: '1',
  };

  // Stubs
  const GlAlert = {
    template: '<div><slot /></div>',
  };

  // Props
  const label = 'label';
  const description = 'description';
  const inputName = 'inputName';
  const inputId = 'inputId';
  const headerText = 'headerText';
  const defaultToggleText = 'defaultToggleText';
  const toggleClass = 'foo-bar';
  const block = true;

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

  // Helpers
  const createComponent = ({ props = {}, slots = {}, stubs = {} } = {}) => {
    wrapper = shallowMountExtended(EntitySelect, {
      propsData: {
        label,
        description,
        inputName,
        inputId,
        headerText,
        defaultToggleText,
        fetchItems: fetchItemsMock,
        toggleClass,
        block,
        ...props,
      },
      stubs: {
        GlAlert,
        EntitySelect,
        ...stubs,
      },
      slots,
    });
  };
  const openListbox = () => findListbox().vm.$emit('shown');
  const search = (searchString) => findListbox().vm.$emit('search', searchString);
  const selectGroup = async () => {
    openListbox();
    await nextTick();
    findListbox().vm.$emit('select', itemMock.value);
    return nextTick();
  };

  beforeEach(() => {
    fetchItemsMock = jest.fn().mockImplementation(() => ({ items: [itemMock], totalPages: 1 }));
  });

  describe('GlCollapsibleListbox props', () => {
    beforeEach(() => {
      createComponent();
    });

    it.each`
      prop             | expectedValue
      ${'block'}       | ${block}
      ${'toggleClass'} | ${toggleClass}
      ${'headerText'}  | ${headerText}
    `('passes the $prop prop to GlCollapsibleListbox', ({ prop, expectedValue }) => {
      expect(findListbox().props(prop)).toBe(expectedValue);
    });
  });

  describe('on mount', () => {
    it('calls the fetch function when the listbox is opened', async () => {
      createComponent();
      openListbox();
      await nextTick();

      expect(fetchItemsMock).toHaveBeenCalledTimes(1);
    });

    it("fetches the initially selected value's name", async () => {
      fetchInitialSelectionTextMock = jest.fn().mockImplementation(() => itemMock.text);
      createComponent({
        props: {
          fetchInitialSelectionText: fetchInitialSelectionTextMock,
          initialSelection: itemMock.value,
        },
      });
      await nextTick();

      expect(fetchInitialSelectionTextMock).toHaveBeenCalledTimes(1);
      expect(findListbox().props('toggleText')).toBe(itemMock.text);
    });
  });

  it("renders the error slot's content", () => {
    const selector = 'data-test-id="error-element"';
    createComponent({
      slots: {
        error: `<div ${selector} />`,
      },
    });

    expect(wrapper.find(`[${selector}]`).exists()).toBe(true);
  });

  it('renders the label slot if provided', () => {
    const testid = 'label-slot';
    createComponent({
      slots: {
        label: `<div data-testid="${testid}" />`,
      },
      stubs: {
        GlFormGroup,
      },
    });

    expect(wrapper.findByTestId(testid).exists()).toBe(true);
  });

  it('passes description prop to GlFormGroup', () => {
    createComponent();

    expect(wrapper.findComponent(GlFormGroup).attributes('description')).toBe(description);
  });

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

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

    describe('once a group is selected', () => {
      it('emits `input` event with the select value', async () => {
        createComponent();
        await selectGroup();

        expect(wrapper.emitted('input')[0][0]).toMatchObject(itemMock);
      });

      it(`uses the selected group's name as the toggle text`, async () => {
        createComponent();
        await selectGroup();

        expect(findListbox().props('toggleText')).toBe(itemMock.text);
      });

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

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

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

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

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

      it('emits `input` event with an empty object on reset', async () => {
        createComponent();
        await selectGroup();

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

        expect(Object.keys(wrapper.emitted('input')[2][0]).length).toBe(0);
      });
    });
  });

  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 () => {
      createComponent();

      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';
      createComponent();
      openListbox();

      expect(fetchItemsMock).toHaveBeenCalledTimes(1);

      fetchItemsMock.mockImplementation(() => ({ items: [], totalPages: 1 }));
      search(searchString);
      await nextTick();

      expect(fetchItemsMock).toHaveBeenCalledTimes(2);
    });

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

      expect(fetchItemsMock).toHaveBeenCalledTimes(1);
      expect(findListbox().props('noResultsText')).toBe(QUERY_TOO_SHORT_MESSAGE);
    });
  });

  describe('pagination', () => {
    const searchString = 'searchString';

    beforeEach(() => {
      let requestCount = 0;
      fetchItemsMock.mockImplementation((searchQuery, page) => {
        requestCount += 1;
        return {
          items: [
            {
              text: `Group [page: ${page} - search: ${searchQuery}]`,
              value: `id:${requestCount}`,
            },
          ],
          totalPages: 3,
        };
      });
      createComponent();
      openListbox();
      findListbox().vm.$emit('bottom-reached');
      return nextTick();
    });

    it('fetches the next page when bottom is reached', () => {
      expect(fetchItemsMock).toHaveBeenCalledTimes(2);
      expect(fetchItemsMock).toHaveBeenLastCalledWith('', 2);
    });

    it('fetches the first page when the search query changes', async () => {
      search(searchString);
      await nextTick();

      expect(fetchItemsMock).toHaveBeenCalledTimes(3);
      expect(fetchItemsMock).toHaveBeenLastCalledWith(searchString, 1);
    });

    it('retains the search query when infinite scrolling', async () => {
      search(searchString);
      await nextTick();
      findListbox().vm.$emit('bottom-reached');
      await nextTick();

      expect(fetchItemsMock).toHaveBeenCalledTimes(4);
      expect(fetchItemsMock).toHaveBeenLastCalledWith(searchString, 2);
    });

    it('pauses infinite scroll after fetching the last page', async () => {
      expect(findListbox().props('infiniteScroll')).toBe(true);

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

      expect(findListbox().props('infiniteScroll')).toBe(false);
    });

    it('resumes infinite scroll when search query changes', async () => {
      findListbox().vm.$emit('bottom-reached');
      await waitForPromises();

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

      search(searchString);
      await waitForPromises();

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