Choices/src/scripts/components/list.test.js
Josh Johnson 0e44a916e3
Fix constructor (#693)
* breaking test

* Remove ablity to pass multiple elements + tests

* Update readme

* Update README.md

* 🔖 Version 8.0.0

* Remove type definition hack

* Update coverage command

* Add some missing list tests

* Remove .only

* Update demo page to loop over elements

* Update constructor to set initialised flag if already active

* Make templates private

* Throw type error once if element is invalid

* Fix list children bug

* Re-add generic examples to index.html

* Housekeeping

* Use typeof instead of isType where applicable

* Remove isElement

* Add test for isIE11
2019-10-29 21:19:56 +00:00

97 lines
2.7 KiB
JavaScript

import { expect } from 'chai';
import List from './list';
describe('components/list', () => {
let instance;
let choicesElement;
beforeEach(() => {
choicesElement = document.createElement('div');
instance = new List({
element: choicesElement,
});
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
describe('constructor', () => {
it('assigns choices element to class', () => {
expect(instance.element).to.eql(choicesElement);
});
it('sets the height of the element', () => {
expect(instance.height).to.eql(choicesElement.scrollTop);
});
});
describe('clear', () => {
it("clears element's inner HTML", () => {
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,
);
});
});
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);
});
});
describe('hasChildren', () => {
describe('when list has children', () => {
it('returns true', () => {
const childElement = document.createElement('span');
instance.element.appendChild(childElement);
const response = instance.hasChildren();
expect(response).to.equal(true);
});
});
describe('when list does not have children', () => {
it('returns false', () => {
instance.element.innerHTML = '';
const response = instance.hasChildren();
expect(response).to.equal(false);
});
});
});
describe('scrollToTop', () => {
it("sets the position's scroll position to 0", () => {
instance.element.scrollTop = 10;
instance.scrollToTop();
expect(instance.element.scrollTop).to.equal(0);
});
});
});