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

deprecated_jquery_dropdown_spec.js « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 439c20e0fb599305a5747474f8f5a33a88c62099 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* eslint-disable no-param-reassign */

import $ from 'jquery';
import mockProjects from 'test_fixtures_static/projects.json';
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
import '~/lib/utils/common_utils';
import { visitUrl } from '~/lib/utils/url_utility';

jest.mock('~/lib/utils/url_utility', () => ({
  visitUrl: jest.fn().mockName('visitUrl'),
}));

describe('deprecatedJQueryDropdown', () => {
  const NON_SELECTABLE_CLASSES =
    '.divider, .separator, .dropdown-header, .dropdown-menu-empty-item';
  const SEARCH_INPUT_SELECTOR = '.dropdown-input-field';
  const ITEM_SELECTOR = `.dropdown-content li:not(${NON_SELECTABLE_CLASSES})`;
  const FOCUSED_ITEM_SELECTOR = `${ITEM_SELECTOR} a.is-focused`;
  const ARROW_KEYS = {
    DOWN: 40,
    UP: 38,
    ENTER: 13,
    ESC: 27,
  };

  let remoteCallback;

  const test = {};

  const navigateWithKeys = (direction, steps, cb, i) => {
    i = i || 0;
    if (!i) direction = direction.toUpperCase();
    $('body').trigger({
      type: 'keydown',
      which: ARROW_KEYS[direction],
      keyCode: ARROW_KEYS[direction],
    });
    i += 1;
    if (i <= steps) {
      navigateWithKeys(direction, steps, cb, i);
    } else {
      cb();
    }
  };

  const remoteMock = (data, term, callback) => {
    remoteCallback = callback.bind({}, data);
  };

  function initDropDown(hasRemote, isFilterable, extraOpts = {}) {
    const options = {
      selectable: true,
      filterable: isFilterable,
      data: hasRemote ? remoteMock.bind({}, test.projectsData) : test.projectsData,
      search: {
        fields: ['name'],
      },
      text: (project) => project.name_with_namespace || project.name,
      id: (project) => project.id,
      ...extraOpts,
    };
    test.dropdownButtonElement = $('#js-project-dropdown', test.dropdownContainerElement);
    initDeprecatedJQueryDropdown(test.dropdownButtonElement, options);
  }

  beforeEach(() => {
    loadHTMLFixture('static/deprecated_jquery_dropdown.html');
    test.dropdownContainerElement = $('.dropdown.inline');
    test.$dropdownMenuElement = $('.dropdown-menu', test.dropdownContainerElement);
    test.projectsData = JSON.parse(JSON.stringify(mockProjects));
  });

  afterEach(() => {
    $('body').off('keydown');
    test.dropdownContainerElement.off('keyup');

    resetHTMLFixture();
  });

  it('should open on click', () => {
    initDropDown.call(this, false);

    expect(test.dropdownContainerElement).not.toHaveClass('show');
    test.dropdownButtonElement.click();

    expect(test.dropdownContainerElement).toHaveClass('show');
  });

  it('escapes HTML as text', () => {
    test.projectsData[0].name_with_namespace = '<script>alert("testing");</script>';

    initDropDown.call(this, false);

    test.dropdownButtonElement.click();

    expect($('.dropdown-content li:first-child').text()).toBe('<script>alert("testing");</script>');
  });

  it('should output HTML when highlighting', () => {
    test.projectsData[0].name_with_namespace = 'testing';
    $('.dropdown-input .dropdown-input-field').val('test');

    initDropDown.call(this, false, true, {
      highlight: true,
    });

    test.dropdownButtonElement.click();

    expect($('.dropdown-content li:first-child').text()).toBe('testing');

    expect($('.dropdown-content li:first-child a').html()).toBe(
      '<b>t</b><b>e</b><b>s</b><b>t</b>ing',
    );
  });

  describe('that is open', () => {
    beforeEach(() => {
      initDropDown.call(this, false, false);
      test.dropdownButtonElement.click();
    });

    it('should select a following item on DOWN keypress', () => {
      expect($(FOCUSED_ITEM_SELECTOR, test.$dropdownMenuElement).length).toBe(0);
      const randomIndex = Math.floor(Math.random() * (test.projectsData.length - 1)) + 0;
      navigateWithKeys('down', randomIndex, () => {
        expect($(FOCUSED_ITEM_SELECTOR, test.$dropdownMenuElement).length).toBe(1);
        expect($(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, test.$dropdownMenuElement)).toHaveClass(
          'is-focused',
        );
      });
    });

    it('should select a previous item on UP keypress', () => {
      expect($(FOCUSED_ITEM_SELECTOR, test.$dropdownMenuElement).length).toBe(0);
      navigateWithKeys('down', test.projectsData.length - 1, () => {
        expect($(FOCUSED_ITEM_SELECTOR, test.$dropdownMenuElement).length).toBe(1);
        const randomIndex = Math.floor(Math.random() * (test.projectsData.length - 2)) + 0;
        navigateWithKeys('up', randomIndex, () => {
          expect($(FOCUSED_ITEM_SELECTOR, test.$dropdownMenuElement).length).toBe(1);
          expect(
            $(
              `${ITEM_SELECTOR}:eq(${test.projectsData.length - 2 - randomIndex}) a`,
              test.$dropdownMenuElement,
            ),
          ).toHaveClass('is-focused');
        });
      });
    });

    it('should click the selected item on ENTER keypress', () => {
      expect(test.dropdownContainerElement).toHaveClass('show');
      const randomIndex = Math.floor(Math.random() * (test.projectsData.length - 1)) + 0;
      navigateWithKeys('down', randomIndex, () => {
        navigateWithKeys('enter', null, () => {
          expect(test.dropdownContainerElement).not.toHaveClass('show');
          const link = $(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, test.$dropdownMenuElement);

          expect(link).toHaveClass('is-active');
          const linkedLocation = link.attr('href');
          if (linkedLocation && linkedLocation !== '#') {
            expect(visitUrl).toHaveBeenCalledWith(linkedLocation);
          }
        });
      });
    });

    it('should close on ESC keypress', () => {
      expect(test.dropdownContainerElement).toHaveClass('show');
      test.dropdownContainerElement.trigger({
        type: 'keyup',
        which: ARROW_KEYS.ESC,
        keyCode: ARROW_KEYS.ESC,
      });

      expect(test.dropdownContainerElement).not.toHaveClass('show');
    });
  });

  describe('opened and waiting for a remote callback', () => {
    beforeEach(() => {
      initDropDown.call(this, true, true);
      test.dropdownButtonElement.click();
    });

    it('should show loading indicator while search results are being fetched by backend', () => {
      const dropdownMenu = document.querySelector('.dropdown-menu');

      expect(dropdownMenu.className.indexOf('is-loading')).not.toBe(-1);
      remoteCallback();

      expect(dropdownMenu.className.indexOf('is-loading')).toBe(-1);
    });

    it('should not focus search input while remote task is not complete', () => {
      expect(document.activeElement).toBeDefined();
      expect(document.activeElement).not.toEqual(document.querySelector(SEARCH_INPUT_SELECTOR));
      remoteCallback();

      expect(document.activeElement).toEqual(document.querySelector(SEARCH_INPUT_SELECTOR));
    });

    it('should focus search input after remote task is complete', () => {
      remoteCallback();

      expect(document.activeElement).toBeDefined();
      expect(document.activeElement).toEqual(document.querySelector(SEARCH_INPUT_SELECTOR));
    });

    it('should focus on input when opening for the second time after transition', () => {
      remoteCallback();
      test.dropdownContainerElement.trigger({
        type: 'keyup',
        which: ARROW_KEYS.ESC,
        keyCode: ARROW_KEYS.ESC,
      });
      test.dropdownButtonElement.click();
      test.dropdownContainerElement.trigger('transitionend');

      expect(document.activeElement).toBeDefined();
      expect(document.activeElement).toEqual(document.querySelector(SEARCH_INPUT_SELECTOR));
    });
  });

  describe('input focus with array data', () => {
    it('should focus input when passing array data to drop down', () => {
      initDropDown.call(this, false, true);
      test.dropdownButtonElement.click();
      test.dropdownContainerElement.trigger('transitionend');

      expect(document.activeElement).toBeDefined();
      expect(document.activeElement).toEqual(document.querySelector(SEARCH_INPUT_SELECTOR));
    });
  });

  it('should still have input value on close and restore', () => {
    const $searchInput = $(SEARCH_INPUT_SELECTOR);
    initDropDown.call(this, false, true);
    $searchInput.trigger('focus').val('g').trigger('input');

    expect($searchInput.val()).toEqual('g');
    test.dropdownButtonElement.trigger('hidden.bs.dropdown');
    $searchInput.trigger('blur').trigger('focus');

    expect($searchInput.val()).toEqual('g');
  });

  describe('renderItem', () => {
    function dropdownWithOptions(options) {
      const $dropdownDiv = $('<div />');

      initDeprecatedJQueryDropdown($dropdownDiv, options);

      return $dropdownDiv.data('deprecatedJQueryDropdown');
    }

    function basicDropdown() {
      return dropdownWithOptions({});
    }

    describe('without selected value', () => {
      let dropdown;

      beforeEach(() => {
        dropdown = basicDropdown();
      });

      it('marks items without ID as active', () => {
        const dummyData = {};

        const html = dropdown.renderItem(dummyData, null, null);

        const link = html.querySelector('a');

        expect(link).toHaveClass('is-active');
      });

      it('does not mark items with ID as active', () => {
        const dummyData = {
          id: 'ea',
        };

        const html = dropdown.renderItem(dummyData, null, null);

        const link = html.querySelector('a');

        expect(link).not.toHaveClass('is-active');
      });
    });

    it('should return an empty .separator li when when appropriate', () => {
      const dropdown = basicDropdown();
      const sep = { type: 'separator' };
      const li = dropdown.renderItem(sep);

      expect(li).toHaveClass('separator');
      expect(li.childNodes.length).toEqual(0);
    });

    it('should return an empty .divider li when when appropriate', () => {
      const dropdown = basicDropdown();
      const div = { type: 'divider' };
      const li = dropdown.renderItem(div);

      expect(li).toHaveClass('divider');
      expect(li.childNodes.length).toEqual(0);
    });

    it('should return a .dropdown-header li with the correct content when when appropriate', () => {
      const dropdown = basicDropdown();
      const text = 'My Header';
      const header = { type: 'header', content: text };
      const li = dropdown.renderItem(header);

      expect(li).toHaveClass('dropdown-header');
      expect(li.childNodes.length).toEqual(1);
      expect(li.textContent).toEqual(text);
    });

    describe('with a trackSuggestionsClickedLabel', () => {
      it('includes data-track attributes', () => {
        const dropdown = dropdownWithOptions({
          trackSuggestionClickedLabel: 'some_value_for_label',
        });
        const item = {
          id: 'some-element-id',
          text: 'the link text',
          url: 'http://example.com',
          category: 'Suggestion category',
        };
        const li = dropdown.renderItem(item, null, 3);
        const link = li.querySelector('a');

        expect(link).toHaveAttr('data-track-action', 'click_text');
        expect(link).toHaveAttr('data-track-label', 'some_value_for_label');
        expect(link).toHaveAttr('data-track-value', '3');
        expect(link).toHaveAttr('data-track-property', 'suggestion-category');
      });

      it('defaults property to no_category when category not provided', () => {
        const dropdown = dropdownWithOptions({
          trackSuggestionClickedLabel: 'some_value_for_label',
        });
        const item = {
          id: 'some-element-id',
          text: 'the link text',
          url: 'http://example.com',
        };
        const li = dropdown.renderItem(item);
        const link = li.querySelector('a');

        expect(link).toHaveAttr('data-track-property', 'no-category');
      });
    });
  });

  it('should keep selected item after selecting a second time', () => {
    const options = {
      isSelectable(item, $el) {
        return !$el.hasClass('is-active');
      },
      toggleLabel(item) {
        return item && item.id;
      },
    };
    initDropDown.call(this, false, false, options);
    const $item = $(`${ITEM_SELECTOR}:first() a`, test.$dropdownMenuElement);

    // select item the first time
    test.dropdownButtonElement.click();
    $item.click();

    expect($item).toHaveClass('is-active');
    // select item the second time
    test.dropdownButtonElement.click();
    $item.click();

    expect($item).toHaveClass('is-active');

    expect($('.dropdown-toggle-text')).toHaveText(test.projectsData[0].id.toString());
  });
});