import { expect } from 'chai'; import { spy, stub } from 'sinon'; import sinonChai from 'sinon-chai'; import Choices, { DEFAULT_CONFIG, ActionType, EventType, KeyCodeMap, InputChoice, InputGroup } from '../../src'; import { WrappedSelect, WrappedInput } from '../../src/scripts/components/index'; import { removeItem } from '../../src/scripts/actions/items'; import templates from '../../src/scripts/templates'; import { ChoiceFull } from '../../src/scripts/interfaces/choice-full'; import { GroupFull } from '../../src/scripts/interfaces/group-full'; import { SearchByFuse } from '../../src/scripts/search/fuse'; import { SearchByPrefixFilter } from '../../src/scripts/search/prefix-filter'; 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, { allowHTML: true }); }); 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.deep.equal({ ...DEFAULT_CONFIG, searchEnabled: false, closeDropdownOnSelect: true, }); }); }); describe('passing config options', () => { it('merges the passed config with the default config', () => { document.body.innerHTML = ` `; const config = { allowHTML: true, renderChoiceLimit: 5, }; instance = new Choices('[data-choice]', config); expect(instance.config).to.deep.equal({ ...DEFAULT_CONFIG, searchEnabled: false, closeDropdownOnSelect: true, ...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]', { allowHTML: true, searchEnabled: true, }); expect(instance.config.searchEnabled).to.equal(true); }); it('sets searchEnabled to false', () => { document.body.innerHTML = ` `; instance = new Choices('[data-choice]', { allowHTML: true, 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]', { allowHTML: true, 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(undefined, { allowHTML: true }); 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(undefined, { allowHTML: true })).to.throw( TypeError, 'Selector [data-choice] failed to find an element', ); }); }); describe('when an element is not of the expected type', () => { it('throws an error', () => { document.body.innerHTML = `
`; expect(() => new Choices(undefined, { allowHTML: true })).to.throw( TypeError, 'Selector [data-choice] failed to find an element', ); }); }); }); 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', { allowHTML: true }); expect(instance.initialised).to.equal(true); }); it('intialises', () => { const initSpy = spy(); // initialise with the same element instance = new Choices('#input-1', { allowHTML: true, 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', { allowHTML: true, silent: true }); }); it('sets the initialised flag to true', () => { // initialise with the same element instance = new Choices('#input-1', { allowHTML: true, 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', { allowHTML: true, 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]', { allowHTML: true }); 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]', { allowHTML: true }); 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]', { allowHTML: true }); 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]', { allowHTML: true }); 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]', { allowHTML: true })).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, { allowHTML: true, 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.initialisedOK = undefined; 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('does not fire initial render with no items or choices', () => { expect(renderSpy.called).to.equal(false); }); 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, { allowHTML: true }); }); describe('not already initialised', () => { beforeEach(() => { instance.initialised = false; instance.initialisedOK = undefined; 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.deep.equal(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.deep.equal(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.deep.equal(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.deep.equal(instance); }); it('opens containerOuter', () => new Promise((done) => { requestAnimationFrame(() => { expect(containerOuterOpenSpy.called).to.equal(true); done(true); }); })); it('shows dropdown with blurInput flag', () => new Promise((done) => { requestAnimationFrame(() => { expect(dropdownShowSpy.called).to.equal(true); done(true); }); })); it('triggers event on passedElement', () => new Promise((done) => { requestAnimationFrame(() => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.deep.equal(EventType.showDropdown); expect(passedElementTriggerEventStub.lastCall.args[1]).to.undefined; done(true); }); })); 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', () => new Promise((done) => { requestAnimationFrame(() => { expect(inputFocusSpy.called).to.equal(true); done(true); }); })); }); }); }); 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.deep.equal(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.deep.equal(instance); }); it('closes containerOuter', () => new Promise((done) => { requestAnimationFrame(() => { expect(containerOuterCloseSpy.called).to.equal(true); done(true); }); })); it('hides dropdown with blurInput flag', () => new Promise((done) => { requestAnimationFrame(() => { expect(dropdownHideSpy.called).to.equal(true); done(true); }); })); it('triggers event on passedElement', () => new Promise((done) => { requestAnimationFrame(() => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.deep.equal(EventType.hideDropdown); expect(passedElementTriggerEventStub.lastCall.args[1]).to.undefined; done(true); }); })); 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', () => new Promise((done) => { requestAnimationFrame(() => { expect(inputRemoveActiveDescendantSpy.called).to.equal(true); done(true); }); })); it('blurs input', () => new Promise((done) => { requestAnimationFrame(() => { expect(inputBlurSpy.called).to.equal(true); done(true); }); })); }); }); }); describe('highlightItem', () => { let passedElementTriggerEventStub; let storeDispatchSpy; let storeGetGroupByIdStub; let choicesStub; const groupIdValue = 'Test'; const item: ChoiceFull = { groupId: 0, highlighted: false, active: false, disabled: false, placeholder: false, selected: false, id: 1234, value: 'Test', label: 'Test', score: 0, rank: 0, }; beforeEach(() => { choicesStub = stub(instance._store, 'choices').get(() => [item]); passedElementTriggerEventStub = stub(); storeGetGroupByIdStub = stub().returns({ id: 4321, label: groupIdValue, }); storeDispatchSpy = spy(instance._store, 'dispatch'); instance._store.getGroupById = storeGetGroupByIdStub; instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { choicesStub.reset(); storeDispatchSpy.restore(); instance._store.getGroupById.reset(); instance.passedElement.triggerEvent.reset(); }); describe('no item passed', () => { beforeEach(() => { output = instance.highlightItem(); }); it('returns this', () => { expect(output).to.deep.equal(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', () => { describe('passing truthy second paremeter', () => { beforeEach(() => { output = instance.highlightItem(item, true); }); it('returns this', () => { expect(output).to.deep.equal(instance); }); it('dispatches highlightItem action with correct arguments', () => { expect(storeDispatchSpy.called).to.equal(true); expect(storeDispatchSpy.lastCall.args[0]).to.deep.equal({ type: ActionType.HIGHLIGHT_ITEM, item, 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(EventType.highlightItem); expect(passedElementTriggerEventStub.lastCall.args[1]).to.contains({ id: item.id, value: item.value, label: item.label, groupValue: undefined, }); }); }); describe('item without groupId', () => { beforeEach(() => { item.groupId = 4321; output = instance.highlightItem(item); }); it('triggers event with groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EventType.highlightItem); expect(passedElementTriggerEventStub.lastCall.args[1]).to.contains({ 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.deep.equal(instance); }); }); }); }); describe('unhighlightItem', () => { let choicesStub; let passedElementTriggerEventStub; let storeDispatchSpy; let storeGetGroupByIdStub; const groupIdValue = 'Test'; const item: ChoiceFull = { groupId: 0, highlighted: true, active: false, disabled: false, placeholder: false, selected: false, id: 1234, value: 'Test', label: 'Test', score: 0, rank: 0, }; beforeEach(() => { choicesStub = stub(instance._store, 'choices').get(() => [item]); passedElementTriggerEventStub = stub(); storeGetGroupByIdStub = stub().returns({ id: 4321, label: groupIdValue, }); storeDispatchSpy = spy(instance._store, 'dispatch'); instance._store.getGroupById = storeGetGroupByIdStub; instance.passedElement.triggerEvent = passedElementTriggerEventStub; }); afterEach(() => { choicesStub.reset(); storeDispatchSpy.restore(); instance._store.getGroupById.reset(); instance.passedElement.triggerEvent.reset(); }); describe('no item passed', () => { beforeEach(() => { output = instance.unhighlightItem(); }); it('returns this', () => { expect(output).to.deep.equal(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', () => { describe('passing truthy second paremeter', () => { beforeEach(() => { output = instance.unhighlightItem(item, true); }); it('returns this', () => { expect(output).to.deep.equal(instance); }); it('dispatches highlightItem action with correct arguments', () => { expect(storeDispatchSpy.called).to.equal(true); expect(storeDispatchSpy.lastCall.args[0]).to.deep.contains({ type: ActionType.HIGHLIGHT_ITEM, item, 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(EventType.highlightItem); expect(passedElementTriggerEventStub.lastCall.args[1]).to.contains({ value: item.value, label: item.label, groupValue: undefined, }); }); }); describe('item without groupId', () => { beforeEach(() => { item.groupId = 4321; output = instance.unhighlightItem(item); }); it('triggers event with groupValue', () => { expect(passedElementTriggerEventStub.called).to.equal(true); expect(passedElementTriggerEventStub.lastCall.args[0]).to.equal(EventType.highlightItem); expect(passedElementTriggerEventStub.lastCall.args[1]).to.contains({ value: item.value, label: item.label, groupValue: groupIdValue, }); }); }); describe('passing falsey second paremeter', () => { beforeEach(() => { output = instance.unhighlightItem(item, false); }); it("doesn't trigger event", () => { expect(passedElementTriggerEventStub.called).to.equal(false); }); it('returns this', () => { expect(output).to.deep.equal(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.deep.equal(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.deep.equal(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.deep.equal(instance); }); it('dispatches clearChoices action', () => { expect(storeDispatchStub.lastCall.args[0]).to.deep.equal({ type: ActionType.CLEAR_CHOICES, }); }); }); 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.deep.equal(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; instance._isSearching = 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.deep.equal({ type: ActionType.ACTIVATE_CHOICES, active: true, }); }); }); }); describe('setChoices with callback/Promise', () => { describe('not initialised', () => { beforeEach(() => { instance.initialised = false; instance.initialisedOK = undefined; }); it('should throw', () => { expect(() => instance.setChoices(null)).Throw(TypeError); }); }); describe('initialised twice', () => { it('throws', () => { instance.initialised = true; instance.initialisedOK = false; expect(() => instance.setChoices(null)).to.throw( TypeError, 'setChoices called for an element which has multiple instances of Choices initialised on it', ); }); }); 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 = ' `; instance = new Choices('[data-choice]', { choices, allowHTML: false, searchEnabled: true, }); }); describe('fuse', () => { beforeEach(() => { process.env.SEARCH_FUSE = 'full'; instance._searcher = new SearchByFuse(instance.config); }); it('details are passed', () => new Promise((done) => { const query = 'This is a query & a "test" with characters that should not be sanitised.'; instance.input.value = query; instance.input.focus(); instance.passedElement.element.addEventListener( 'search', (event) => { expect(event.detail).to.contains({ value: query, resultCount: 0, }); done(true); }, { once: true }, ); instance._onKeyUp({ target: null, keyCode: null }); instance._onInput({ target: null }); })); it('uses Fuse options', () => new Promise((done) => { instance.config.fuseOptions.isCaseSensitive = true; instance.config.fuseOptions.minMatchCharLength = 4; instance._searcher = new SearchByFuse(instance.config); instance.input.value = 'test'; instance.input.focus(); instance.passedElement.element.addEventListener( 'search', (event) => { expect(event.detail.resultCount).to.eql(0); done(true); }, { once: true }, ); instance._onKeyUp({ target: null, keyCode: null }); instance._onInput({ target: null }); })); it('is fired with a searchFloor of 0', () => new Promise((done) => { instance.config.searchFloor = 0; instance.input.value = 'qwerty'; instance.input.focus(); instance.passedElement.element.addEventListener('search', (event) => { expect(event.detail).to.contains({ value: instance.input.value, resultCount: 0, }); done(true); }); instance._onKeyUp({ target: null, keyCode: null }); instance._onInput({ target: null }); })); }); describe('prefix-filter', () => { beforeEach(() => { instance._searcher = new SearchByPrefixFilter(instance.config); }); it('details are passed', () => new Promise((done) => { const query = 'This is a query & a "test" with characters that should not be sanitised.'; instance.input.value = query; instance.input.focus(); instance.passedElement.element.addEventListener( 'search', (event) => { expect(event.detail).to.contains({ value: query, resultCount: 0, }); done(true); }, { once: true }, ); instance._onKeyUp({ target: null, keyCode: null }); instance._onInput({ target: null }); })); it('is fired with a searchFloor of 0', () => new Promise((done) => { instance.config.searchFloor = 0; instance.input.value = 'qwerty'; instance.input.focus(); instance.passedElement.element.addEventListener('search', (event) => { expect(event.detail).to.contains({ value: instance.input.value, resultCount: 0, }); done(true); }); instance._onKeyUp({ target: null, keyCode: null }); instance._onInput({ target: null }); })); }); }); }); describe('private methods', () => { describe('_createGroupsFragment', () => { let _createChoicesFragmentStub; const choices: ChoiceFull[] = [ { id: 1, selected: true, groupId: 1, value: 'Choice 1', label: 'Choice 1', disabled: false, active: false, placeholder: false, highlighted: false, score: 0, rank: 0, }, { id: 2, selected: false, groupId: 2, value: 'Choice 2', label: 'Choice 2', disabled: false, active: false, placeholder: false, highlighted: false, score: 0, rank: 0, }, { id: 3, selected: false, groupId: 1, value: 'Choice 3', label: 'Choice 3', disabled: false, active: false, placeholder: false, highlighted: false, score: 0, rank: 0, }, ]; const groups: GroupFull[] = [ { id: 2, label: 'Group 2', active: true, disabled: false, choices: [], }, { id: 1, label: 'Group 1', active: true, disabled: false, choices: [], }, ]; beforeEach(() => { _createChoicesFragmentStub = stub(); instance._createChoicesFragment = _createChoicesFragmentStub; }); afterEach(() => { instance._createChoicesFragment.reset(); }); describe('returning a fragment of groups', () => { describe('passing fragment argument', () => { it('updates fragment with groups', () => { const fragment = document.createDocumentFragment(); const childElement = document.createElement('div'); fragment.appendChild(childElement); output = instance._createGroupsFragment(groups, choices, fragment); const elementToWrapFragment = document.createElement('div'); elementToWrapFragment.appendChild(output); expect(output).to.be.instanceOf(DocumentFragment); expect(elementToWrapFragment.children[0]).to.deep.equal(childElement); expect(elementToWrapFragment.querySelectorAll('[data-group]').length).to.equal(2); }); }); describe('not passing fragment argument', () => { it('returns new groups fragment', () => { output = instance._createGroupsFragment(groups, choices); const elementToWrapFragment = document.createElement('div'); elementToWrapFragment.appendChild(output); expect(output).to.be.instanceOf(DocumentFragment); expect(elementToWrapFragment.querySelectorAll('[data-group]').length).to.equal(2); }); }); describe('sorting groups', () => { let sortFnStub; beforeEach(() => { sortFnStub = stub(); instance.config.sorter = sortFnStub; instance.config.shouldSort = true; }); afterEach(() => { instance.config.sorter.reset(); }); it('sorts groups by config.sorter', () => { expect(sortFnStub.called).to.equal(false); instance._createGroupsFragment(groups, choices); expect(sortFnStub.called).to.equal(true); }); }); describe('not sorting groups', () => { let sortFnStub; beforeEach(() => { sortFnStub = stub(); instance.config.sorter = sortFnStub; instance.config.shouldSort = false; }); afterEach(() => { instance.config.sorter.reset(); }); it('does not sort groups', () => { instance._createGroupsFragment(groups, choices); expect(sortFnStub.called).to.equal(false); }); }); describe('select-one element', () => { beforeEach(() => { instance._isSelectOneElement = true; }); it('calls _createChoicesFragment with choices that belong to each group', () => { expect(_createChoicesFragmentStub.called).to.equal(false); instance._createGroupsFragment(groups, choices); expect(_createChoicesFragmentStub.called).to.equal(true); expect(_createChoicesFragmentStub.firstCall.args[0][0]).to.contains({ selected: true, groupId: 1, value: 'Choice 1', label: 'Choice 1', }); expect(_createChoicesFragmentStub.firstCall.args[0][1]).to.contains({ selected: false, groupId: 1, value: 'Choice 3', label: 'Choice 3', }); }); }); describe('text/select-multiple element', () => { describe('renderSelectedChoices set to "always"', () => { beforeEach(() => { instance._isSelectOneElement = false; instance.config.renderSelectedChoices = 'always'; }); it('calls _createChoicesFragment with choices that belong to each group', () => { expect(_createChoicesFragmentStub.called).to.equal(false); instance._createGroupsFragment(groups, choices); expect(_createChoicesFragmentStub.called).to.equal(true); expect(_createChoicesFragmentStub.firstCall.args[0][0]).to.deep.contains({ selected: true, groupId: 1, value: 'Choice 1', label: 'Choice 1', }); expect(_createChoicesFragmentStub.firstCall.args[0][1]).to.deep.contains({ selected: false, groupId: 1, value: 'Choice 3', label: 'Choice 3', }); expect(_createChoicesFragmentStub.secondCall.args[0][0]).to.deep.contains({ selected: false, groupId: 2, value: 'Choice 2', label: 'Choice 2', }); }); }); describe('renderSelectedChoices not set to "always"', () => { beforeEach(() => { instance._isSelectOneElement = false; instance.config.renderSelectedChoices = false; }); it('calls _createChoicesFragment with choices that belong to each group that are not already selected', () => { expect(_createChoicesFragmentStub.called).to.equal(false); instance._createGroupsFragment(groups, choices); expect(_createChoicesFragmentStub.called).to.equal(true); expect(_createChoicesFragmentStub.firstCall.args[0][0]).to.deep.contains({ id: 3, selected: false, groupId: 1, value: 'Choice 3', label: 'Choice 3', }); expect(_createChoicesFragmentStub.secondCall.args[0][0]).to.deep.contains({ id: 2, selected: false, groupId: 2, value: 'Choice 2', label: 'Choice 2', }); }); }); }); }); }); describe('_generatePlaceholderValue', () => { describe('select element', () => { describe('when a placeholder option is defined', () => { it('returns the text value of the placeholder option', () => { const placeholderValue = 'I am a placeholder'; instance._isSelectElement = true; instance.passedElement.placeholderOption = { text: placeholderValue, }; const value = instance._generatePlaceholderValue(); expect(value).to.equal(placeholderValue); }); }); describe('when a placeholder option is not defined', () => { it('returns null', () => { instance._isSelectElement = true; instance.passedElement.placeholderOption = undefined; const value = instance._generatePlaceholderValue(); expect(value).to.equal(null); }); }); }); describe('text input', () => { describe('when the placeholder config option is set to true', () => { describe('when the placeholderValue config option is defined', () => { it('returns placeholderValue', () => { const placeholderValue = 'I am a placeholder'; instance._isSelectElement = false; instance.config.placeholder = true; instance.config.placeholderValue = placeholderValue; instance._hasNonChoicePlaceholder = true; const value = instance._generatePlaceholderValue(); expect(value).to.equal(placeholderValue); }); }); }); describe('when the placeholder config option is set to false', () => { it('returns null', () => { instance._isSelectElement = false; instance.config.placeholder = false; const value = instance._generatePlaceholderValue(); expect(value).to.equal(null); }); }); }); }); describe('_onKeyDown', () => { let items; let hasItems; let hasActiveDropdown; let hasFocussedInput; beforeEach(() => { instance.showDropdown = stub(); instance._onSelectKey = stub(); instance._onEnterKey = stub(); instance._onEscapeKey = stub(); instance._onDirectionKey = stub(); instance._onDeleteKey = stub(); ({ items } = instance._store); hasItems = instance.itemList.element.hasChildNodes(); hasActiveDropdown = instance.dropdown.isActive; hasFocussedInput = instance.input.isFocussed; }); describe('direction key', () => { const keyCodes = [ [KeyCodeMap.UP_KEY, 'ArrowUp'], [KeyCodeMap.DOWN_KEY, 'ArrowDown'], [KeyCodeMap.PAGE_UP_KEY, 'PageUp'], [KeyCodeMap.PAGE_DOWN_KEY, 'PageDown'], ]; keyCodes.forEach(([keyCode, key]) => { it(`calls _onDirectionKey with the expected arguments`, () => { const event = { keyCode, key, }; instance._onKeyDown(event); expect(instance._onDirectionKey).to.have.been.calledWith(event, hasActiveDropdown); }); }); }); describe('select key', () => { it(`calls _onSelectKey with the expected arguments`, () => { const event = { keyCode: KeyCodeMap.A_KEY, key: 'A', }; instance._onKeyDown(event); expect(instance._onSelectKey).to.have.been.calledWith(event, hasItems); }); }); describe('enter key', () => { it(`calls _onEnterKey with the expected arguments`, () => { const event = { keyCode: KeyCodeMap.ENTER_KEY, key: 'Enter', }; instance._onKeyDown(event); expect(instance._onEnterKey).to.have.been.calledWith(event, hasActiveDropdown); }); }); describe('delete key', () => { // this is not an error; the constants are named the reverse of their assigned key names, according // to their actual values, which appear to conform to the Windows VK mappings: // 0x08 = 'Backspace', 0x2E = 'Delete' // https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values#editing_keys const keyCodes = [ [KeyCodeMap.DELETE_KEY, 'Backspace'], [KeyCodeMap.BACK_KEY, 'Delete'], ]; keyCodes.forEach(([keyCode, key]) => { it(`calls _onDeleteKey with the expected arguments`, () => { const event = { keyCode, key, }; instance._onKeyDown(event); expect(instance._onDeleteKey).to.have.been.calledWith(event, items, hasFocussedInput); }); }); }); }); describe('_removeItem', () => { beforeEach(() => { instance._store.dispatch = stub(); }); afterEach(() => { instance._store.dispatch.reset(); }); describe('when given an item to remove', () => { const item: ChoiceFull = { highlighted: false, active: false, disabled: false, placeholder: false, selected: false, id: 1111, value: 'test value', label: 'test label', groupId: 3333, customProperties: {}, score: 0, rank: 0, }; it('dispatches a REMOVE_ITEM action to the store', () => { instance._removeItem(item); expect(instance._store.dispatch).to.have.been.calledWith(removeItem(item)); }); it('triggers a REMOVE_ITEM event on the passed element', () => new Promise((done) => { passedElement.addEventListener( 'removeItem', (event) => { expect(event.detail).to.contains({ id: item.id, value: item.value, label: item.label, customProperties: item.customProperties, groupValue: undefined, }); done(true); }, false, ); instance._removeItem(item); })); describe('when the item belongs to a group', () => { const group = { id: 1, label: 'testing', }; const itemWithGroup = { ...item, value: 'testing', groupId: group.id, }; beforeEach(() => { instance._store.getGroupById = stub(); instance._store.getGroupById.returns(group); }); afterEach(() => { instance._store.getGroupById.reset(); }); it("includes the group's value in the triggered event", () => new Promise((done) => { passedElement.addEventListener( 'removeItem', (event) => { expect(event.detail).to.contains({ id: itemWithGroup.id, value: itemWithGroup.value, label: itemWithGroup.label, customProperties: itemWithGroup.customProperties, groupValue: group.label, }); done(true); }, false, ); instance._removeItem(itemWithGroup); })); }); }); }); }); });