summaryrefslogtreecommitdiff
path: root/spec/javascripts/namespace_select_spec.js
blob: 3b2641f7646e8009e9cf4b829b302158c1d556c1 (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
import $ from 'jquery';
import NamespaceSelect from '~/namespace_select';

describe('NamespaceSelect', () => {
  beforeEach(() => {
    spyOn($.fn, 'glDropdown');
  });

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

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

    expect($.fn.glDropdown).toHaveBeenCalled();
  });

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

    beforeEach(() => {
      const dropdown = document.createElement('div');
      // eslint-disable-next-line no-new
      new NamespaceSelect({ dropdown });
      glDropdownOptions = $.fn.glDropdown.calls.argsFor(0)[0];
    });

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

      glDropdownOptions.clicked({ e: dummyEvent });

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

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

    beforeEach(() => {
      const dropdown = document.createElement('div');
      dropdown.dataset.isFilter = 'true';
      // eslint-disable-next-line no-new
      new NamespaceSelect({ dropdown });
      glDropdownOptions = $.fn.glDropdown.calls.argsFor(0)[0];
    });

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

      glDropdownOptions.clicked({ e: dummyEvent });

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

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

      const itemUrl = glDropdownOptions.url(dummyNamespace);

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