Choices/src/scripts/components/wrapped-select.test.js

160 lines
4.7 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai';
2017-12-19 14:08:57 +01:00
import { stub } from 'sinon';
import WrappedElement from './wrapped-element';
import WrappedSelect from './wrapped-select';
2018-05-21 18:01:03 +02:00
import { DEFAULT_CLASSNAMES } from '../constants';
2017-10-29 19:56:24 +01:00
describe('components/wrappedSelect', () => {
let instance;
let element;
beforeEach(() => {
element = document.createElement('select');
element.id = 'target';
2017-12-20 13:38:16 +01:00
for (let i = 1; i <= 4; i++) {
const option = document.createElement('option');
option.value = `Value ${i}`;
option.innerHTML = `Label ${i}`;
if (i === 1) {
option.setAttribute('placeholder', '');
}
element.appendChild(option);
2017-12-20 13:38:16 +01:00
}
document.body.appendChild(element);
2017-12-20 13:38:16 +01:00
2018-05-21 18:01:03 +02:00
instance = new WrappedSelect({
element: document.getElementById('target'),
classNames: DEFAULT_CLASSNAMES,
});
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
2017-12-19 14:08:57 +01:00
describe('constructor', () => {
it('assigns choices element to class', () => {
expect(instance.element).to.eql(element);
});
it('assigns classnames to class', () => {
expect(instance.classNames).to.eql(DEFAULT_CLASSNAMES);
});
});
2017-12-19 14:08:57 +01:00
describe('inherited methods', () => {
2018-04-24 13:54:45 +02:00
['conceal', 'reveal', 'enable', 'disable'].forEach((method) => {
2017-12-20 13:38:16 +01:00
beforeEach(() => {
stub(WrappedElement.prototype, method);
});
afterEach(() => {
WrappedElement.prototype[method].restore();
});
2017-12-19 14:08:57 +01:00
describe(method, () => {
it(`calls super.${method}`, () => {
2017-12-20 13:38:16 +01:00
expect(WrappedElement.prototype[method].called).to.equal(false);
2017-12-19 14:08:57 +01:00
instance[method]();
expect(WrappedElement.prototype[method].called).to.equal(true);
});
});
});
});
2018-04-24 13:54:45 +02:00
describe('placeholderOption getter', () => {
2017-12-20 13:38:16 +01:00
it('returns option element with placeholder attribute', () => {
2018-04-24 13:54:45 +02:00
expect(instance.placeholderOption).to.be.instanceOf(HTMLOptionElement);
2017-12-20 13:38:16 +01:00
});
});
2017-12-19 14:08:57 +01:00
2018-04-24 13:54:45 +02:00
describe('options getter', () => {
2017-12-20 13:38:16 +01:00
it('returns all option elements', () => {
2018-04-24 13:54:45 +02:00
const { options } = instance;
expect(options).to.be.an('array');
options.forEach((option) => {
2017-12-20 13:38:16 +01:00
expect(option).to.be.instanceOf(HTMLOptionElement);
});
});
});
2017-12-19 14:08:57 +01:00
2018-04-24 13:54:45 +02:00
describe('optionGroups getter', () => {
it('returns an array of all option groups', () => {
for (let i = 1; i <= 3; i++) {
const group = document.createElement('optgroup');
instance.element.appendChild(group);
}
2018-04-24 13:54:45 +02:00
const { optionGroups } = instance;
expect(optionGroups.length).to.equal(3);
optionGroups.forEach((option) => {
expect(option).to.be.instanceOf(HTMLOptGroupElement);
});
});
});
2018-04-24 13:54:45 +02:00
describe('options setter', () => {
let appendDocFragmentStub;
const options = [
{
value: '1',
label: 'Test 1',
selected: false,
disabled: true,
},
{
value: '2',
label: 'Test 2',
selected: true,
disabled: false,
},
];
2017-12-19 14:08:57 +01:00
beforeEach(() => {
appendDocFragmentStub = stub();
instance.appendDocFragment = appendDocFragmentStub;
});
2017-12-19 14:08:57 +01:00
afterEach(() => {
instance.appendDocFragment.reset();
});
it('creates an option element for each passed object, adds it to a fragment and calls appendDocFragment with created fragment', () => {
expect(appendDocFragmentStub.called).to.equal(false);
2018-04-24 13:54:45 +02:00
instance.options = options;
expect(appendDocFragmentStub.called).to.equal(true);
const fragment = appendDocFragmentStub.firstCall.args[0];
const selectElement = document.createElement('select');
selectElement.appendChild(fragment);
2017-12-19 14:08:57 +01:00
expect(fragment).to.be.instanceOf(DocumentFragment);
expect(selectElement.options.length).to.equal(2);
expect(selectElement.options[0].value).to.equal(options[0].value);
expect(selectElement.options[1].value).to.equal(options[1].value);
});
});
2017-12-19 14:08:57 +01:00
2017-12-20 13:38:16 +01:00
describe('appendDocFragment', () => {
it('empties contents of element', () => {
expect(instance.element.getElementsByTagName('option').length).to.equal(4);
instance.appendDocFragment(document.createDocumentFragment());
expect(instance.element.getElementsByTagName('option').length).to.equal(0);
});
2017-12-19 14:08:57 +01:00
2017-12-20 13:38:16 +01:00
it('appends passed fragment to element', () => {
const fragment = document.createDocumentFragment();
const elementToAppend = document.createElement('div');
elementToAppend.id = 'fragment-target';
fragment.appendChild(elementToAppend);
expect(instance.element.querySelector('#fragment-target')).to.equal(null);
instance.appendDocFragment(fragment);
expect(instance.element.querySelector('#fragment-target')).to.eql(elementToAppend);
});
});
});