2017-04-06 10:36:26 +00:00
|
|
|
import DropDown from '~/droplab/drop_down';
|
|
|
|
import utils from '~/droplab/utils';
|
2018-02-01 14:19:27 +00:00
|
|
|
import { SELECTED_CLASS } from '~/droplab/constants';
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('DropLab DropDown', function() {
|
|
|
|
describe('class constructor', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
spyOn(DropDown.prototype, 'getItems');
|
|
|
|
spyOn(DropDown.prototype, 'initTemplateString');
|
|
|
|
spyOn(DropDown.prototype, 'addEvents');
|
|
|
|
|
|
|
|
this.list = { innerHTML: 'innerHTML' };
|
|
|
|
this.dropdown = new DropDown(this.list);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .hidden property to true', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hidden).toBe(true);
|
2017-05-12 17:16:47 +00:00
|
|
|
});
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .list property', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.list).toBe(this.list);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('calls .getItems', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(DropDown.prototype.getItems).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('calls .initTemplateString', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(DropDown.prototype.initTemplateString).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('calls .addEvents', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(DropDown.prototype.addEvents).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .initialState property to the .list.innerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.initialState).toBe(this.list.innerHTML);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if the list argument is a string', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.element = {};
|
|
|
|
this.selector = '.selector';
|
|
|
|
|
|
|
|
spyOn(Document.prototype, 'querySelector').and.returnValue(this.element);
|
|
|
|
|
|
|
|
this.dropdown = new DropDown(this.selector);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('calls .querySelector with the selector string', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(Document.prototype.querySelector).toHaveBeenCalledWith(this.selector);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .list property element', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.list).toBe(this.element);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('getItems', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { querySelectorAll: () => {} };
|
|
|
|
this.dropdown = { list: this.list };
|
|
|
|
this.nodeList = [];
|
|
|
|
|
|
|
|
spyOn(this.list, 'querySelectorAll').and.returnValue(this.nodeList);
|
|
|
|
|
|
|
|
this.getItems = DropDown.prototype.getItems.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('calls .querySelectorAll with a list item query', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.querySelectorAll).toHaveBeenCalledWith('li');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .items property to the returned list items', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.items).toEqual(jasmine.any(Array));
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('returns the .items', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.getItems).toEqual(jasmine.any(Array));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('initTemplateString', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.items = [{ outerHTML: '<a></a>' }, { outerHTML: '<img>' }];
|
|
|
|
this.dropdown = { items: this.items };
|
|
|
|
|
|
|
|
DropDown.prototype.initTemplateString.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set .templateString to the last items .outerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.templateString).toBe(this.items[1].outerHTML);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should not set .templateString to a non-last items .outerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.templateString).not.toBe(this.items[0].outerHTML);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if .items is not set', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { getItems: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'getItems').and.returnValue([]);
|
|
|
|
|
|
|
|
DropDown.prototype.initTemplateString.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .getItems', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.getItems).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if items array is empty', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { items: [] };
|
|
|
|
|
|
|
|
DropDown.prototype.initTemplateString.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set .templateString to an empty string', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.templateString).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('clickEvent', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-19 21:18:39 +00:00
|
|
|
this.classList = jasmine.createSpyObj('classList', ['contains']);
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { dispatchEvent: () => {} };
|
2018-02-01 14:19:27 +00:00
|
|
|
this.dropdown = {
|
|
|
|
hideOnClick: true,
|
|
|
|
hide: () => {},
|
|
|
|
list: this.list,
|
|
|
|
addSelectedClass: () => {},
|
|
|
|
};
|
|
|
|
this.event = {
|
|
|
|
preventDefault: () => {},
|
|
|
|
target: {
|
|
|
|
classList: this.classList,
|
|
|
|
closest: () => null,
|
|
|
|
},
|
|
|
|
};
|
2017-04-06 10:36:26 +00:00
|
|
|
this.customEvent = {};
|
2018-02-01 14:19:27 +00:00
|
|
|
this.dummyListItem = document.createElement('li');
|
2018-10-17 07:13:26 +00:00
|
|
|
spyOn(this.event.target, 'closest').and.callFake(selector => {
|
2018-02-01 14:19:27 +00:00
|
|
|
if (selector === 'li') {
|
|
|
|
return this.dummyListItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
2017-04-06 10:36:26 +00:00
|
|
|
|
|
|
|
spyOn(this.dropdown, 'hide');
|
|
|
|
spyOn(this.dropdown, 'addSelectedClass');
|
|
|
|
spyOn(this.list, 'dispatchEvent');
|
|
|
|
spyOn(this.event, 'preventDefault');
|
|
|
|
spyOn(window, 'CustomEvent').and.returnValue(this.customEvent);
|
2017-04-19 21:18:39 +00:00
|
|
|
this.classList.contains.and.returnValue(false);
|
2018-02-01 14:19:27 +00:00
|
|
|
});
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call event.target.closest', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2018-02-01 14:19:27 +00:00
|
|
|
expect(this.event.target.closest).toHaveBeenCalledWith('.droplab-item-ignore');
|
|
|
|
expect(this.event.target.closest).toHaveBeenCalledWith('li');
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call addSelectedClass', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
|
|
|
expect(this.dropdown.addSelectedClass).toHaveBeenCalledWith(this.dummyListItem);
|
2017-05-12 17:16:47 +00:00
|
|
|
});
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .preventDefault', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.event.preventDefault).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .hide', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hide).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should construct CustomEvent', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-02-01 14:19:27 +00:00
|
|
|
expect(window.CustomEvent).toHaveBeenCalledWith('click.dl', jasmine.any(Object));
|
2017-04-19 21:18:39 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .dispatchEvent with the customEvent', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.dispatchEvent).toHaveBeenCalledWith(this.customEvent);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if the target is a UL element', function() {
|
|
|
|
beforeEach(function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
this.event.target = document.createElement('ul');
|
2017-04-19 21:18:39 +00:00
|
|
|
|
2018-02-01 14:19:27 +00:00
|
|
|
spyOn(this.event.target, 'closest');
|
2017-04-19 21:18:39 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should return immediately', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
|
|
|
expect(this.event.target.closest).not.toHaveBeenCalled();
|
|
|
|
expect(this.dropdown.addSelectedClass).not.toHaveBeenCalled();
|
2017-04-19 21:18:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if the target has the droplab-item-ignore class', function() {
|
|
|
|
beforeEach(function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
this.ignoredButton = document.createElement('button');
|
|
|
|
this.ignoredButton.classList.add('droplab-item-ignore');
|
|
|
|
this.event.target = this.ignoredButton;
|
2017-04-07 01:50:10 +00:00
|
|
|
|
2018-02-01 14:19:27 +00:00
|
|
|
spyOn(this.ignoredButton, 'closest').and.callThrough();
|
|
|
|
});
|
2017-04-07 01:50:10 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('does not select element', function() {
|
2017-04-07 01:50:10 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2018-02-01 14:19:27 +00:00
|
|
|
expect(this.ignoredButton.closest.calls.count()).toBe(1);
|
|
|
|
expect(this.ignoredButton.closest).toHaveBeenCalledWith('.droplab-item-ignore');
|
|
|
|
expect(this.dropdown.addSelectedClass).not.toHaveBeenCalled();
|
2017-04-07 01:50:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if no selected element exists', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.event.preventDefault.calls.reset();
|
2018-02-01 14:19:27 +00:00
|
|
|
this.dummyListItem = null;
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should return before .preventDefault is called', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.event.preventDefault).not.toHaveBeenCalled();
|
2018-02-01 14:19:27 +00:00
|
|
|
expect(this.dropdown.addSelectedClass).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if hideOnClick is false', () => {
|
2018-10-17 07:13:26 +00:00
|
|
|
beforeEach(function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
this.dropdown.hideOnClick = false;
|
|
|
|
this.dropdown.hide.calls.reset();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should not call .hide', function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
DropDown.prototype.clickEvent.call(this.dropdown, this.event);
|
|
|
|
|
|
|
|
expect(this.dropdown.hide).not.toHaveBeenCalled();
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('addSelectedClass', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.items = Array(4).forEach((item, i) => {
|
|
|
|
this.items[i] = { classList: { add: () => {} } };
|
|
|
|
spyOn(this.items[i].classList, 'add');
|
|
|
|
});
|
|
|
|
this.selected = { classList: { add: () => {} } };
|
|
|
|
this.dropdown = { removeSelectedClasses: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'removeSelectedClasses');
|
|
|
|
spyOn(this.selected.classList, 'add');
|
|
|
|
|
|
|
|
DropDown.prototype.addSelectedClass.call(this.dropdown, this.selected);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .removeSelectedClasses', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.removeSelectedClasses).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .classList.add', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.selected.classList.add).toHaveBeenCalledWith(SELECTED_CLASS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('removeSelectedClasses', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.items = Array(4);
|
|
|
|
this.items.forEach((item, i) => {
|
|
|
|
this.items[i] = { classList: { add: () => {} } };
|
|
|
|
spyOn(this.items[i].classList, 'add');
|
|
|
|
});
|
|
|
|
this.dropdown = { items: this.items };
|
|
|
|
|
|
|
|
DropDown.prototype.removeSelectedClasses.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .classList.remove for all items', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.items.forEach((item, i) => {
|
|
|
|
expect(this.items[i].classList.add).toHaveBeenCalledWith(SELECTED_CLASS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if .items is not set', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { getItems: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'getItems').and.returnValue([]);
|
|
|
|
|
|
|
|
DropDown.prototype.removeSelectedClasses.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .getItems', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.getItems).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('addEvents', function() {
|
|
|
|
beforeEach(function() {
|
2018-02-01 14:19:27 +00:00
|
|
|
this.list = {
|
|
|
|
addEventListener: () => {},
|
|
|
|
querySelectorAll: () => [],
|
|
|
|
};
|
2017-11-25 11:33:05 +00:00
|
|
|
this.dropdown = {
|
|
|
|
list: this.list,
|
|
|
|
clickEvent: () => {},
|
|
|
|
closeDropdown: () => {},
|
|
|
|
eventWrapper: {},
|
|
|
|
};
|
2018-02-01 14:19:27 +00:00
|
|
|
});
|
2017-04-06 10:36:26 +00:00
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .addEventListener', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
spyOn(this.list, 'addEventListener');
|
|
|
|
|
|
|
|
DropDown.prototype.addEvents.call(this.dropdown);
|
|
|
|
|
|
|
|
expect(this.list.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function));
|
2017-11-25 11:33:05 +00:00
|
|
|
expect(this.list.addEventListener).toHaveBeenCalledWith('keyup', jasmine.any(Function));
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('setData', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { render: () => {} };
|
|
|
|
this.data = ['data'];
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'render');
|
|
|
|
|
|
|
|
DropDown.prototype.setData.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set .data', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.data).toBe(this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .render with the .data', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.render).toHaveBeenCalledWith(this.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('addData', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { render: () => {}, data: ['data1'] };
|
|
|
|
this.data = ['data2'];
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'render');
|
|
|
|
spyOn(Array.prototype, 'concat').and.callThrough();
|
|
|
|
|
|
|
|
DropDown.prototype.addData.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .concat with data', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(Array.prototype.concat).toHaveBeenCalledWith(this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set .data with concatination', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.data).toEqual(['data1', 'data2']);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .render with the .data', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.render).toHaveBeenCalledWith(['data1', 'data2']);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if .data is undefined', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { render: () => {}, data: undefined };
|
|
|
|
this.data = ['data2'];
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'render');
|
|
|
|
|
|
|
|
DropDown.prototype.addData.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set .data with concatination', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.data).toEqual(['data2']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('render', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.renderableList = {};
|
2020-01-02 13:03:23 +00:00
|
|
|
this.list = {
|
|
|
|
querySelector: q => {
|
|
|
|
if (q === '.filter-dropdown-loading') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.renderableList;
|
|
|
|
},
|
|
|
|
dispatchEvent: () => {},
|
|
|
|
};
|
|
|
|
this.dropdown = { renderChildren: () => {}, list: this.list };
|
2017-04-06 10:36:26 +00:00
|
|
|
this.data = [0, 1];
|
2017-08-30 07:48:55 +00:00
|
|
|
this.customEvent = {};
|
2017-04-06 10:36:26 +00:00
|
|
|
|
|
|
|
spyOn(this.dropdown, 'renderChildren').and.callFake(data => data);
|
2017-08-30 07:48:55 +00:00
|
|
|
spyOn(this.list, 'dispatchEvent');
|
2017-04-06 10:36:26 +00:00
|
|
|
spyOn(this.data, 'map').and.callThrough();
|
2017-08-30 07:48:55 +00:00
|
|
|
spyOn(window, 'CustomEvent').and.returnValue(this.customEvent);
|
2017-04-06 10:36:26 +00:00
|
|
|
|
|
|
|
DropDown.prototype.render.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .map', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.data.map).toHaveBeenCalledWith(jasmine.any(Function));
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .renderChildren for each data item', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.renderChildren.calls.count()).toBe(this.data.length);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the renderableList .innerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.renderableList.innerHTML).toBe('01');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call render.dl', function() {
|
2017-08-30 07:48:55 +00:00
|
|
|
expect(window.CustomEvent).toHaveBeenCalledWith('render.dl', jasmine.any(Object));
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call dispatchEvent with the customEvent', function() {
|
2017-08-30 07:48:55 +00:00
|
|
|
expect(this.list.dispatchEvent).toHaveBeenCalledWith(this.customEvent);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if no data argument is passed', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.data.map.calls.reset();
|
|
|
|
this.dropdown.renderChildren.calls.reset();
|
|
|
|
|
|
|
|
DropDown.prototype.render.call(this.dropdown, undefined);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should not call .map', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.data.map).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should not call .renderChildren', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.renderChildren).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if no dynamic list is present', function() {
|
|
|
|
beforeEach(function() {
|
2017-08-30 07:48:55 +00:00
|
|
|
this.list = { querySelector: () => {}, dispatchEvent: () => {} };
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { renderChildren: () => {}, list: this.list };
|
|
|
|
this.data = [0, 1];
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'renderChildren').and.callFake(data => data);
|
|
|
|
spyOn(this.list, 'querySelector');
|
|
|
|
spyOn(this.data, 'map').and.callThrough();
|
|
|
|
|
|
|
|
DropDown.prototype.render.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('sets the .list .innerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.innerHTML).toBe('01');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('renderChildren', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.templateString = 'templateString';
|
2017-05-12 17:16:47 +00:00
|
|
|
this.dropdown = { templateString: this.templateString };
|
2017-04-06 10:36:26 +00:00
|
|
|
this.data = { droplab_hidden: true };
|
|
|
|
this.html = 'html';
|
|
|
|
this.template = { firstChild: { outerHTML: 'outerHTML', style: {} } };
|
|
|
|
|
2017-04-12 08:18:22 +00:00
|
|
|
spyOn(utils, 'template').and.returnValue(this.html);
|
2017-04-06 10:36:26 +00:00
|
|
|
spyOn(document, 'createElement').and.returnValue(this.template);
|
2017-05-12 17:16:47 +00:00
|
|
|
spyOn(DropDown, 'setImagesSrc');
|
2017-04-06 10:36:26 +00:00
|
|
|
|
|
|
|
this.renderChildren = DropDown.prototype.renderChildren.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call utils.t with .templateString and data', function() {
|
2017-04-12 08:18:22 +00:00
|
|
|
expect(utils.template).toHaveBeenCalledWith(this.templateString, this.data);
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call document.createElement', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(document.createElement).toHaveBeenCalledWith('div');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set the templates .innerHTML to the HTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.template.innerHTML).toBe(this.html);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .setImagesSrc with the template', function() {
|
2017-05-12 17:16:47 +00:00
|
|
|
expect(DropDown.setImagesSrc).toHaveBeenCalledWith(this.template);
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set the template display to none', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.template.firstChild.style.display).toBe('none');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should return the templates .firstChild.outerHTML', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.renderChildren).toBe(this.template.firstChild.outerHTML);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if droplab_hidden is false', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.data = { droplab_hidden: false };
|
|
|
|
this.renderChildren = DropDown.prototype.renderChildren.call(this.dropdown, this.data);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should set the template display to block', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.template.firstChild.style.display).toBe('block');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('setImagesSrc', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.template = { querySelectorAll: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.template, 'querySelectorAll').and.returnValue([]);
|
|
|
|
|
2017-05-12 17:16:47 +00:00
|
|
|
DropDown.setImagesSrc(this.template);
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .querySelectorAll', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.template.querySelectorAll).toHaveBeenCalledWith('img[data-src]');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('show', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { style: {} };
|
|
|
|
this.dropdown = { list: this.list, hidden: true };
|
|
|
|
|
|
|
|
DropDown.prototype.show.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should set .list display to block', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.style.display).toBe('block');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should set .hidden to false', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hidden).toBe(false);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if .hidden is false', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { style: {} };
|
|
|
|
this.dropdown = { list: this.list, hidden: false };
|
|
|
|
|
|
|
|
this.show = DropDown.prototype.show.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should return undefined', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.show).toEqual(undefined);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should not set .list display to block', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.style.display).not.toEqual('block');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('hide', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { style: {} };
|
|
|
|
this.dropdown = { list: this.list };
|
|
|
|
|
|
|
|
DropDown.prototype.hide.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should set .list display to none', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.list.style.display).toBe('none');
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should set .hidden to true', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hidden).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('toggle', function() {
|
|
|
|
beforeEach(function() {
|
2017-05-12 17:16:47 +00:00
|
|
|
this.hidden = true;
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { hidden: this.hidden, show: () => {}, hide: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'show');
|
|
|
|
spyOn(this.dropdown, 'hide');
|
|
|
|
|
|
|
|
DropDown.prototype.toggle.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .show', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.show).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('if .hidden is false', function() {
|
|
|
|
beforeEach(function() {
|
2017-05-12 17:16:47 +00:00
|
|
|
this.hidden = false;
|
2017-04-06 10:36:26 +00:00
|
|
|
this.dropdown = { hidden: this.hidden, show: () => {}, hide: () => {} };
|
|
|
|
|
|
|
|
spyOn(this.dropdown, 'show');
|
|
|
|
spyOn(this.dropdown, 'hide');
|
|
|
|
|
|
|
|
DropDown.prototype.toggle.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('should call .hide', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hide).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
describe('destroy', function() {
|
|
|
|
beforeEach(function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
this.list = { removeEventListener: () => {} };
|
|
|
|
this.eventWrapper = { clickEvent: 'clickEvent' };
|
|
|
|
this.dropdown = { list: this.list, hide: () => {}, eventWrapper: this.eventWrapper };
|
|
|
|
|
|
|
|
spyOn(this.list, 'removeEventListener');
|
|
|
|
spyOn(this.dropdown, 'hide');
|
|
|
|
|
|
|
|
DropDown.prototype.destroy.call(this.dropdown);
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should call .hide', function() {
|
2017-04-06 10:36:26 +00:00
|
|
|
expect(this.dropdown.hide).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-10-17 07:13:26 +00:00
|
|
|
it('it should call .removeEventListener', function() {
|
|
|
|
expect(this.list.removeEventListener).toHaveBeenCalledWith(
|
|
|
|
'click',
|
|
|
|
this.eventWrapper.clickEvent,
|
|
|
|
);
|
2017-04-06 10:36:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|