From 1b7ad266194d43492b7bdabb70cb7615a37670f2 Mon Sep 17 00:00:00 2001 From: Mateusz Rzepa Date: Sat, 22 Nov 2025 12:30:09 +0100 Subject: [PATCH] Fix boolean facet selection and add regression test --- src/helpers.js | 9 +++------ tests/searchSpec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index aecc87b..965f812 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -333,20 +333,17 @@ export const getBuckets = function(data, input, aggregations) { } const doc_count = v2[1].array().length; + const isSelected = filters.some((f) => String(f) === String(v2[0])); //hide zero_doc_count facet only if it is not selected - if ( - hide_zero_doc_count && - doc_count === 0 && - filters.indexOf(v2[0]) === -1 - ) { + if (hide_zero_doc_count && doc_count === 0 && !isSelected) { return; } return { key: v2[0], doc_count: doc_count, - selected: filters.indexOf(v2[0]) !== -1, + selected: isSelected, }; }) .filter(Boolean); diff --git a/tests/searchSpec.js b/tests/searchSpec.js index c62ec03..9edc48b 100644 --- a/tests/searchSpec.js +++ b/tests/searchSpec.js @@ -200,6 +200,36 @@ describe('search', function () { done(); }); + it('marks boolean facets as selected', function test(done) { + const dataset = [ + { boolean: true, string: 'true' }, + { boolean: false, string: 'false' }, + ]; + + const itemsjs = itemsJS(dataset, { + aggregations: { + boolean: {}, + string: {}, + }, + }); + + const result = itemsjs.search({ + filters: { + boolean: [true], + string: ['true'], + }, + }); + + const booleanBuckets = result.data.aggregations.boolean.buckets; + const stringBuckets = result.data.aggregations.string.buckets; + + assert.equal(booleanBuckets[0].key, 'true'); + assert.equal(booleanBuckets[0].selected, true); + assert.equal(stringBuckets[0].selected, true); + + done(); + }); + it('makes search with non existing filter value with conjunction true should return no results', function test(done) { const itemsjs = itemsJS(items, configuration);