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

108 lines
3.1 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';
import { DEFAULT_CLASSNAMES, DEFAULT_CONFIG } from '../constants';
2017-10-29 19:56:24 +01:00
describe('components/wrappedSelect', () => {
let instance;
let choicesInstance;
2017-12-20 13:38:16 +01:00
let selectElement;
beforeEach(() => {
choicesInstance = {
config: {
...DEFAULT_CONFIG,
},
};
2017-12-20 13:38:16 +01:00
selectElement = document.createElement('select');
selectElement.id = 'target';
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', '');
}
selectElement.appendChild(option);
}
document.body.appendChild(selectElement);
instance = new WrappedSelect(choicesInstance, document.getElementById('target'), DEFAULT_CLASSNAMES);
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
2017-12-19 14:08:57 +01:00
describe('inherited methods', () => {
['getElement', '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);
});
});
});
});
2017-12-20 13:38:16 +01:00
describe('getPlaceholderOption', () => {
it('returns option element with placeholder attribute', () => {
const output = instance.getPlaceholderOption();
expect(output).to.be.instanceOf(HTMLOptionElement);
});
});
2017-12-19 14:08:57 +01:00
2017-12-20 13:38:16 +01:00
describe('getOptions', () => {
it('returns all option elements', () => {
const output = instance.getOptions();
expect(output).to.be.an('array');
output.forEach((option) => {
expect(option).to.be.instanceOf(HTMLOptionElement);
});
});
});
2017-12-19 14:08:57 +01:00
// describe('getOptionGroups', () => {
2017-12-20 13:38:16 +01:00
// it('returns all option groups', () => {
2017-12-19 14:08:57 +01:00
2017-12-20 13:38:16 +01:00
// });
2017-12-19 14:08:57 +01:00
// });
// describe('setOptions', () => {
// });
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);
});
});
});