Add list unit tests

This commit is contained in:
Josh Johnson 2017-10-11 09:39:31 +01:00
parent d211cf9507
commit f9303d86b0
2 changed files with 50 additions and 3 deletions

View file

@ -18,10 +18,9 @@ export default class List {
/**
* Scroll to passed position on Y axis
*/
scrollTo(scrollPos) {
scrollTo(scrollPos = 0) {
this.element.scrollTop = scrollPos;
}
/**
* Append node to element
*/

View file

@ -14,7 +14,7 @@ describe('List', () => {
...DEFAULT_CONFIG,
},
};
choicesElement = document.createElement('select');
choicesElement = document.createElement('div');
instance = new List(choicesInstance, choicesElement, DEFAULT_CLASSNAMES);
});
@ -29,4 +29,52 @@ describe('List', () => {
it('assigns classnames to class', () => {
expect(instance.classNames).to.eql(DEFAULT_CLASSNAMES);
});
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('scrollTo', () => {
it('scrolls element to passed position', () => {
const scrollPosition = 20;
expect(instance.element.scrollTop).to.equal(0);
instance.scrollTo(scrollPosition);
expect(instance.element.scrollTop).to.equal(scrollPosition);
});
});
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);
});
});
});