Choices/src/scripts/components/list.test.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-10-10 13:56:36 +02:00
import { expect } from 'chai';
import List from './list';
2017-10-29 19:56:24 +01:00
describe('components/list', () => {
2017-10-10 13:56:36 +02:00
let instance;
let choicesElement;
beforeEach(() => {
2017-10-11 10:39:31 +02:00
choicesElement = document.createElement('div');
2018-05-21 18:01:03 +02:00
instance = new List({
element: choicesElement,
});
2017-10-10 13:56:36 +02:00
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
2017-12-10 19:00:57 +01:00
describe('constructor', () => {
it('assigns choices element to class', () => {
expect(instance.element).to.eql(choicesElement);
});
2017-10-10 13:56:36 +02:00
});
2017-10-11 10:39:31 +02:00
describe('clear', () => {
it("clears element's inner HTML", () => {
2017-10-11 10:39:31 +02:00
const innerHTML = 'test';
instance.element.innerHTML = innerHTML;
expect(instance.element.innerHTML).to.equal(innerHTML);
instance.clear();
expect(instance.element.innerHTML).to.equal('');
});
});
describe('append', () => {
it('appends passed node to element', () => {
const elementToAppend = document.createElement('span');
const childClass = 'test-element';
elementToAppend.classList.add(childClass);
expect(instance.element.querySelector(`.${childClass}`)).to.equal(null);
instance.append(elementToAppend);
expect(instance.element.querySelector(`.${childClass}`)).to.equal(
elementToAppend,
);
2017-10-11 10:39:31 +02:00
});
});
describe('getChild', () => {
let childElement;
const childClass = 'test-element';
beforeEach(() => {
childElement = document.createElement('span');
childElement.classList.add(childClass);
instance.element.appendChild(childElement);
});
it('returns child element', () => {
const expectedResponse = childElement;
const actualResponse = instance.getChild(`.${childClass}`);
expect(expectedResponse).to.eql(actualResponse);
});
});
2017-10-10 13:56:36 +02:00
});