summaryrefslogtreecommitdiff
path: root/spec/javascripts/blob/target_branch_dropdown_spec.js
blob: 4fb79663c516516086f2aaf50b35925fe30f3114 (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
require('~/gl_dropdown');
require('~/lib/utils/type_utility');
require('~/blob/create_branch_dropdown');
require('~/blob/target_branch_dropdown');

describe('TargetBranchDropdown', () => {
  const fixtureTemplate = 'static/target_branch_dropdown.html.raw';
  let dropdown;

  function createDropdown() {
    const projectBranches = getJSONFixture('project_branches.json');
    const dropdownEl = document.querySelector('.js-project-branches-dropdown');
    dropdown = new gl.TargetBranchDropDown(dropdownEl);
    dropdown.cachedRefs = projectBranches;
    dropdown.refreshData();
    return dropdown;
  }

  function submitBtn() {
    return document.querySelector('button[type="submit"]');
  }

  function searchField() {
    return document.querySelector('.dropdown-page-one .dropdown-input-field');
  }

  function element() {
    return document.querySelectorAll('div.dropdown-content li a');
  }

  function elementAtIndex(index) {
    return element()[index];
  }

  function clickElementAtIndex(index) {
    elementAtIndex(index).click();
  }

  preloadFixtures(fixtureTemplate);

  beforeEach(() => {
    loadFixtures(fixtureTemplate);
    createDropdown();
  });

  it('disable submit when branch is not selected', () => {
    document.querySelector('input[name="target_branch"]').value = null;
    clickElementAtIndex(1);

    expect(submitBtn().getAttribute('disabled')).toEqual('');
  });

  it('enable submit when a branch is selected', () => {
    clickElementAtIndex(1);

    expect(submitBtn().getAttribute('disabled')).toBe(null);
  });

  it('triggers change.branch event on a branch click', () => {
    spyOnEvent(dropdown.$dropdown, 'change.branch');
    clickElementAtIndex(0);

    expect('change.branch').toHaveBeenTriggeredOn(dropdown.$dropdown);
  });

  describe('#dropdownData', () => {
    it('cache the refs', () => {
      const refs = dropdown.cachedRefs;
      dropdown.cachedRefs = null;

      dropdown.dropdownData(refs);

      expect(dropdown.cachedRefs).toEqual(refs);
    });

    it('returns the Branches with the newBranch and defaultBranch', () => {
      const refs = dropdown.cachedRefs;
      dropdown.branchInput.value = 'master';
      dropdown.newBranch = { id: 'new_branch', text: 'new_branch', title: 'new_branch' };

      const branches = dropdown.dropdownData(refs).Branches;

      expect(branches.length).toEqual(4);
      expect(branches[0]).toEqual(dropdown.newBranch);
      expect(branches[1]).toEqual({ id: 'master', text: 'master', title: 'master' });
      expect(branches[2]).toEqual({ id: 'development', text: 'development', title: 'development' });
      expect(branches[3]).toEqual({ id: 'staging', text: 'staging', title: 'staging' });
    });
  });

  describe('#setNewBranch', () => {
    it('adds the new branch and select it', () => {
      const branchName = 'new_branch';

      dropdown.setNewBranch(branchName);

      expect(elementAtIndex(0)).toHaveClass('is-active');
      expect(elementAtIndex(0)).toContainHtml(branchName);
    });

    it("doesn't add a new branch if already exists in the list", () => {
      const branchName = elementAtIndex(0).text;
      const initialLength = element().length;

      dropdown.setNewBranch(branchName);

      expect(element().length).toEqual(initialLength);
    });

    it('clears the search filter', () => {
      const branchName = elementAtIndex(0).text;
      searchField().value = 'searching';

      dropdown.setNewBranch(branchName);

      expect(searchField().value).toEqual('');
    });
  });
});