From 9c9a4c115a563a8a74b31d9a218b38ce9f746ca1 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 19 Dec 2017 13:08:57 +0000 Subject: [PATCH] Add further component tests --- package.json | 2 +- src/scripts/src/components/container.test.js | 28 +++++++-- .../src/components/wrapped-element.test.js | 63 +++++++++++++++++++ src/scripts/src/components/wrapped-input.js | 2 +- .../src/components/wrapped-input.test.js | 21 +++++++ src/scripts/src/components/wrapped-select.js | 2 +- .../src/components/wrapped-select.test.js | 34 ++++++++++ src/scripts/src/lib/utils.js | 2 +- 8 files changed, 146 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 83bb463..919333d 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "eslint-plugin-jsx-a11y": "^5.1.1", "eslint-plugin-react": "^7.2.1", "jasmine-core": "2.4.1", - "jsdom": "^11.3.0", + "jsdom": "^11.5.1", "mocha": "^3.4.2", "node-sass": "^3.4.2", "nodemon": "^1.9.1", diff --git a/src/scripts/src/components/container.test.js b/src/scripts/src/components/container.test.js index 2dfbcb3..a336432 100644 --- a/src/scripts/src/components/container.test.js +++ b/src/scripts/src/components/container.test.js @@ -135,6 +135,16 @@ describe('components/container', () => { expect(instance.shouldFlip(100)).to.equal(true); }); }); + + describe('position config option set to "bottom"', () => { + beforeEach(() => { + instance.config.position = 'bottom'; + }); + + it('returns false', () => { + expect(instance.shouldFlip(100)).to.equal(false); + }); + }); }); }); @@ -241,10 +251,20 @@ describe('components/container', () => { focusStub.restore(); }); - it('focuses element if isFocussed flag is set to false', () => { - instance.isFocussed = false; - instance.focus(); - expect(focusStub.callCount).to.equal(1); + describe('isFocussed flag being set to false', () => { + it('focuses element', () => { + instance.isFocussed = false; + instance.focus(); + expect(focusStub.called).to.equal(true); + }); + }); + + describe('isFocussed flag being set to true', () => { + it('does not focus element', () => { + instance.isFocussed = true; + instance.focus(); + expect(focusStub.called).to.equal(false); + }); }); }); diff --git a/src/scripts/src/components/wrapped-element.test.js b/src/scripts/src/components/wrapped-element.test.js index 41feba8..8883db7 100644 --- a/src/scripts/src/components/wrapped-element.test.js +++ b/src/scripts/src/components/wrapped-element.test.js @@ -68,4 +68,67 @@ describe('components/wrappedElement', () => { expect(instance.element.getAttribute('data-choice-orig-style')).to.equal(null); }); }); + + describe('enable', () => { + beforeEach(() => { + instance.disable(); + }); + + it('removes disabled attribute', () => { + expect(instance.element.hasAttribute('disabled')).to.equal(true); + instance.enable(); + expect(instance.element.hasAttribute('disabled')).to.equal(false); + }); + + it('sets elements disabled state to false', () => { + expect(instance.element.disabled).to.equal(true); + instance.enable(); + expect(instance.element.disabled).to.equal(false); + }); + + it('sets isDisabled flag to false', () => { + expect(instance.isDisabled).to.equal(true); + instance.enable(); + expect(instance.isDisabled).to.equal(false); + }); + }); + + describe('disable', () => { + beforeEach(() => { + instance.enable(); + }); + + it('sets disabled attribute (to blank string)', () => { + expect(instance.element.hasAttribute('disabled')).to.equal(false); + instance.disable(); + expect(instance.element.getAttribute('disabled')).to.equal(''); + }); + + it('sets elements disabled state to true', () => { + expect(instance.element.disabled).to.equal(false); + instance.disable(); + expect(instance.element.disabled).to.equal(true); + }); + + it('sets isDisabled flag to true', () => { + expect(instance.isDisabled).to.equal(false); + instance.disable(); + expect(instance.isDisabled).to.equal(true); + }); + }); + + describe('triggerEvent', () => { + it('fires event on element using passed eventType and data', (done) => { + const data = { + test: true, + }; + + instance.element.addEventListener('testEvent', ({ detail }) => { + expect(detail).to.eql(data); + done(); + }); + + instance.triggerEvent('testEvent', data); + }); + }); }); diff --git a/src/scripts/src/components/wrapped-input.js b/src/scripts/src/components/wrapped-input.js index a874a59..83250e3 100644 --- a/src/scripts/src/components/wrapped-input.js +++ b/src/scripts/src/components/wrapped-input.js @@ -26,7 +26,7 @@ export default class WrappedInput extends WrappedElement { } disable() { - super.enable(); + super.disable(); } setValue(items) { diff --git a/src/scripts/src/components/wrapped-input.test.js b/src/scripts/src/components/wrapped-input.test.js index 8c79ebd..205af58 100644 --- a/src/scripts/src/components/wrapped-input.test.js +++ b/src/scripts/src/components/wrapped-input.test.js @@ -1,4 +1,6 @@ import { expect } from 'chai'; +import { stub } from 'sinon'; +import WrappedElement from './wrapped-element'; import WrappedInput from './wrapped-input'; import { DEFAULT_CLASSNAMES, DEFAULT_CONFIG } from '../constants'; @@ -22,6 +24,25 @@ describe('components/wrappedInput', () => { instance = null; }); + describe('inherited methods', () => { + ['getElement', 'conceal', 'reveal', 'enable', 'disable'].forEach((method) => { + describe(method, () => { + beforeEach(() => { + stub(WrappedElement.prototype, method); + }); + + afterEach(() => { + WrappedElement.prototype[method].restore(); + }); + + it(`calls super.${method}`, () => { + instance[method](); + expect(WrappedElement.prototype[method].called).to.equal(true); + }); + }); + }); + }); + describe('setValue', () => { const data = [ { diff --git a/src/scripts/src/components/wrapped-select.js b/src/scripts/src/components/wrapped-select.js index a437ce4..196aa16 100644 --- a/src/scripts/src/components/wrapped-select.js +++ b/src/scripts/src/components/wrapped-select.js @@ -26,7 +26,7 @@ export default class WrappedSelect extends WrappedElement { } disable() { - super.enable(); + super.disable(); } getPlaceholderOption() { diff --git a/src/scripts/src/components/wrapped-select.test.js b/src/scripts/src/components/wrapped-select.test.js index e9fb2f1..685ac02 100644 --- a/src/scripts/src/components/wrapped-select.test.js +++ b/src/scripts/src/components/wrapped-select.test.js @@ -1,4 +1,6 @@ import { expect } from 'chai'; +import { stub } from 'sinon'; +import WrappedElement from './wrapped-element'; import WrappedSelect from './wrapped-select'; import { DEFAULT_CLASSNAMES, DEFAULT_CONFIG } from '../constants'; @@ -21,4 +23,36 @@ describe('components/wrappedSelect', () => { document.body.innerHTML = ''; instance = null; }); + + describe('inherited methods', () => { + ['getElement', 'conceal', 'reveal', 'enable', 'disable'].forEach((method) => { + describe(method, () => { + it(`calls super.${method}`, () => { + stub(WrappedElement.prototype, method); + instance[method](); + expect(WrappedElement.prototype[method].called).to.equal(true); + }); + }); + }); + }); + + // describe('getPlaceholderOption', () => { + + // }); + + // describe('getOptions', () => { + + // }); + + // describe('getOptionGroups', () => { + + // }); + + // describe('setOptions', () => { + + // }); + + // describe('appendDocFragment', () => { + + // }); }); diff --git a/src/scripts/src/lib/utils.js b/src/scripts/src/lib/utils.js index af1833d..b8d2d08 100644 --- a/src/scripts/src/lib/utils.js +++ b/src/scripts/src/lib/utils.js @@ -559,7 +559,7 @@ export const sortByScore = (a, b) => a.score - b.score; * @return {Object} Triggered event */ export const dispatchEvent = (element, type, customArgs = null) => { - const event = new CustomEvent(type, { + const event = new window.CustomEvent(type, { detail: customArgs, bubbles: true, cancelable: true,