Choices/src/scripts/components/dropdown.test.ts
Josh Johnson 68313da412
Convert to typescript (#795)
* Typescript config setup

* Add type annotations to components

* Further type additions

* And more...

* Add types to actions

* Add types to templates

* Further type checks

* Further type additons

* Install fuse latest

* Housekeeping

* Remove old type definitions

* Fix remaning type issues

* Fix some failing tests

* Remove types workflow

* Fix failing unit tests

* Resolve back space event regression

* Convert cypress files to .ts

* Fix eslint issues

* Remove cachebusting urls

* Resolve delete button bug

* Resolve regression bugs

* Fix lint script

* Fix lint workflow

* Pass args instead of object to keyboard handlers

* Flatten misc reducer

* Resolve keyboad action test failures

* Use Pick instead of Partial

* Use interfaces in action tests

* Update firefox image

* Incorporate #791

* Incorporate #788
2019-12-23 18:22:54 +00:00

144 lines
3.4 KiB
TypeScript

import { expect } from 'chai';
import sinon from 'sinon';
import Dropdown from './dropdown';
import { DEFAULT_CLASSNAMES } from '../constants';
describe('components/dropdown', () => {
let instance;
let choicesElement;
beforeEach(() => {
choicesElement = document.createElement('div');
document.body.appendChild(choicesElement);
instance = new Dropdown({
element: choicesElement,
type: 'text',
classNames: DEFAULT_CLASSNAMES,
});
});
afterEach(() => {
document.body.innerHTML = '';
instance = null;
});
describe('constructor', () => {
it('assigns choices element to instance', () => {
expect(instance.element).to.eql(choicesElement);
});
it('assigns classnames to instance', () => {
expect(instance.classNames).to.eql(DEFAULT_CLASSNAMES);
});
});
describe('distanceFromTopWindow', () => {
let top;
let dimensions;
let getBoundingClientRectStub;
beforeEach(() => {
top = 100;
dimensions = {
bottom: 121,
height: 0,
left: 0,
right: 0,
top,
width: 0,
};
getBoundingClientRectStub = sinon
.stub(instance.element, 'getBoundingClientRect')
.returns(dimensions);
});
afterEach(() => {
getBoundingClientRectStub.restore();
});
it('determines how far the top of our element is from the top of the viewport', () => {
const expectedResponse = dimensions.bottom;
const actualResponse = instance.distanceFromTopWindow;
expect(actualResponse).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);
});
it('returns instance', () => {
expect(actualResponse).to.eql(instance);
});
});
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);
});
it('returns instance', () => {
expect(actualResponse).to.eql(instance);
});
});
});