2019-11-18 09:06:43 +00:00
|
|
|
/* eslint-disable no-unused-expressions, consistent-return, no-param-reassign, default-case, no-return-assign */
|
2017-01-06 18:46:56 +00:00
|
|
|
|
2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2017-05-16 21:01:51 +00:00
|
|
|
import '~/gl_dropdown';
|
2018-09-13 21:59:09 +00:00
|
|
|
import initSearchAutocomplete from '~/search_autocomplete';
|
2017-05-16 21:01:51 +00:00
|
|
|
import '~/lib/utils/common_utils';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
describe('Search autocomplete dropdown', () => {
|
2019-11-18 09:06:43 +00:00
|
|
|
let widget = null;
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const userName = 'root';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const userId = 1;
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const dashboardIssuesPath = '/dashboard/issues';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const dashboardMRsPath = '/dashboard/merge_requests';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const projectIssuesPath = '/gitlab-org/gitlab-foss/issues';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-01-21 00:08:46 +00:00
|
|
|
const projectMRsPath = '/gitlab-org/gitlab-foss/-/merge_requests';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-01-21 00:08:46 +00:00
|
|
|
const groupIssuesPath = '/groups/gitlab-org/-/issues';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2020-01-21 00:08:46 +00:00
|
|
|
const groupMRsPath = '/groups/gitlab-org/-/merge_requests';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const projectName = 'GitLab Community Edition';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const groupName = 'Gitlab Org';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-10-06 20:36:15 +00:00
|
|
|
const removeBodyAttributes = function() {
|
|
|
|
const $body = $('body');
|
|
|
|
|
|
|
|
$body.removeAttr('data-page');
|
|
|
|
$body.removeAttr('data-project');
|
|
|
|
$body.removeAttr('data-group');
|
|
|
|
};
|
|
|
|
|
2016-07-27 03:32:10 +00:00
|
|
|
// Add required attributes to body before starting the test.
|
|
|
|
// section would be dashboard|group|project
|
2017-10-06 20:36:15 +00:00
|
|
|
const addBodyAttributes = function(section) {
|
2016-07-24 20:45:11 +00:00
|
|
|
if (section == null) {
|
|
|
|
section = 'dashboard';
|
|
|
|
}
|
2017-10-06 20:36:15 +00:00
|
|
|
|
|
|
|
const $body = $('body');
|
|
|
|
removeBodyAttributes();
|
2016-07-24 20:45:11 +00:00
|
|
|
switch (section) {
|
|
|
|
case 'dashboard':
|
2017-10-06 20:36:15 +00:00
|
|
|
return $body.attr('data-page', 'root:index');
|
2016-07-24 20:45:11 +00:00
|
|
|
case 'group':
|
2017-10-06 20:36:15 +00:00
|
|
|
$body.attr('data-page', 'groups:show');
|
2016-07-24 20:45:11 +00:00
|
|
|
return $body.data('group', 'gitlab-org');
|
|
|
|
case 'project':
|
2017-10-06 20:36:15 +00:00
|
|
|
$body.attr('data-page', 'projects:show');
|
2016-07-24 20:45:11 +00:00
|
|
|
return $body.data('project', 'gitlab-ce');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-24 17:49:01 +00:00
|
|
|
const disableProjectIssues = function() {
|
|
|
|
document.querySelector('.js-search-project-options').setAttribute('data-issues-disabled', true);
|
|
|
|
};
|
|
|
|
|
2016-07-27 03:32:10 +00:00
|
|
|
// Mock `gl` object in window for dashboard specific page. App code will need it.
|
2019-11-18 09:06:43 +00:00
|
|
|
const mockDashboardOptions = function() {
|
2016-07-24 20:45:11 +00:00
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 08:19:55 +00:00
|
|
|
return (window.gl.dashboardOptions = {
|
2016-07-24 20:45:11 +00:00
|
|
|
issuesPath: dashboardIssuesPath,
|
2018-04-05 08:19:55 +00:00
|
|
|
mrPath: dashboardMRsPath,
|
|
|
|
});
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2016-07-27 03:32:10 +00:00
|
|
|
// Mock `gl` object in window for project specific page. App code will need it.
|
2019-11-18 09:06:43 +00:00
|
|
|
const mockProjectOptions = function() {
|
2016-07-24 20:45:11 +00:00
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 08:19:55 +00:00
|
|
|
return (window.gl.projectOptions = {
|
2016-07-24 20:45:11 +00:00
|
|
|
'gitlab-ce': {
|
|
|
|
issuesPath: projectIssuesPath,
|
|
|
|
mrPath: projectMRsPath,
|
2019-09-18 14:02:45 +00:00
|
|
|
projectName,
|
2018-04-05 08:19:55 +00:00
|
|
|
},
|
|
|
|
});
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const mockGroupOptions = function() {
|
2016-07-24 20:45:11 +00:00
|
|
|
window.gl || (window.gl = {});
|
2018-04-05 08:19:55 +00:00
|
|
|
return (window.gl.groupOptions = {
|
2016-07-24 20:45:11 +00:00
|
|
|
'gitlab-org': {
|
|
|
|
issuesPath: groupIssuesPath,
|
|
|
|
mrPath: groupMRsPath,
|
2018-04-05 08:19:55 +00:00
|
|
|
projectName: groupName,
|
|
|
|
},
|
|
|
|
});
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2019-11-18 09:06:43 +00:00
|
|
|
const assertLinks = function(list, issuesPath, mrsPath) {
|
2017-10-24 17:49:01 +00:00
|
|
|
if (issuesPath) {
|
2018-10-29 03:03:43 +00:00
|
|
|
const issuesAssignedToMeLink = `a[href="${issuesPath}/?assignee_username=${userName}"]`;
|
|
|
|
const issuesIHaveCreatedLink = `a[href="${issuesPath}/?author_username=${userName}"]`;
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
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 17:49:01 +00:00
|
|
|
}
|
2018-10-29 03:03:43 +00:00
|
|
|
const mrsAssignedToMeLink = `a[href="${mrsPath}/?assignee_username=${userName}"]`;
|
|
|
|
const mrsIHaveCreatedLink = `a[href="${mrsPath}/?author_username=${userName}"]`;
|
2018-10-09 18:03:09 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
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 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 16:03:28 +00:00
|
|
|
preloadFixtures('static/search_autocomplete.html');
|
2018-04-05 08:19:55 +00:00
|
|
|
beforeEach(function() {
|
2019-03-26 16:03:28 +00:00
|
|
|
loadFixtures('static/search_autocomplete.html');
|
2017-10-06 20:36:15 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
window.gon = {};
|
|
|
|
window.gon.current_user_id = userId;
|
|
|
|
window.gon.current_username = userName;
|
2017-01-30 21:02:19 +00:00
|
|
|
|
2018-09-13 21:59:09 +00:00
|
|
|
return (widget = initSearchAutocomplete());
|
2018-04-05 08:19:55 +00:00
|
|
|
});
|
2017-01-30 21:02:19 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
afterEach(function() {
|
|
|
|
// Undo what we did to the shared <body>
|
|
|
|
removeBodyAttributes();
|
|
|
|
window.gon = {};
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
it('should show Dashboard specific dropdown menu', function() {
|
|
|
|
addBodyAttributes();
|
|
|
|
mockDashboardOptions();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
2019-11-18 09:06:43 +00:00
|
|
|
const list = widget.wrap.find('.dropdown-menu').find('ul');
|
2018-04-05 08:19:55 +00:00
|
|
|
return assertLinks(list, dashboardIssuesPath, dashboardMRsPath);
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
it('should show Group specific dropdown menu', function() {
|
|
|
|
addBodyAttributes('group');
|
|
|
|
mockGroupOptions();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
2019-11-18 09:06:43 +00:00
|
|
|
const list = widget.wrap.find('.dropdown-menu').find('ul');
|
2018-04-05 08:19:55 +00:00
|
|
|
return assertLinks(list, groupIssuesPath, groupMRsPath);
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
it('should show Project specific dropdown menu', function() {
|
|
|
|
addBodyAttributes('project');
|
|
|
|
mockProjectOptions();
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
2019-11-18 09:06:43 +00:00
|
|
|
const list = widget.wrap.find('.dropdown-menu').find('ul');
|
2018-04-05 08:19:55 +00:00
|
|
|
return assertLinks(list, projectIssuesPath, projectMRsPath);
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
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);
|
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
it('should not show category related menu if there is text in the input', function() {
|
|
|
|
addBodyAttributes('project');
|
|
|
|
mockProjectOptions();
|
|
|
|
widget.searchInput.val('help');
|
|
|
|
widget.searchInput.triggerHandler('focus');
|
2019-11-18 09:06:43 +00:00
|
|
|
const list = widget.wrap.find('.dropdown-menu').find('ul');
|
|
|
|
const link = `a[href='${projectIssuesPath}/?assignee_username=${userName}']`;
|
2018-10-09 18:03:09 +00:00
|
|
|
|
|
|
|
expect(list.find(link).length).toBe(0);
|
2018-04-05 08:19:55 +00:00
|
|
|
});
|
2018-10-09 17:01:49 +00:00
|
|
|
|
2018-04-05 08:19:55 +00:00
|
|
|
it('should not submit the search form when selecting an autocomplete row with the keyboard', function() {
|
2019-11-18 09:06:43 +00:00
|
|
|
const ENTER = 13;
|
|
|
|
const DOWN = 40;
|
2018-04-05 08:19:55 +00:00
|
|
|
addBodyAttributes();
|
|
|
|
mockDashboardOptions(true);
|
2019-11-18 09:06:43 +00:00
|
|
|
const submitSpy = spyOnEvent('form', 'submit');
|
2018-04-05 08:19:55 +00:00
|
|
|
widget.searchInput.triggerHandler('focus');
|
|
|
|
widget.wrap.trigger($.Event('keydown', { which: DOWN }));
|
2019-11-18 09:06:43 +00:00
|
|
|
const enterKeyEvent = $.Event('keydown', { which: ENTER });
|
2018-04-05 08:19:55 +00:00
|
|
|
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 20:45:11 +00:00
|
|
|
});
|
2020-04-22 21:10:00 +00:00
|
|
|
|
|
|
|
describe('disableAutocomplete', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
widget.enableAutocomplete();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should close the Dropdown', function() {
|
|
|
|
const toggleSpy = spyOn(widget.dropdownToggle, 'dropdown');
|
|
|
|
|
|
|
|
widget.dropdown.addClass('show');
|
|
|
|
widget.disableAutocomplete();
|
|
|
|
|
|
|
|
expect(toggleSpy).toHaveBeenCalledWith('toggle');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('enableAutocomplete', function() {
|
|
|
|
it('should open the Dropdown', function() {
|
|
|
|
const toggleSpy = spyOn(widget.dropdownToggle, 'dropdown');
|
|
|
|
widget.enableAutocomplete();
|
|
|
|
|
|
|
|
expect(toggleSpy).toHaveBeenCalledWith('toggle');
|
|
|
|
});
|
|
|
|
});
|
2018-04-05 08:19:55 +00:00
|
|
|
});
|