import chai, { expect } from 'chai'; import { spy, stub } from 'sinon'; import sinonChai from 'sinon-chai'; import Choices from './choices'; import { EVENTS, ACTION_TYPES, KEY_CODES } from './constants'; import { WrappedSelect, WrappedInput } from './components/index'; import { removeItem } from './actions/items'; import templates from './templates'; import { Choice } from './interfaces/choice'; import { Group } from './interfaces/group'; import { Item } from './interfaces/item'; import { DEFAULT_CONFIG } from './defaults'; chai.use(sinonChai); describe('choices', () => { let instance; let output; let passedElement; beforeEach(() => { passedElement = document.createElement('input'); passedElement.type = 'text'; passedElement.className = 'js-choices'; document.body.appendChild(passedElement); instance = new Choices(passedElement); }); afterEach(() => { output = null; instance = null; }); describe('constructor', () => { describe('config', () => { describe('not passing config options', () => { it('uses the default config', () => { document.body.innerHTML = ` `; instance = new Choices(); expect(instance.config).to.eql(DEFAULT_CONFIG); }); }); describe('passing config options', () => { it('merges the passed config with the default config', () => { document.body.innerHTML = ` `; const config = { renderChoiceLimit: 5, }; instance = new Choices('[data-choice]', config); expect(instance.config).to.eql({ ...DEFAULT_CONFIG, ...config, }); }); describe('passing the searchEnabled config option with a value of false', () => { describe('passing a select-multiple element', () => { it('sets searchEnabled to true', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]', { searchEnabled: false, }); expect(instance.config.searchEnabled).to.equal(true); }); }); }); describe('passing the renderSelectedChoices config option with an unexpected value', () => { it('sets renderSelectedChoices to "auto"', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]', { renderSelectedChoices: 'test' as any, }); expect(instance.config.renderSelectedChoices).to.equal('auto'); }); }); }); }); describe('not passing an element', () => { it('returns a Choices instance for the first element with a "data-choice" attribute', () => { document.body.innerHTML = ` `; const inputs = document.querySelectorAll('[data-choice]'); expect(inputs.length).to.equal(3); instance = new Choices(); expect(instance.passedElement.element.id).to.equal(inputs[0].id); }); describe('when an element cannot be found in the DOM', () => { it('throws an error', () => { document.body.innerHTML = ``; expect(() => new Choices()).to.throw( TypeError, 'Expected one of the following types text|select-one|select-multiple', ); }); }); }); describe('passing an element', () => { describe('passing an element that has not been initialised with Choices', () => { beforeEach(() => { document.body.innerHTML = ` `; }); it('sets the initialised flag to true', () => { instance = new Choices('#input-1'); expect(instance.initialised).to.equal(true); }); it('intialises', () => { const initSpy = spy(); // initialise with the same element instance = new Choices('#input-1', { silent: true, callbackOnInit: initSpy, }); expect(initSpy.called).to.equal(true); }); }); describe('passing an element that has already be initialised with Choices', () => { beforeEach(() => { document.body.innerHTML = ` `; // initialise once new Choices('#input-1', { silent: true }); }); it('sets the initialised flag to true', () => { // initialise with the same element instance = new Choices('#input-1', { silent: true }); expect(instance.initialised).to.equal(true); }); it('does not reinitialise', () => { const initSpy = spy(); // initialise with the same element instance = new Choices('#input-1', { silent: true, callbackOnInit: initSpy, }); expect(initSpy.called).to.equal(false); }); }); describe(`passing an element as a DOMString`, () => { describe('passing a input element type', () => { it('sets the "passedElement" instance property as an instance of WrappedInput', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]'); expect(instance.passedElement).to.be.an.instanceOf(WrappedInput); }); }); describe('passing a select element type', () => { it('sets the "passedElement" instance property as an instance of WrappedSelect', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]'); expect(instance.passedElement).to.be.an.instanceOf(WrappedSelect); }); }); }); describe(`passing an element as a HTMLElement`, () => { describe('passing a input element type', () => { it('sets the "passedElement" instance property as an instance of WrappedInput', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]'); expect(instance.passedElement).to.be.an.instanceOf(WrappedInput); }); }); describe('passing a select element type', () => { it('sets the "passedElement" instance property as an instance of WrappedSelect', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]'); expect(instance.passedElement).to.be.an.instanceOf(WrappedSelect); }); }); }); describe('passing an invalid element type', () => { it('throws an TypeError', () => { document.body.innerHTML = `
`; expect(() => new Choices('[data-choice]')).to.throw( TypeError, 'Expected one of the following types text|select-one|select-multiple', ); }); }); }); }); describe('public methods', () => { describe('init', () => { const callbackOnInitSpy = spy(); beforeEach(() => { instance = new Choices(passedElement, { callbackOnInit: callbackOnInitSpy, silent: true, }); }); describe('when already initialised', () => { beforeEach(() => { instance.initialised = true; instance.init(); }); it("doesn't set initialise flag", () => { expect(instance.initialised).to.not.equal(false); }); }); describe('not already initialised', () => { let createTemplatesSpy; let createInputSpy; let storeSubscribeSpy; let renderSpy; let addEventListenersSpy; beforeEach(() => { createTemplatesSpy = spy(instance, '_createTemplates'); createInputSpy = spy(instance, '_createStructure'); storeSubscribeSpy = spy(instance._store, 'subscribe'); renderSpy = spy(instance, '_render'); addEventListenersSpy = spy(instance, '_addEventListeners'); instance.initialised = false; instance.init(); }); afterEach(() => { createTemplatesSpy.restore(); createInputSpy.restore(); storeSubscribeSpy.restore(); renderSpy.restore(); addEventListenersSpy.restore(); }); it('sets initialise flag', () => { expect(instance.initialised).to.equal(true); }); it('creates templates', () => { expect(createTemplatesSpy.called).to.equal(true); }); it('creates input', () => { expect(createInputSpy.called).to.equal(true); }); it('subscribes to store with render method', () => { expect(storeSubscribeSpy.called).to.equal(true); expect(storeSubscribeSpy.lastCall.args[0]).to.equal(instance._render); }); it('fires initial render', () => { expect(renderSpy.called).to.equal(true); }); it('adds event listeners', () => { expect(addEventListenersSpy.called).to.equal(true); }); it('fires callback', () => { expect(callbackOnInitSpy.called).to.equal(true); }); }); }); describe('destroy', () => { beforeEach(() => { passedElement = document.createElement('input'); passedElement.type = 'text'; passedElement.className = 'js-choices'; document.body.appendChild(passedElement); instance = new Choices(passedElement); }); describe('not already initialised', () => { beforeEach(() => { instance.initialised = false; instance.destroy(); }); it("doesn't set initialise flag", () => { expect(instance.initialised).to.not.equal(true); }); }); describe('when already initialised', () => { let removeEventListenersSpy; let passedElementRevealSpy; let containerOuterUnwrapSpy; let clearStoreSpy; beforeEach(() => { removeEventListenersSpy = spy(instance, '_removeEventListeners'); passedElementRevealSpy = spy(instance.passedElement, 'reveal'); containerOuterUnwrapSpy = spy(instance.containerOuter, 'unwrap'); clearStoreSpy = spy(instance, 'clearStore'); instance.initialised = true; instance.destroy(); }); afterEach(() => { removeEventListenersSpy.restore(); passedElementRevealSpy.restore(); containerOuterUnwrapSpy.restore(); clearStoreSpy.restore(); }); it('removes event listeners', () => { expect(removeEventListenersSpy.called).to.equal(true); }); it('reveals passed element', () => { expect(passedElementRevealSpy.called).to.equal(true); }); it('reverts outer container', () => { expect(containerOuterUnwrapSpy.called).to.equal(true); expect(containerOuterUnwrapSpy.lastCall.args[0]).to.equal( instance.passedElement.element, ); }); it('clears store', () => { expect(clearStoreSpy.called).to.equal(true); }); it('restes templates config', () => { expect(instance._templates).to.deep.equal(templates); }); it('resets initialise flag', () => { expect(instance.initialised).to.equal(false); }); }); }); describe('enable', () => { let passedElementEnableSpy; let addEventListenersSpy; let containerOuterEnableSpy; let inputEnableSpy; beforeEach(() => { addEventListenersSpy = spy(instance, '_addEventListeners'); passedElementEnableSpy = spy(instance.passedElement, 'enable'); containerOuterEnableSpy = spy(instance.containerOuter, 'enable'); inputEnableSpy = spy(instance.input, 'enable'); }); afterEach(() => { addEventListenersSpy.restore(); passedElementEnableSpy.restore(); containerOuterEnableSpy.restore(); inputEnableSpy.restore(); }); describe('when already enabled', () => { beforeEach(() => { instance.passedElement.isDisabled = false; instance.containerOuter.isDisabled = false; output = instance.enable(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(passedElementEnableSpy.called).to.equal(false); expect(addEventListenersSpy.called).to.equal(false); expect(inputEnableSpy.called).to.equal(false); expect(containerOuterEnableSpy.called).to.equal(false); }); }); describe('when not already enabled', () => { beforeEach(() => { instance.passedElement.isDisabled = true; instance.containerOuter.isDisabled = true; instance.enable(); }); it('adds event listeners', () => { expect(addEventListenersSpy.called).to.equal(true); }); it('enables input', () => { expect(inputEnableSpy.called).to.equal(true); }); it('enables containerOuter', () => { expect(containerOuterEnableSpy.called).to.equal(true); }); }); }); describe('disable', () => { let removeEventListenersSpy; let passedElementDisableSpy; let containerOuterDisableSpy; let inputDisableSpy; beforeEach(() => { removeEventListenersSpy = spy(instance, '_removeEventListeners'); passedElementDisableSpy = spy(instance.passedElement, 'disable'); containerOuterDisableSpy = spy(instance.containerOuter, 'disable'); inputDisableSpy = spy(instance.input, 'disable'); }); afterEach(() => { removeEventListenersSpy.restore(); passedElementDisableSpy.restore(); containerOuterDisableSpy.restore(); inputDisableSpy.restore(); }); describe('when already disabled', () => { beforeEach(() => { instance.passedElement.isDisabled = true; instance.containerOuter.isDisabled = true; output = instance.disable(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(removeEventListenersSpy.called).to.equal(false); expect(passedElementDisableSpy.called).to.equal(false); expect(containerOuterDisableSpy.called).to.equal(false); expect(inputDisableSpy.called).to.equal(false); }); }); describe('when not already disabled', () => { beforeEach(() => { instance.passedElement.isDisabled = false; instance.containerOuter.isDisabled = false; output = instance.disable(); }); it('removes event listeners', () => { expect(removeEventListenersSpy.called).to.equal(true); }); it('disables input', () => { expect(inputDisableSpy.called).to.equal(true); }); it('enables containerOuter', () => { expect(containerOuterDisableSpy.called).to.equal(true); }); }); }); describe('showDropdown', () => { let containerOuterOpenSpy; let dropdownShowSpy; let inputFocusSpy; let passedElementTriggerEventStub; beforeEach(() => { containerOuterOpenSpy = spy(instance.containerOuter, 'open'); dropdownShowSpy = spy(instance.dropdown, 'show'); inputFocusSpy = spy(instance.input, 'focus'); passedElementTriggerEventStub = stub(); instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { containerOuterOpenSpy.restore(); dropdownShowSpy.restore(); inputFocusSpy.restore(); instance.passedElement.triggerEvent.reset(); }); describe('dropdown active', () => { beforeEach(() => { instance.dropdown.isActive = true; output = instance.showDropdown(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(containerOuterOpenSpy.called).to.equal(false); expect(dropdownShowSpy.called).to.equal(false); expect(inputFocusSpy.called).to.equal(false); expect(passedElementTriggerEventStub.called).to.equal(false); }); }); describe('dropdown inactive', () => { beforeEach(() => { instance.dropdown.isActive = false; output = instance.showDropdown(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('opens containerOuter', (done) => { requestAnimationFrame(() => { expect(containerOuterOpenSpy.called).to.equal(true); done(); }); }); it('shows dropdown with blurInput flag', (done) => { requestAnimationFrame(() => { expect(dropdownShowSpy.called).to.equal(true); done(); }); }); it('triggers event on passedElement', (done) => { requestAnimationFrame(() => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.eql( EVENTS.showDropdown, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({}); done(); }); }); describe('passing true focusInput flag with canSearch set to true', () => { beforeEach(() => { instance.dropdown.isActive = false; instance._canSearch = true; output = instance.showDropdown(true); }); it('focuses input', (done) => { requestAnimationFrame(() => { expect(inputFocusSpy.called).to.equal(true); done(); }); }); }); }); }); describe('hideDropdown', () => { let containerOuterCloseSpy; let dropdownHideSpy; let inputBlurSpy; let inputRemoveActiveDescendantSpy; let passedElementTriggerEventStub; beforeEach(() => { containerOuterCloseSpy = spy(instance.containerOuter, 'close'); dropdownHideSpy = spy(instance.dropdown, 'hide'); inputBlurSpy = spy(instance.input, 'blur'); inputRemoveActiveDescendantSpy = spy( instance.input, 'removeActiveDescendant', ); passedElementTriggerEventStub = stub(); instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { containerOuterCloseSpy.restore(); dropdownHideSpy.restore(); inputBlurSpy.restore(); inputRemoveActiveDescendantSpy.restore(); instance.passedElement.triggerEvent.reset(); }); describe('dropdown inactive', () => { beforeEach(() => { instance.dropdown.isActive = false; output = instance.hideDropdown(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(containerOuterCloseSpy.called).to.equal(false); expect(dropdownHideSpy.called).to.equal(false); expect(inputBlurSpy.called).to.equal(false); expect(passedElementTriggerEventStub.called).to.equal(false); }); }); describe('dropdown active', () => { beforeEach(() => { instance.dropdown.isActive = true; output = instance.hideDropdown(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('closes containerOuter', (done) => { requestAnimationFrame(() => { expect(containerOuterCloseSpy.called).to.equal(true); done(); }); }); it('hides dropdown with blurInput flag', (done) => { requestAnimationFrame(() => { expect(dropdownHideSpy.called).to.equal(true); done(); }); }); it('triggers event on passedElement', (done) => { requestAnimationFrame(() => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.eql( EVENTS.hideDropdown, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({}); done(); }); }); describe('passing true blurInput flag with canSearch set to true', () => { beforeEach(() => { instance.dropdown.isActive = true; instance._canSearch = true; output = instance.hideDropdown(true); }); it('removes active descendants', (done) => { requestAnimationFrame(() => { expect(inputRemoveActiveDescendantSpy.called).to.equal(true); done(); }); }); it('blurs input', (done) => { requestAnimationFrame(() => { expect(inputBlurSpy.called).to.equal(true); done(); }); }); }); }); }); describe('highlightItem', () => { let passedElementTriggerEventStub; let storeDispatchSpy; let storeGetGroupByIdStub; const groupIdValue = 'Test'; beforeEach(() => { passedElementTriggerEventStub = stub(); storeGetGroupByIdStub = stub().returns({ value: groupIdValue, }); storeDispatchSpy = spy(instance._store, 'dispatch'); instance._store.getGroupById = storeGetGroupByIdStub; instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { storeDispatchSpy.restore(); instance._store.getGroupById.reset(); instance.passedElement.triggerEvent.reset(); }); describe('no item passed', () => { beforeEach(() => { output = instance.highlightItem(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(passedElementTriggerEventStub.called).to.equal(false); expect(storeDispatchSpy.called).to.equal(false); expect(storeGetGroupByIdStub.called).to.equal(false); }); }); describe('item passed', () => { const item: Item = { id: 1234, value: 'Test', label: 'Test', }; describe('passing truthy second paremeter', () => { beforeEach(() => { output = instance.highlightItem(item, true); }); it('returns this', () => { expect(output).to.eql(instance); }); it('dispatches highlightItem action with correct arguments', () => { expect(storeDispatchSpy.called).to.equal(true); expect(storeDispatchSpy.lastCall.args[0]).to.eql({ type: ACTION_TYPES.HIGHLIGHT_ITEM, id: item.id, highlighted: true, }); }); describe('item with negative groupId', () => { beforeEach(() => { item.groupId = -1; output = instance.highlightItem(item); }); it('triggers event with null groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal( EVENTS.highlightItem, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({ id: item.id, value: item.value, label: item.label, groupValue: null, }); }); }); describe('item without groupId', () => { beforeEach(() => { item.groupId = 1; output = instance.highlightItem(item); }); it('triggers event with groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal( EVENTS.highlightItem, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({ id: item.id, value: item.value, label: item.label, groupValue: groupIdValue, }); }); }); }); describe('passing falsey second paremeter', () => { beforeEach(() => { output = instance.highlightItem(item, false); }); it("doesn't trigger event", () => { expect(passedElementTriggerEventStub.called).to.equal(false); }); it('returns this', () => { expect(output).to.eql(instance); }); }); }); }); describe('unhighlightItem', () => { let passedElementTriggerEventStub; let storeDispatchSpy; let storeGetGroupByIdStub; const groupIdValue = 'Test'; beforeEach(() => { passedElementTriggerEventStub = stub(); storeGetGroupByIdStub = stub().returns({ value: groupIdValue, }); storeDispatchSpy = spy(instance._store, 'dispatch'); instance._store.getGroupById = storeGetGroupByIdStub; instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { storeDispatchSpy.restore(); instance._store.getGroupById.reset(); instance.passedElement.triggerEvent.reset(); }); describe('no item passed', () => { beforeEach(() => { output = instance.unhighlightItem(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('returns early', () => { expect(passedElementTriggerEventStub.called).to.equal(false); expect(storeDispatchSpy.called).to.equal(false); expect(storeGetGroupByIdStub.called).to.equal(false); }); }); describe('item passed', () => { const item: Item = { id: 1234, value: 'Test', label: 'Test', }; describe('passing truthy second paremeter', () => { beforeEach(() => { output = instance.unhighlightItem(item, true); }); it('returns this', () => { expect(output).to.eql(instance); }); it('dispatches highlightItem action with correct arguments', () => { expect(storeDispatchSpy.called).to.equal(true); expect(storeDispatchSpy.lastCall.args[0]).to.eql({ type: ACTION_TYPES.HIGHLIGHT_ITEM, id: item.id, highlighted: false, }); }); describe('item with negative groupId', () => { beforeEach(() => { item.groupId = -1; output = instance.unhighlightItem(item); }); it('triggers event with null groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal( EVENTS.highlightItem, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({ id: item.id, value: item.value, label: item.label, groupValue: null, }); }); }); describe('item without groupId', () => { beforeEach(() => { item.groupId = 1; output = instance.highlightItem(item); }); it('triggers event with groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal( EVENTS.highlightItem, ); expect(passedElementTriggerEventStub.lastCall.args[1]).to.eql({ id: item.id, value: item.value, label: item.label, groupValue: groupIdValue, }); }); }); }); describe('passing falsey second paremeter', () => { beforeEach(() => { output = instance.highlightItem(item, false); }); it("doesn't trigger event", () => { expect(passedElementTriggerEventStub.called).to.equal(false); }); it('returns this', () => { expect(output).to.eql(instance); }); }); }); }); describe('highlightAll', () => { let storeGetItemsStub; let highlightItemStub; const items = [ { id: 1, value: 'Test 1', }, { id: 2, value: 'Test 2', }, ]; beforeEach(() => { storeGetItemsStub = stub(instance._store, 'items').get(() => items); highlightItemStub = stub(); instance.highlightItem = highlightItemStub; output = instance.highlightAll(); }); afterEach(() => { highlightItemStub.reset(); storeGetItemsStub.reset(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('highlights each item in store', () => { expect(highlightItemStub.callCount).to.equal(items.length); expect(highlightItemStub.firstCall.args[0]).to.equal(items[0]); expect(highlightItemStub.lastCall.args[0]).to.equal(items[1]); }); }); describe('unhighlightAll', () => { let storeGetItemsStub; let unhighlightItemStub; const items = [ { id: 1, value: 'Test 1', }, { id: 2, value: 'Test 2', }, ]; beforeEach(() => { storeGetItemsStub = stub(instance._store, 'items').get(() => items); unhighlightItemStub = stub(); instance.unhighlightItem = unhighlightItemStub; output = instance.unhighlightAll(); }); afterEach(() => { instance.unhighlightItem.reset(); storeGetItemsStub.reset(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('unhighlights each item in store', () => { expect(unhighlightItemStub.callCount).to.equal(items.length); expect(unhighlightItemStub.firstCall.args[0]).to.equal(items[0]); expect(unhighlightItemStub.lastCall.args[0]).to.equal(items[1]); }); }); describe('clearChoices', () => { let storeDispatchStub; beforeEach(() => { storeDispatchStub = stub(); instance._store.dispatch = storeDispatchStub; output = instance.clearChoices(); }); afterEach(() => { instance._store.dispatch.reset(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('dispatches clearChoices action', () => { expect(storeDispatchStub.lastCall.args[0]).to.eql({ type: ACTION_TYPES.CLEAR_CHOICES, }); }); }); describe('clearStore', () => { let storeDispatchStub; beforeEach(() => { storeDispatchStub = stub(); instance._store.dispatch = storeDispatchStub; output = instance.clearStore(); }); afterEach(() => { instance._store.dispatch.reset(); }); it('returns this', () => { expect(output).to.eql(instance); }); it('dispatches clearAll action', () => { expect(storeDispatchStub.lastCall.args[0]).to.eql({ type: ACTION_TYPES.CLEAR_ALL, }); }); }); describe('clearInput', () => { let inputClearSpy; let storeDispatchStub; beforeEach(() => { inputClearSpy = spy(instance.input, 'clear'); storeDispatchStub = stub(); instance._store.dispatch = storeDispatchStub; output = instance.clearInput(); }); afterEach(() => { inputClearSpy.restore(); instance._store.dispatch.reset(); }); it('returns this', () => { expect(output).to.eql(instance); }); describe('text element', () => { beforeEach(() => { instance._isSelectOneElement = false; instance._isTextElement = false; output = instance.clearInput(); }); it('clears input with correct arguments', () => { expect(inputClearSpy.called).to.equal(true); expect(inputClearSpy.lastCall.args[0]).to.equal(true); }); }); describe('select element with search enabled', () => { beforeEach(() => { instance._isSelectOneElement = true; instance._isTextElement = false; instance.config.searchEnabled = true; output = instance.clearInput(); }); it('clears input with correct arguments', () => { expect(inputClearSpy.called).to.equal(true); expect(inputClearSpy.lastCall.args[0]).to.equal(false); }); it('resets search flag', () => { expect(instance._isSearching).to.equal(false); }); it('dispatches activateChoices action', () => { expect(storeDispatchStub.called).to.equal(true); expect(storeDispatchStub.lastCall.args[0]).to.eql({ type: ACTION_TYPES.ACTIVATE_CHOICES, active: true, }); }); }); }); describe('setChoices with callback/Promise', () => { describe('not initialised', () => { beforeEach(() => { instance.initialised = false; }); it('should throw', () => { expect(() => instance.setChoices(null)).Throw(ReferenceError); }); }); describe('text element', () => { beforeEach(() => { instance._isSelectElement = false; }); it('should throw', () => { expect(() => instance.setChoices(null)).Throw(TypeError); }); }); describe('passing invalid function', () => { beforeEach(() => { instance._isSelectElement = true; }); it('should throw on non function', () => { expect(() => instance.setChoices(null)).Throw(TypeError, /Promise/i); }); it(`should throw on function that doesn't return promise`, () => { expect(() => instance.setChoices(() => 'boo')).to.throw( TypeError, /promise/i, ); }); }); describe('select element', () => { it('fetches and sets choices', async () => { document.body.innerHTML = '