BigW Consortium Gitlab

search_autocomplete_spec.js 6.63 KB
Newer Older
1
/* eslint-disable space-before-function-paren, max-len, no-var, one-var, one-var-declaration-per-line, no-unused-expressions, consistent-return, no-param-reassign, default-case, no-return-assign, comma-dangle, object-shorthand, prefer-template, quotes, new-parens, vars-on-top, new-cap, max-len */
2

3 4 5 6
import '~/gl_dropdown';
import '~/search_autocomplete';
import '~/lib/utils/common_utils';
import 'vendor/fuzzaldrin-plus';
Fatih Acet committed
7 8 9

(function() {
  var addBodyAttributes, assertLinks, dashboardIssuesPath, dashboardMRsPath, groupIssuesPath, groupMRsPath, groupName, mockDashboardOptions, mockGroupOptions, mockProjectOptions, projectIssuesPath, projectMRsPath, projectName, userId, widget;
10
  var userName = 'root';
Fatih Acet committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  widget = null;

  userId = 1;

  dashboardIssuesPath = '/dashboard/issues';

  dashboardMRsPath = '/dashboard/merge_requests';

  projectIssuesPath = '/gitlab-org/gitlab-ce/issues';

  projectMRsPath = '/gitlab-org/gitlab-ce/merge_requests';

  groupIssuesPath = '/groups/gitlab-org/issues';

  groupMRsPath = '/groups/gitlab-org/merge_requests';

  projectName = 'GitLab Community Edition';

  groupName = 'Gitlab Org';

32 33
  // Add required attributes to body before starting the test.
  // section would be dashboard|group|project
Fatih Acet committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  addBodyAttributes = function(section) {
    var $body;
    if (section == null) {
      section = 'dashboard';
    }
    $body = $('body');
    $body.removeAttr('data-page');
    $body.removeAttr('data-project');
    $body.removeAttr('data-group');
    switch (section) {
      case 'dashboard':
        return $body.data('page', 'root:index');
      case 'group':
        $body.data('page', 'groups:show');
        return $body.data('group', 'gitlab-org');
      case 'project':
        $body.data('page', 'projects:show');
        return $body.data('project', 'gitlab-ce');
    }
  };

55
  // Mock `gl` object in window for dashboard specific page. App code will need it.
Fatih Acet committed
56 57 58 59 60 61 62 63
  mockDashboardOptions = function() {
    window.gl || (window.gl = {});
    return window.gl.dashboardOptions = {
      issuesPath: dashboardIssuesPath,
      mrPath: dashboardMRsPath
    };
  };

64
  // Mock `gl` object in window for project specific page. App code will need it.
Fatih Acet committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  mockProjectOptions = function() {
    window.gl || (window.gl = {});
    return window.gl.projectOptions = {
      'gitlab-ce': {
        issuesPath: projectIssuesPath,
        mrPath: projectMRsPath,
        projectName: projectName
      }
    };
  };

  mockGroupOptions = function() {
    window.gl || (window.gl = {});
    return window.gl.groupOptions = {
      'gitlab-org': {
        issuesPath: groupIssuesPath,
        mrPath: groupMRsPath,
        projectName: groupName
      }
    };
  };

  assertLinks = function(list, issuesPath, mrsPath) {
    var a1, a2, a3, a4, issuesAssignedToMeLink, issuesIHaveCreatedLink, mrsAssignedToMeLink, mrsIHaveCreatedLink;
89 90
    issuesAssignedToMeLink = issuesPath + "/?assignee_username=" + userName;
    issuesIHaveCreatedLink = issuesPath + "/?author_username=" + userName;
91 92
    mrsAssignedToMeLink = mrsPath + "/?assignee_username=" + userName;
    mrsIHaveCreatedLink = mrsPath + "/?author_username=" + userName;
Fatih Acet committed
93 94 95 96 97
    a1 = "a[href='" + issuesAssignedToMeLink + "']";
    a2 = "a[href='" + issuesIHaveCreatedLink + "']";
    a3 = "a[href='" + mrsAssignedToMeLink + "']";
    a4 = "a[href='" + mrsIHaveCreatedLink + "']";
    expect(list.find(a1).length).toBe(1);
Luke Bennett committed
98
    expect(list.find(a1).text()).toBe('Issues assigned to me');
Fatih Acet committed
99
    expect(list.find(a2).length).toBe(1);
Luke Bennett committed
100
    expect(list.find(a2).text()).toBe("Issues I've created");
Fatih Acet committed
101
    expect(list.find(a3).length).toBe(1);
Luke Bennett committed
102
    expect(list.find(a3).text()).toBe('Merge requests assigned to me');
Fatih Acet committed
103
    expect(list.find(a4).length).toBe(1);
Luke Bennett committed
104
    return expect(list.find(a4).text()).toBe("Merge requests I've created");
Fatih Acet committed
105 106 107
  };

  describe('Search autocomplete dropdown', function() {
108
    preloadFixtures('static/search_autocomplete.html.raw');
Fatih Acet committed
109
    beforeEach(function() {
110
      loadFixtures('static/search_autocomplete.html.raw');
111 112
      widget = new gl.SearchAutocomplete;
      // Prevent turbolinks from triggering within gl_dropdown
113
      spyOn(window.gl.utils, 'visitUrl').and.returnValue(true);
114 115 116 117 118 119 120 121 122 123

      window.gon = {};
      window.gon.current_user_id = userId;
      window.gon.current_username = userName;

      return widget = new gl.SearchAutocomplete;
    });

    afterEach(function() {
      window.gon = {};
Fatih Acet committed
124 125 126 127 128
    });
    it('should show Dashboard specific dropdown menu', function() {
      var list;
      addBodyAttributes();
      mockDashboardOptions();
129
      widget.searchInput.triggerHandler('focus');
Fatih Acet committed
130 131 132 133 134 135 136
      list = widget.wrap.find('.dropdown-menu').find('ul');
      return assertLinks(list, dashboardIssuesPath, dashboardMRsPath);
    });
    it('should show Group specific dropdown menu', function() {
      var list;
      addBodyAttributes('group');
      mockGroupOptions();
137
      widget.searchInput.triggerHandler('focus');
Fatih Acet committed
138 139 140 141 142 143 144
      list = widget.wrap.find('.dropdown-menu').find('ul');
      return assertLinks(list, groupIssuesPath, groupMRsPath);
    });
    it('should show Project specific dropdown menu', function() {
      var list;
      addBodyAttributes('project');
      mockProjectOptions();
145
      widget.searchInput.triggerHandler('focus');
Fatih Acet committed
146 147 148
      list = widget.wrap.find('.dropdown-menu').find('ul');
      return assertLinks(list, projectIssuesPath, projectMRsPath);
    });
149
    it('should not show category related menu if there is text in the input', function() {
Fatih Acet committed
150 151 152 153
      var link, list;
      addBodyAttributes('project');
      mockProjectOptions();
      widget.searchInput.val('help');
154
      widget.searchInput.triggerHandler('focus');
Fatih Acet committed
155 156 157 158
      list = widget.wrap.find('.dropdown-menu').find('ul');
      link = "a[href='" + projectIssuesPath + "/?assignee_id=" + userId + "']";
      return expect(list.find(link).length).toBe(0);
    });
159 160 161 162 163 164
    return it('should not submit the search form when selecting an autocomplete row with the keyboard', function() {
      var ENTER = 13;
      var DOWN = 40;
      addBodyAttributes();
      mockDashboardOptions(true);
      var submitSpy = spyOnEvent('form', 'submit');
165
      widget.searchInput.triggerHandler('focus');
166 167 168 169 170 171 172 173 174 175
      widget.wrap.trigger($.Event('keydown', { which: DOWN }));
      var enterKeyEvent = $.Event('keydown', { which: ENTER });
      widget.searchInput.trigger(enterKeyEvent);
      // This does not currently catch failing behavior. For security reasons,
      // browsers will not trigger default behavior (form submit, in this
      // example) on JavaScript-created keypresses.
      expect(submitSpy).not.toHaveBeenTriggered();
      // Does a worse job at capturing the intent of the test, but works.
      expect(enterKeyEvent.isDefaultPrevented()).toBe(true);
    });
Fatih Acet committed
176
  });
177
}).call(window);