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

154 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-10-10 13:56:36 +02:00
import { expect } from 'chai';
2017-10-11 10:19:13 +02:00
import sinon from 'sinon';
2017-10-10 13:56:36 +02:00
import Dropdown from './dropdown';
2018-05-21 18:01:03 +02:00
import { DEFAULT_CLASSNAMES } from '../constants';
2017-10-10 13:56:36 +02:00
2017-10-29 19:56:24 +01:00
describe('components/dropdown', () => {
2017-10-10 13:56:36 +02:00
let instance;
let choicesElement;
beforeEach(() => {
2017-10-11 10:19:13 +02:00
choicesElement = document.createElement('div');
document.body.appendChild(choicesElement);
2018-05-21 18:01:03 +02:00
instance = new Dropdown({
element: choicesElement,
type: 'text',
classNames: DEFAULT_CLASSNAMES,
});
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 instance', () => {
expect(instance.element).to.eql(choicesElement);
});
2017-10-10 13:56:36 +02:00
2017-12-10 19:00:57 +01:00
it('assigns classnames to instance', () => {
expect(instance.classNames).to.eql(DEFAULT_CLASSNAMES);
});
2017-10-10 13:56:36 +02:00
});
2017-10-11 10:19:13 +02:00
2018-04-24 13:54:45 +02:00
describe('distanceFromTopWindow', () => {
2017-10-11 10:19:13 +02:00
let top;
let offset;
let dimensions;
let getBoundingClientRectStub;
beforeEach(() => {
top = 100;
offset = 50;
dimensions = {
bottom: 0,
height: 0,
left: 0,
right: 0,
top,
width: 0,
};
getBoundingClientRectStub = sinon.stub(instance.element, 'getBoundingClientRect').returns(dimensions);
window.pageYOffset = 50;
});
afterEach(() => {
getBoundingClientRectStub.restore();
});
it('determines how far the top of our element is from the top of the window', () => {
const expectedResponse = top + offset;
2018-04-24 13:54:45 +02:00
const actualResponse = instance.distanceFromTopWindow();
2017-10-11 10:19:13 +02:00
expect(actualResponse).to.equal(expectedResponse);
});
it('assigns dimensions to instance', () => {
2018-04-24 13:54:45 +02:00
instance.distanceFromTopWindow();
2017-10-11 10:19:13 +02:00
const expectedResponse = dimensions;
expect(instance.dimensions).to.equal(expectedResponse);
});
it('assigns posisiton to instance', () => {
2018-04-24 13:54:45 +02:00
instance.distanceFromTopWindow();
2017-10-11 10:19:13 +02:00
const expectedResponse = top + offset;
expect(instance.position).to.equal(expectedResponse);
});
});
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('show', () => {
let actualResponse;
beforeEach(() => {
actualResponse = instance.show();
});
afterEach(() => {
instance.hide();
});
it('adds active class', () => {
expect(instance.element.classList.contains(DEFAULT_CLASSNAMES.activeState)).to.equal(true);
});
it('sets expanded attribute', () => {
expect(instance.element.getAttribute('aria-expanded')).to.equal('true');
});
it('sets isActive instance flag', () => {
expect(instance.isActive).to.equal(true);
});
2018-05-21 18:01:03 +02:00
it('returns instance', () => {
expect(actualResponse).to.eql(instance);
2017-10-11 10:19:13 +02:00
});
});
describe('hide', () => {
let actualResponse;
beforeEach(() => {
actualResponse = instance.hide();
});
afterEach(() => {
instance.show();
});
it('adds active class', () => {
expect(instance.element.classList.contains(DEFAULT_CLASSNAMES.activeState)).to.equal(false);
});
it('sets expanded attribute', () => {
expect(instance.element.getAttribute('aria-expanded')).to.equal('false');
});
it('sets isActive instance flag', () => {
expect(instance.isActive).to.equal(false);
});
2018-05-21 18:01:03 +02:00
it('returns instance', () => {
expect(actualResponse).to.eql(instance);
2017-10-11 10:19:13 +02:00
});
});
2017-10-10 13:56:36 +02:00
});