summaryrefslogtreecommitdiff
path: root/spec/frontend/namespace_select_spec.js
blob: d6f3eb75cd9b8d3a030461ca92f4f3cdfe330fae (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
import NamespaceSelect from '~/namespace_select';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';

jest.mock('~/deprecated_jquery_dropdown');

describe('NamespaceSelect', () => {
  it('initializes deprecatedJQueryDropdown', () => {
    const dropdown = document.createElement('div');

    // eslint-disable-next-line no-new
    new NamespaceSelect({ dropdown });

    expect(initDeprecatedJQueryDropdown).toHaveBeenCalled();
  });

  describe('as input', () => {
    let deprecatedJQueryDropdownOptions;

    beforeEach(() => {
      const dropdown = document.createElement('div');
      // eslint-disable-next-line no-new
      new NamespaceSelect({ dropdown });
      [[, deprecatedJQueryDropdownOptions]] = initDeprecatedJQueryDropdown.mock.calls;
    });

    it('prevents click events', () => {
      const dummyEvent = new Event('dummy');
      jest.spyOn(dummyEvent, 'preventDefault').mockImplementation(() => {});

      // expect(foo).toContain('test');
      deprecatedJQueryDropdownOptions.clicked({ e: dummyEvent });

      expect(dummyEvent.preventDefault).toHaveBeenCalled();
    });
  });

  describe('as filter', () => {
    let deprecatedJQueryDropdownOptions;

    beforeEach(() => {
      const dropdown = document.createElement('div');
      dropdown.dataset.isFilter = 'true';
      // eslint-disable-next-line no-new
      new NamespaceSelect({ dropdown });
      [[, deprecatedJQueryDropdownOptions]] = initDeprecatedJQueryDropdown.mock.calls;
    });

    it('does not prevent click events', () => {
      const dummyEvent = new Event('dummy');
      jest.spyOn(dummyEvent, 'preventDefault').mockImplementation(() => {});

      deprecatedJQueryDropdownOptions.clicked({ e: dummyEvent });

      expect(dummyEvent.preventDefault).not.toHaveBeenCalled();
    });

    it('sets URL of dropdown items', () => {
      const dummyNamespace = { id: 'eal' };

      const itemUrl = deprecatedJQueryDropdownOptions.url(dummyNamespace);

      expect(itemUrl).toContain(`namespace_id=${dummyNamespace.id}`);
    });
  });
});