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

79 lines
1.9 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 WrappedInput from './wrapped-input';
2018-05-21 18:01:03 +02:00
import { DEFAULT_CLASSNAMES } from '../constants';
2017-10-29 19:56:24 +01:00
describe('components/wrappedInput', () => {
let instance;
let element;
2018-05-21 18:01:03 +02:00
const delimiter = '-';
beforeEach(() => {
element = document.createElement('input');
2018-05-21 18:01:03 +02:00
instance = new WrappedInput({
element,
classNames: DEFAULT_CLASSNAMES,
delimiter,
});
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
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-19 14:08:57 +01:00
describe(method, () => {
beforeEach(() => {
stub(WrappedElement.prototype, method);
});
afterEach(() => {
WrappedElement.prototype[method].restore();
});
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('value setter', () => {
const data = [
{
id: 'ID 1',
value: 'Value 1',
},
{
id: 'ID 2',
value: 'Value 2',
},
{
id: 'ID 3',
value: 'Value 3',
},
];
it('sets delimited value of element based on passed data', () => {
expect(instance.element.value).to.equal('');
2018-04-24 13:54:45 +02:00
instance.value = data;
2018-05-21 18:01:03 +02:00
expect(instance.value).to.equal(`Value 1${delimiter}Value 2${delimiter}Value 3`);
});
});
});