2018-10-09 11:25:53 -04:00
|
|
|
/* eslint-disable no-param-reassign */
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-04-16 14:40:30 -04:00
|
|
|
import GLDropdown from '~/gl_dropdown';
|
2017-05-16 17:01:51 -04:00
|
|
|
import '~/lib/utils/common_utils';
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
describe('glDropdown', function describeDropdown() {
|
2019-03-26 12:03:28 -04:00
|
|
|
preloadFixtures('static/gl_dropdown.html');
|
2019-04-25 05:35:33 -04:00
|
|
|
loadJSONFixtures('static/projects.json');
|
2017-09-05 18:29:54 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
const NON_SELECTABLE_CLASSES =
|
|
|
|
'.divider, .separator, .dropdown-header, .dropdown-menu-empty-item';
|
2016-10-22 11:46:53 -04:00
|
|
|
const SEARCH_INPUT_SELECTOR = '.dropdown-input-field';
|
2016-07-27 12:38:08 -04:00
|
|
|
const ITEM_SELECTOR = `.dropdown-content li:not(${NON_SELECTABLE_CLASSES})`;
|
|
|
|
const FOCUSED_ITEM_SELECTOR = `${ITEM_SELECTOR} a.is-focused`;
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
const ARROW_KEYS = {
|
|
|
|
DOWN: 40,
|
|
|
|
UP: 38,
|
|
|
|
ENTER: 13,
|
2018-10-17 03:13:26 -04:00
|
|
|
ESC: 27,
|
2016-07-27 12:38:08 -04:00
|
|
|
};
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-10-22 11:46:53 -04:00
|
|
|
let remoteCallback;
|
|
|
|
|
2017-01-12 00:52:42 -05:00
|
|
|
const navigateWithKeys = function navigateWithKeys(direction, steps, cb, i) {
|
2016-07-27 12:38:08 -04:00
|
|
|
i = i || 0;
|
2016-07-28 12:32:43 -04:00
|
|
|
if (!i) direction = direction.toUpperCase();
|
2016-07-27 12:38:08 -04:00
|
|
|
$('body').trigger({
|
|
|
|
type: 'keydown',
|
2016-07-28 12:32:43 -04:00
|
|
|
which: ARROW_KEYS[direction],
|
2018-10-17 03:13:26 -04:00
|
|
|
keyCode: ARROW_KEYS[direction],
|
2016-07-27 12:38:08 -04:00
|
|
|
});
|
2017-01-10 17:35:09 -05:00
|
|
|
i += 1;
|
2016-07-27 12:38:08 -04:00
|
|
|
if (i <= steps) {
|
|
|
|
navigateWithKeys(direction, steps, cb, i);
|
|
|
|
} else {
|
|
|
|
cb();
|
2016-07-26 20:17:07 -04:00
|
|
|
}
|
2016-07-27 12:38:08 -04:00
|
|
|
};
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-01-12 00:52:42 -05:00
|
|
|
const remoteMock = function remoteMock(data, term, callback) {
|
2016-10-22 11:46:53 -04:00
|
|
|
remoteCallback = callback.bind({}, data);
|
2017-01-10 17:54:56 -05:00
|
|
|
};
|
2016-10-22 11:46:53 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
function initDropDown(hasRemote, isFilterable, extraOpts = {}) {
|
2018-10-17 03:13:26 -04:00
|
|
|
const options = Object.assign(
|
|
|
|
{
|
|
|
|
selectable: true,
|
|
|
|
filterable: isFilterable,
|
|
|
|
data: hasRemote ? remoteMock.bind({}, this.projectsData) : this.projectsData,
|
|
|
|
search: {
|
|
|
|
fields: ['name'],
|
|
|
|
},
|
|
|
|
text: project => project.name_with_namespace || project.name,
|
|
|
|
id: project => project.id,
|
2017-09-05 18:29:54 -04:00
|
|
|
},
|
2018-10-17 03:13:26 -04:00
|
|
|
extraOpts,
|
|
|
|
);
|
|
|
|
this.dropdownButtonElement = $(
|
|
|
|
'#js-project-dropdown',
|
|
|
|
this.dropdownContainerElement,
|
|
|
|
).glDropdown(options);
|
2017-09-05 18:29:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-03-26 12:03:28 -04:00
|
|
|
loadFixtures('static/gl_dropdown.html');
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownContainerElement = $('.dropdown.inline');
|
|
|
|
this.$dropdownMenuElement = $('.dropdown-menu', this.dropdownContainerElement);
|
2019-04-25 05:35:33 -04:00
|
|
|
this.projectsData = getJSONFixture('static/projects.json');
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-10-22 11:46:53 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
afterEach(() => {
|
2018-02-20 17:20:48 -05:00
|
|
|
$('body').off('keydown');
|
|
|
|
this.dropdownContainerElement.off('keyup');
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should open on click', () => {
|
|
|
|
initDropDown.call(this, false);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownButtonElement.click();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('escapes HTML as text', () => {
|
|
|
|
this.projectsData[0].name_with_namespace = '<script>alert("testing");</script>';
|
2016-07-27 12:38:08 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
initDropDown.call(this, false);
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownButtonElement.click();
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect($('.dropdown-content li:first-child').text()).toBe('<script>alert("testing");</script>');
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should output HTML when highlighting', () => {
|
|
|
|
this.projectsData[0].name_with_namespace = 'testing';
|
|
|
|
$('.dropdown-input .dropdown-input-field').val('test');
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
initDropDown.call(this, false, true, {
|
|
|
|
highlight: true,
|
|
|
|
});
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownButtonElement.click();
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect($('.dropdown-content li:first-child').text()).toBe('testing');
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect($('.dropdown-content li:first-child a').html()).toBe(
|
|
|
|
'<b>t</b><b>e</b><b>s</b><b>t</b>ing',
|
|
|
|
);
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2017-05-09 02:57:13 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
describe('that is open', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initDropDown.call(this, false, false);
|
|
|
|
this.dropdownButtonElement.click();
|
2017-05-09 02:57:13 -04:00
|
|
|
});
|
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should select a following item on DOWN keypress', () => {
|
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(0);
|
2018-10-17 03:13:26 -04:00
|
|
|
const randomIndex = Math.floor(Math.random() * (this.projectsData.length - 1)) + 0;
|
2017-09-05 18:29:54 -04:00
|
|
|
navigateWithKeys('down', randomIndex, () => {
|
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
2018-10-17 03:13:26 -04:00
|
|
|
expect($(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, this.$dropdownMenuElement)).toHaveClass(
|
|
|
|
'is-focused',
|
|
|
|
);
|
2016-07-28 12:32:43 -04:00
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should select a previous item on UP keypress', () => {
|
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(0);
|
2018-10-17 03:13:26 -04:00
|
|
|
navigateWithKeys('down', this.projectsData.length - 1, () => {
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
2018-10-17 03:13:26 -04:00
|
|
|
const randomIndex = Math.floor(Math.random() * (this.projectsData.length - 2)) + 0;
|
2017-09-05 18:29:54 -04:00
|
|
|
navigateWithKeys('up', randomIndex, () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(
|
|
|
|
$(
|
|
|
|
`${ITEM_SELECTOR}:eq(${this.projectsData.length - 2 - randomIndex}) a`,
|
|
|
|
this.$dropdownMenuElement,
|
|
|
|
),
|
|
|
|
).toHaveClass('is-focused');
|
2016-07-27 12:38:08 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should click the selected item on ENTER keypress', () => {
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
const randomIndex = Math.floor(Math.random() * (this.projectsData.length - 1)) + 0;
|
|
|
|
navigateWithKeys('down', randomIndex, () => {
|
2018-04-16 14:40:30 -04:00
|
|
|
const visitUrl = spyOnDependency(GLDropdown, 'visitUrl').and.stub();
|
2017-09-05 18:29:54 -04:00
|
|
|
navigateWithKeys('enter', null, () => {
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
const link = $(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, this.$dropdownMenuElement);
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect(link).toHaveClass('is-active');
|
|
|
|
const linkedLocation = link.attr('href');
|
2018-10-09 14:03:09 -04:00
|
|
|
if (linkedLocation && linkedLocation !== '#') {
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(visitUrl).toHaveBeenCalledWith(linkedLocation);
|
2018-10-09 14:03:09 -04:00
|
|
|
}
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('should close on ESC keypress', () => {
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownContainerElement.trigger({
|
|
|
|
type: 'keyup',
|
|
|
|
which: ARROW_KEYS.ESC,
|
2018-10-17 03:13:26 -04:00
|
|
|
keyCode: ARROW_KEYS.ESC,
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-04-24 16:43:55 -04:00
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('show');
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
describe('opened and waiting for a remote callback', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initDropDown.call(this, true, true);
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show loading indicator while search results are being fetched by backend', () => {
|
|
|
|
const dropdownMenu = document.querySelector('.dropdown-menu');
|
|
|
|
|
2018-10-09 14:30:03 -04:00
|
|
|
expect(dropdownMenu.className.indexOf('is-loading')).not.toBe(-1);
|
2017-09-05 18:29:54 -04:00
|
|
|
remoteCallback();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-10-09 14:30:03 -04:00
|
|
|
expect(dropdownMenu.className.indexOf('is-loading')).toBe(-1);
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not focus search input while remote task is not complete', () => {
|
|
|
|
expect($(document.activeElement)).not.toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
remoteCallback();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus search input after remote task is complete', () => {
|
|
|
|
remoteCallback();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus on input when opening for the second time after transition', () => {
|
|
|
|
remoteCallback();
|
|
|
|
this.dropdownContainerElement.trigger({
|
|
|
|
type: 'keyup',
|
|
|
|
which: ARROW_KEYS.ESC,
|
2018-10-17 03:13:26 -04:00
|
|
|
keyCode: ARROW_KEYS.ESC,
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
this.dropdownContainerElement.trigger('transitionend');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('input focus with array data', () => {
|
|
|
|
it('should focus input when passing array data to drop down', () => {
|
|
|
|
initDropDown.call(this, false, true);
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
this.dropdownContainerElement.trigger('transitionend');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still have input value on close and restore', () => {
|
|
|
|
const $searchInput = $(SEARCH_INPUT_SELECTOR);
|
|
|
|
initDropDown.call(this, false, true);
|
|
|
|
$searchInput
|
|
|
|
.trigger('focus')
|
|
|
|
.val('g')
|
|
|
|
.trigger('input');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($searchInput.val()).toEqual('g');
|
|
|
|
this.dropdownButtonElement.trigger('hidden.bs.dropdown');
|
2018-10-17 03:13:26 -04:00
|
|
|
$searchInput.trigger('blur').trigger('focus');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect($searchInput.val()).toEqual('g');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renderItem', () => {
|
|
|
|
describe('without selected value', () => {
|
|
|
|
let dropdown;
|
2016-10-22 11:46:53 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-10-17 03:13:26 -04:00
|
|
|
const dropdownOptions = {};
|
2017-09-05 18:29:54 -04:00
|
|
|
const $dropdownDiv = $('<div />');
|
|
|
|
$dropdownDiv.glDropdown(dropdownOptions);
|
|
|
|
dropdown = $dropdownDiv.data('glDropdown');
|
2016-10-22 11:46:53 -04:00
|
|
|
});
|
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('marks items without ID as active', () => {
|
2018-10-17 03:13:26 -04:00
|
|
|
const dummyData = {};
|
2017-02-13 16:55:12 -05:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
const html = dropdown.renderItem(dummyData, null, null);
|
2017-02-13 16:55:12 -05:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
const link = html.querySelector('a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect(link).toHaveClass('is-active');
|
2016-10-22 11:46:53 -04:00
|
|
|
});
|
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
it('does not mark items with ID as active', () => {
|
|
|
|
const dummyData = {
|
2018-10-17 03:13:26 -04:00
|
|
|
id: 'ea',
|
2017-09-05 18:29:54 -04:00
|
|
|
};
|
2016-10-22 11:46:53 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
const html = dropdown.renderItem(dummyData, null, null);
|
2016-10-22 11:46:53 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
const link = html.querySelector('a');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-09-05 18:29:54 -04:00
|
|
|
expect(link).not.toHaveClass('is-active');
|
2016-10-22 11:46:53 -04:00
|
|
|
});
|
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2018-03-27 14:51:11 -04:00
|
|
|
|
|
|
|
it('should keep selected item after selecting a second time', () => {
|
|
|
|
const options = {
|
|
|
|
isSelectable(item, $el) {
|
|
|
|
return !$el.hasClass('is-active');
|
|
|
|
},
|
|
|
|
toggleLabel(item) {
|
|
|
|
return item && item.id;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
initDropDown.call(this, false, false, options);
|
|
|
|
const $item = $(`${ITEM_SELECTOR}:first() a`, this.$dropdownMenuElement);
|
|
|
|
|
|
|
|
// select item the first time
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
$item.click();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-03-27 14:51:11 -04:00
|
|
|
expect($item).toHaveClass('is-active');
|
|
|
|
// select item the second time
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
$item.click();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-03-27 14:51:11 -04:00
|
|
|
expect($item).toHaveClass('is-active');
|
|
|
|
|
|
|
|
expect($('.dropdown-toggle-text')).toHaveText(this.projectsData[0].id.toString());
|
|
|
|
});
|
2017-09-05 18:29:54 -04:00
|
|
|
});
|