Skip to content

Commit

Permalink
fix(fields): ignore empty values in getFiltersForValues
Browse files Browse the repository at this point in the history
  • Loading branch information
tkohr committed Dec 5, 2024
1 parent f7d3f67 commit 2601dac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
26 changes: 24 additions & 2 deletions libs/feature/search/src/lib/utils/service/fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
8 changes: 6 additions & 2 deletions libs/feature/search/src/lib/utils/service/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2601dac

Please sign in to comment.