2017-01-10 18:02:20 -05:00
|
|
|
/* 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 */
|
2017-01-06 13:46:56 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-16 17:01:51 -04:00
|
|
|
import '~/gl_dropdown';
|
2017-12-13 04:26:44 -05:00
|
|
|
import SearchAutocomplete from '~/search_autocomplete';
|
2017-05-16 17:01:51 -04:00
|
|
|
import '~/lib/utils/common_utils';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-04-05 04:19:55 -04:00
|
|
|
describe('Search autocomplete dropdown', () => {
|
|
|
|
var assertLinks,
|
|
|
|
dashboardIssuesPath,
|
|
|
|
dashboardMRsPath,
|
|
|
|
groupIssuesPath,
|
|
|
|
groupMRsPath,
|
|
|
|
groupName,
|
|
|
|
mockDashboardOptions,
|
|
|
|
mockGroupOptions,
|
|
|
|
mockProjectOptions,
|
|
|
|
projectIssuesPath,
|
|
|
|
projectMRsPath,
|
|
|
|
projectName,
|
|
|
|
userId,
|
|
|
|
widget;
|
2016-12-15 09:34:34 -05:00
|
|
|
var userName = 'root';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
2017-10-06 16:36:15 -04:00
|
|
|
const removeBodyAttributes = function() {
|
|
|
|
const $body = $('body');
|
|
|
|
|
|
|
|
$body.removeAttr('data-page');
|
|
|
|
$body.removeAttr('data-project');
|
|
|
|
$body.removeAttr('data-group');
|
|
|
|
};
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Add required attributes to body before starting the test.
|
|
|
|
// section would be dashboard|group|project
|
2017-10-06 16:36:15 -04:00
|
|
|
const addBodyAttributes = function(section) {
|
2016-07-24 16:45:11 -04:00
|
|
|
if (section == null) {
|
|
|
|
section = 'dashboard';
|
|
|
|
}
|
2017-10-06 16:36:15 -04:00
|
|
|
|
|
|
|
const $body = $('body');
|
|
|
|
removeBodyAttributes();
|
2016-07-24 16:45:11 -04:00
|
|
|
switch (section) {
|
|
|
|
case 'dashboard':
|
2017-10-06 16:36:15 -04:00
|
|
|
return $body.attr('data-page', 'root:index');
|
2016-07-24 16:45:11 -04:00
|
|
|
case 'group':
|
2017-10-06 16:36:15 -04:00
|
|
|
$body.attr('data-page', 'groups:show');
|
2016-07-24 16:45:11 -04:00
|
|
|
return $body.data('group', 'gitlab-org');
|
|
|
|
case 'project':
|
2017-10-06 16:36:15 -04:00
|
|
|
$body.attr('data-page', 'projects:show');
|
2016-07-24 16:45:11 -04:00
|
|
|
return $body.data('project', 'gitlab-ce');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-24 13:49:01 -04:00
|
|
|
const disableProjectIssues = function() {
|
|
|
|
document.querySelector('.js-search-project-options').setAttribute('data-issues-disabled', true);
|
|
|
|
};
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Mock `gl` object in window for dashboard specific page. App code will need it.
|
2016-07-24 16:45:11 -04:00
|
|
|
mockDashboardOptions = function() {
|
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 04:19:55 -04:00
|
|
|
return (window.gl.dashboardOptions = {
|
2016-07-24 16:45:11 -04:00
|
|
|
issuesPath: dashboardIssuesPath,
|
2018-04-05 04:19:55 -04:00
|
|
|
mrPath: dashboardMRsPath,
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Mock `gl` object in window for project specific page. App code will need it.
|
2016-07-24 16:45:11 -04:00
|
|
|
mockProjectOptions = function() {
|
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 04:19:55 -04:00
|
|
|
return (window.gl.projectOptions = {
|
2016-07-24 16:45:11 -04:00
|
|
|
'gitlab-ce': {
|
|
|
|
issuesPath: projectIssuesPath,
|
|
|
|
mrPath: projectMRsPath,
|
2018-04-05 04:19:55 -04:00
|
|
|
projectName: projectName,
|
|
|
|
},
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mockGroupOptions = function() {
|
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 04:19:55 -04:00
|
|
|
return (window.gl.groupOptions = {
|
2016-07-24 16:45:11 -04:00
|
|
|
'gitlab-org': {
|
|
|
|
issuesPath: groupIssuesPath,
|
|
|
|
mrPath: groupMRsPath,
|
2018-04-05 04:19:55 -04:00
|
|
|
projectName: groupName,
|
|
|
|
},
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
assertLinks = function(list, issuesPath, mrsPath) {
|
2017-10-24 13:49:01 -04:00
|
|
|
if (issuesPath) {
|
2018-04-05 04:19:55 -04:00
|
|
|
const issuesAssignedToMeLink = `a[href="${issuesPath}/?assignee_id=${userId}"]`;
|
|
|
|
const issuesIHaveCreatedLink = `a[href="${issuesPath}/?author_id=${userId}"]`;
|
|
|
|
expect(list.find(issuesAssignedToMeLink).length).toBe(1);
|
|
|
|
expect(list.find(issuesAssignedToMeLink).text()).toBe('Issues assigned to me');
|
|
|
|
expect(list.find(issuesIHaveCreatedLink).length).toBe(1);
|
|
|
|
expect(list.find(issuesIHaveCreatedLink).text()).toBe("Issues I've created");
|
2017-10-24 13:49:01 -04:00
|
|
|
}
|
2018-04-05 04:19:55 -04:00
|
|
|
const mrsAssignedToMeLink = `a[href="${mrsPath}/?assignee_id=${userId}"]`;
|
|
|
|
const mrsIHaveCreatedLink = `a[href="${mrsPath}/?author_id=${userId}"]`;
|
|
|
|
expect(list.find(mrsAssignedToMeLink).length).toBe(1);
|
|
|
|
expect(list.find(mrsAssignedToMeLink).text()).toBe('Merge requests assigned to me');
|
|
|
|
expect(list.find(mrsIHaveCreatedLink).length).toBe(1);
|
|
|
|
expect(list.find(mrsIHaveCreatedLink).text()).toBe("Merge requests I've created");
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
2018-04-05 04:19:55 -04:00
|
|
|
preloadFixtures('static/search_autocomplete.html.raw');
|
|
|
|
beforeEach(function() {
|
|
|
|
loadFixtures('static/search_autocomplete.html.raw');
|
2017-10-06 16:36:15 -04:00
|
|
|
|
2018-04-05 04:19:55 -04:00
|
|
|
window.gon = {};
|
|
|
|
window.gon.current_user_id = userId;
|
|
|
|
window.gon.current_username = userName;
|
2017-01-30 16:02:19 -05:00
|
|
|
|
2018-04-05 04:19:55 -04:00
|
|
|
return (widget = new SearchAutocomplete());
|
|
|
|
});
|
2017-01-30 16:02:19 -05:00
|
|
|
|
2018-04-05 04:19:55 -04:00
|
|
|
afterEach(function() {
|
|
|
|
// Undo what we did to the shared <body>
|
|
|
|
removeBodyAttributes();
|
|
|
|
window.gon = {};
|
|
|
|
});
|
|
|
|
it('should show Dashboard specific dropdown menu', function() {
|
|
|
|
var list;
|
|
|
|
addBodyAttributes();
|
|
|
|
mockDashboardOptions();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
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();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
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();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
list = widget.wrap.find('.dropdown-menu').find('ul');
|
|
|
|
return assertLinks(list, projectIssuesPath, projectMRsPath);
|
|
|
|
});
|
|
|
|
it('should show only Project mergeRequest dropdown menu items when project issues are disabled', function() {
|
|
|
|
addBodyAttributes('project');
|
|
|
|
disableProjectIssues();
|
|
|
|
mockProjectOptions();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
const list = widget.wrap.find('.dropdown-menu').find('ul');
|
|
|
|
assertLinks(list, null, projectMRsPath);
|
|
|
|
});
|
|
|
|
it('should not show category related menu if there is text in the input', function() {
|
|
|
|
var link, list;
|
|
|
|
addBodyAttributes('project');
|
|
|
|
mockProjectOptions();
|
|
|
|
widget.searchInput.val('help');
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
list = widget.wrap.find('.dropdown-menu').find('ul');
|
|
|
|
link = "a[href='" + projectIssuesPath + '/?assignee_id=' + userId + "']";
|
|
|
|
return expect(list.find(link).length).toBe(0);
|
|
|
|
});
|
|
|
|
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');
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
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();
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-04-05 04:19:55 -04:00
|
|
|
});
|