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

redirect_behavior_spec.js « listbox « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b2a40b65ceed952ba13ccfe4773991952f8998f (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
import { initListbox } from '~/listbox';
import { initRedirectListboxBehavior } from '~/listbox/redirect_behavior';
import { redirectTo } from '~/lib/utils/url_utility';
import { getFixture, setHTMLFixture } from 'helpers/fixtures';

jest.mock('~/lib/utils/url_utility');
jest.mock('~/listbox', () => ({
  initListbox: jest.fn().mockReturnValue({ foo: true }),
}));

const fixture = getFixture('listbox/redirect_listbox.html');

describe('initRedirectListboxBehavior', () => {
  let instances;

  beforeEach(() => {
    setHTMLFixture(`
      ${fixture}
      ${fixture}
    `);

    instances = initRedirectListboxBehavior();
  });

  it('calls initListbox for each .js-redirect-listbox', () => {
    expect(instances).toEqual([{ foo: true }, { foo: true }]);

    expect(initListbox).toHaveBeenCalledTimes(2);

    initListbox.mock.calls.forEach((callArgs, i) => {
      const elements = document.querySelectorAll('.js-redirect-listbox');

      expect(callArgs[0]).toBe(elements[i]);
      expect(callArgs[1]).toEqual({
        onChange: expect.any(Function),
      });
    });
  });

  it('passes onChange handler to initListbox that calls redirectTo', () => {
    const [firstCallArgs] = initListbox.mock.calls;
    const { onChange } = firstCallArgs[1];
    const mockItem = { href: '/foo' };

    expect(redirectTo).not.toHaveBeenCalled();

    onChange(mockItem);

    expect(redirectTo).toHaveBeenCalledWith(mockItem.href);
  });
});