From 9c001487bacad7588e92f8b6d54880b88bf21fc6 Mon Sep 17 00:00:00 2001 From: Jeremy Hou Date: Sat, 19 Jan 2019 06:47:22 -0800 Subject: [PATCH] Fixes an issue where deepmerge concatenates array configs (#496) --- cypress/integration/select-multiple.spec.js | 30 +++++++++++++++++ cypress/integration/select-one.spec.js | 36 +++++++++++++++++++++ public/test/select-multiple.html | 10 ++++++ public/test/select-one.html | 10 ++++++ src/scripts/choices.js | 7 +++- 5 files changed, 92 insertions(+), 1 deletion(-) diff --git a/cypress/integration/select-multiple.spec.js b/cypress/integration/select-multiple.spec.js index 731721b..d7db618 100644 --- a/cypress/integration/select-multiple.spec.js +++ b/cypress/integration/select-multiple.spec.js @@ -853,5 +853,35 @@ describe('Choices - select multiple', () => { }); }); }); + + describe('searching by label only', () => { + it('gets zero results when searching by value', () => { + cy.get('[data-test-hook=search-by-label]') + .find('.choices__input--cloned') + .type('value1'); + + cy.get('[data-test-hook=search-by-label]') + .find('.choices__list--dropdown .choices__list') + .children() + .first() + .should($choice => { + expect($choice.text().trim()).to.equal('No results found'); + }); + }); + + it('gets a result when searching by label', () => { + cy.get('[data-test-hook=search-by-label]') + .find('.choices__input--cloned') + .type('label1'); + + cy.get('[data-test-hook=search-by-label]') + .find('.choices__list--dropdown .choices__list') + .children() + .first() + .should($choice => { + expect($choice.text().trim()).to.equal('label1'); + }); + }); + }); }); }); diff --git a/cypress/integration/select-one.spec.js b/cypress/integration/select-one.spec.js index f2d331e..2ff287d 100644 --- a/cypress/integration/select-one.spec.js +++ b/cypress/integration/select-one.spec.js @@ -867,5 +867,41 @@ describe('Choices - select one', () => { }); }); }); + + describe('searching by label only', () => { + beforeEach(() => { + cy.get('[data-test-hook=search-by-label]') + .find('.choices') + .click(); + }); + + it('gets zero results when searching by value', () => { + cy.get('[data-test-hook=search-by-label]') + .find('.choices__input--cloned') + .type('value1'); + + cy.get('[data-test-hook=search-by-label]') + .find('.choices__list--dropdown .choices__list') + .children() + .first() + .should($choice => { + expect($choice.text().trim()).to.equal('No results found'); + }); + }); + + it('gets a result when searching by label', () => { + cy.get('[data-test-hook=search-by-label]') + .find('.choices__input--cloned') + .type('label1'); + + cy.get('[data-test-hook=search-by-label]') + .find('.choices__list--dropdown .choices__list') + .children() + .first() + .should($choice => { + expect($choice.text().trim()).to.equal('label1'); + }); + }); + }); }); }); diff --git a/public/test/select-multiple.html b/public/test/select-multiple.html index 5b1d1dc..a2a7621 100644 --- a/public/test/select-multiple.html +++ b/public/test/select-multiple.html @@ -194,6 +194,14 @@ + +
+ + +
diff --git a/public/test/select-one.html b/public/test/select-one.html index 5611c4d..6f9e549 100644 --- a/public/test/select-one.html +++ b/public/test/select-one.html @@ -198,6 +198,14 @@ + +
+ + +
diff --git a/src/scripts/choices.js b/src/scripts/choices.js index bf57025..19e3af0 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -56,7 +56,12 @@ class Choices { } } - this.config = merge.all([DEFAULT_CONFIG, Choices.userDefaults, userConfig]); + this.config = merge.all( + [DEFAULT_CONFIG, Choices.userDefaults, userConfig], + // When merging array configs, replace with a copy of the userConfig array, + // instead of concatenating with the default array + { arrayMerge: (destinationArray, sourceArray) => [...sourceArray] }, + ); if (!doKeysMatch(this.config, DEFAULT_CONFIG)) { console.warn('Unknown config option(s) passed');