2016-10-26 04:02:58 -04:00
|
|
|
/* eslint-disable */
|
2016-07-26 20:17:07 -04:00
|
|
|
/*= require jquery */
|
|
|
|
/*= require gl_dropdown */
|
|
|
|
/*= require turbolinks */
|
|
|
|
/*= require lib/utils/common_utils */
|
|
|
|
/*= require lib/utils/type_utility */
|
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
(() => {
|
|
|
|
const NON_SELECTABLE_CLASSES = '.divider, .separator, .dropdown-header, .dropdown-menu-empty-link';
|
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,
|
|
|
|
ESC: 27
|
|
|
|
};
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-10-22 11:46:53 -04:00
|
|
|
let remoteCallback;
|
|
|
|
|
2016-07-28 12:32:43 -04:00
|
|
|
let 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],
|
|
|
|
keyCode: ARROW_KEYS[direction]
|
2016-07-27 12:38:08 -04:00
|
|
|
});
|
|
|
|
i++;
|
|
|
|
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
|
|
|
|
2016-10-22 11:46:53 -04:00
|
|
|
let remoteMock = function remoteMock(data, term, callback) {
|
|
|
|
remoteCallback = callback.bind({}, data);
|
|
|
|
}
|
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
describe('Dropdown', function describeDropdown() {
|
|
|
|
fixture.preload('gl_dropdown.html');
|
|
|
|
fixture.preload('projects.json');
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-10-22 11:46:53 -04:00
|
|
|
function initDropDown(hasRemote, isFilterable) {
|
2016-07-28 12:32:43 -04:00
|
|
|
this.dropdownButtonElement = $('#js-project-dropdown', this.dropdownContainerElement).glDropdown({
|
|
|
|
selectable: true,
|
2016-10-22 11:46:53 -04:00
|
|
|
filterable: isFilterable,
|
|
|
|
data: hasRemote ? remoteMock.bind({}, this.projectsData) : this.projectsData,
|
2016-07-28 12:32:43 -04:00
|
|
|
text: (project) => {
|
2016-08-11 10:10:23 -04:00
|
|
|
(project.name_with_namespace || project.name);
|
2016-07-28 12:32:43 -04:00
|
|
|
},
|
|
|
|
id: (project) => {
|
2016-08-11 10:10:23 -04:00
|
|
|
project.id;
|
2016-07-28 12:32:43 -04:00
|
|
|
}
|
|
|
|
});
|
2016-10-22 11:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
fixture.load('gl_dropdown.html');
|
|
|
|
this.dropdownContainerElement = $('.dropdown.inline');
|
|
|
|
this.$dropdownMenuElement = $('.dropdown-menu', this.dropdownContainerElement);
|
|
|
|
this.projectsData = fixture.load('projects.json')[0];
|
2016-07-28 12:32:43 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-07-28 12:32:43 -04:00
|
|
|
afterEach(() => {
|
2016-07-27 12:38:08 -04:00
|
|
|
$('body').unbind('keydown');
|
|
|
|
this.dropdownContainerElement.unbind('keyup');
|
2016-07-28 12:32:43 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
it('should open on click', () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
initDropDown.call(this, false);
|
2016-07-27 12:38:08 -04:00
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('open');
|
2016-07-26 20:17:07 -04:00
|
|
|
this.dropdownButtonElement.click();
|
2016-07-27 12:38:08 -04:00
|
|
|
expect(this.dropdownContainerElement).toHaveClass('open');
|
|
|
|
});
|
|
|
|
|
2016-07-28 12:32:43 -04:00
|
|
|
describe('that is open', () => {
|
|
|
|
beforeEach(() => {
|
2016-10-22 11:46:53 -04:00
|
|
|
initDropDown.call(this, false, false);
|
2016-07-27 12:38:08 -04:00
|
|
|
this.dropdownButtonElement.click();
|
2016-07-28 12:32:43 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
it('should select a following item on DOWN keypress', () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(0);
|
2016-07-27 12:38:08 -04:00
|
|
|
let randomIndex = (Math.floor(Math.random() * (this.projectsData.length - 1)) + 0);
|
|
|
|
navigateWithKeys('down', randomIndex, () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
|
|
|
expect($(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, this.$dropdownMenuElement)).toHaveClass('is-focused');
|
2016-07-27 12:38:08 -04:00
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
it('should select a previous item on UP keypress', () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(0);
|
2016-07-27 12:38:08 -04:00
|
|
|
navigateWithKeys('down', (this.projectsData.length - 1), () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
2016-07-27 12:38:08 -04:00
|
|
|
let randomIndex = (Math.floor(Math.random() * (this.projectsData.length - 2)) + 0);
|
|
|
|
navigateWithKeys('up', randomIndex, () => {
|
2016-10-22 11:46:53 -04:00
|
|
|
expect($(FOCUSED_ITEM_SELECTOR, this.$dropdownMenuElement).length).toBe(1);
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
it('should click the selected item on ENTER keypress', () => {
|
|
|
|
expect(this.dropdownContainerElement).toHaveClass('open')
|
|
|
|
let randomIndex = Math.floor(Math.random() * (this.projectsData.length - 1)) + 0
|
|
|
|
navigateWithKeys('down', randomIndex, () => {
|
|
|
|
spyOn(Turbolinks, 'visit').and.stub();
|
|
|
|
navigateWithKeys('enter', null, () => {
|
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('open');
|
2016-10-22 11:46:53 -04:00
|
|
|
let link = $(`${ITEM_SELECTOR}:eq(${randomIndex}) a`, this.$dropdownMenuElement);
|
2016-07-27 12:38:08 -04:00
|
|
|
expect(link).toHaveClass('is-active');
|
|
|
|
let linkedLocation = link.attr('href');
|
|
|
|
if (linkedLocation && linkedLocation !== '#') expect(Turbolinks.visit).toHaveBeenCalledWith(linkedLocation);
|
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-27 12:38:08 -04:00
|
|
|
it('should close on ESC keypress', () => {
|
|
|
|
expect(this.dropdownContainerElement).toHaveClass('open');
|
|
|
|
this.dropdownContainerElement.trigger({
|
|
|
|
type: 'keyup',
|
|
|
|
which: ARROW_KEYS.ESC,
|
|
|
|
keyCode: ARROW_KEYS.ESC
|
|
|
|
});
|
|
|
|
expect(this.dropdownContainerElement).not.toHaveClass('open');
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
|
|
|
});
|
2016-10-22 11:46:53 -04:00
|
|
|
|
|
|
|
describe('opened and waiting for a remote callback', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
initDropDown.call(this, true, true);
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not focus search input while remote task is not complete', ()=> {
|
|
|
|
expect($(document.activeElement)).not.toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
remoteCallback();
|
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus search input after remote task is complete', ()=> {
|
|
|
|
remoteCallback();
|
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should focus on input when opening for the second time', ()=> {
|
|
|
|
remoteCallback();
|
|
|
|
this.dropdownContainerElement.trigger({
|
|
|
|
type: 'keyup',
|
|
|
|
which: ARROW_KEYS.ESC,
|
|
|
|
keyCode: ARROW_KEYS.ESC
|
|
|
|
});
|
|
|
|
this.dropdownButtonElement.click();
|
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
expect($(document.activeElement)).toEqual($(SEARCH_INPUT_SELECTOR));
|
|
|
|
});
|
|
|
|
});
|
2016-07-26 20:17:07 -04:00
|
|
|
});
|
2016-07-28 12:32:43 -04:00
|
|
|
})();
|