From 2601dac2c2ad242da124563884fa8b32bc8c7aaf Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Thu, 5 Dec 2024 12:01:37 +0100 Subject: [PATCH] fix(fields): ignore empty values in getFiltersForValues --- .../src/lib/utils/service/fields.spec.ts | 26 +++++++++++++++++-- .../search/src/lib/utils/service/fields.ts | 8 ++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/libs/feature/search/src/lib/utils/service/fields.spec.ts b/libs/feature/search/src/lib/utils/service/fields.spec.ts index 95b2fce3e..4d8ba4138 100644 --- a/libs/feature/search/src/lib/utils/service/fields.spec.ts +++ b/libs/feature/search/src/lib/utils/service/fields.spec.ts @@ -275,10 +275,10 @@ describe('search fields implementations', () => { let filter beforeEach(async () => { filter = await lastValueFrom( - searchField.getFiltersForValues(['First value', 'Second value']) + searchField.getFiltersForValues(['First value', 'Second value', '']) ) }) - it('returns appropriate filters', () => { + it('returns appropriate filters (ignoring empty strings)', () => { expect(filter).toEqual({ myField: { 'First value': true, @@ -287,6 +287,17 @@ describe('search fields implementations', () => { }) }) }) + describe('#getFiltersForValues with empty value', () => { + let filter + beforeEach(async () => { + filter = await lastValueFrom(searchField.getFiltersForValues([''])) + }) + it('returns empty filter', () => { + expect(filter).toEqual({ + myField: {}, + }) + }) + }) describe('#getValuesForFilters', () => { let values describe('with several values', () => { @@ -362,6 +373,17 @@ describe('search fields implementations', () => { }) }) }) + describe('#getFiltersForValues with empty value', () => { + let filter + beforeEach(async () => { + filter = await lastValueFrom(searchField.getFiltersForValues([''])) + }) + it('returns empty filter', () => { + expect(filter).toEqual({ + changeDate: {}, + }) + }) + }) describe('#getValuesForFilters', () => { let values describe('with several values', () => { diff --git a/libs/feature/search/src/lib/utils/service/fields.ts b/libs/feature/search/src/lib/utils/service/fields.ts index 204083757..1d8422ba0 100644 --- a/libs/feature/search/src/lib/utils/service/fields.ts +++ b/libs/feature/search/src/lib/utils/service/fields.ts @@ -91,13 +91,17 @@ export class SimpleSearchField implements AbstractSearchField { if (this.getType() === 'values') { return of({ [this.esFieldName]: (values as FieldValue[]).reduce((acc, val) => { - return { ...acc, [val.toString()]: true } + const value = val.toString() + if (value !== '') { + return { ...acc, [value]: true } + } + return acc }, {}), }) } // DateRange return of({ - [this.esFieldName]: values[0], + [this.esFieldName]: values[0] !== '' ? values[0] : {}, }) } getValuesForFilter(