import { expect } from '@open-wc/testing' import { externalFilters, isEmptyFilterValue, normalizeFilters, ownedFilterIndex, patchFilterFields } from '../src/filters/filter-ownership.js' import type { ColumnConfig, FilterValue } from '../src/types.js' describe('filter field ownership', () => { const year = { name: 'year', filter: { operator: 'eq' } } as ColumnConfig const month = { name: 'month', filter: { operator: 'eq' } } as ColumnConfig const predicate = (name: string, operator: string, value: any) => ({ name, operator, value }) as FilterValue it('changing a month preserves same-name bounds and an external amount', () => { const base = [ predicate('year', 'eq', 2026), predicate('year', 'gte', 2025), predicate('year', 'lte', 2026), predicate('month', 'eq', 10), predicate('amount', 'gte', 100000) ] const result = patchFilterFields( base, [predicate('year', 'eq', 2026), predicate('month', 'eq', 11)], [year, month], ['month'] ) expect(result).to.deep.equal([base[0], base[1], base[2], predicate('month', 'eq', 11), base[4]]) expect(externalFilters(result, [year, month]).map(one => one.filter)).to.deep.equal([base[1], base[2], base[4]]) }) it('a field changed to the value it holds leaves the filters as they were, order included', () => { const base = [predicate('year', 'eq', '2026'), predicate('month', 'eq', '10')] const fields = [predicate('year', 'eq', '2026'), predicate('month', 'eq', '10')] expect(patchFilterFields(base, fields, [year, month], ['year'])).to.deep.equal(base) }) it('a field that held nothing adds its predicate after the others', () => { const base = [predicate('amount', 'gte', 100000)] expect(patchFilterFields(base, [predicate('month', 'eq', '9')], [year, month], ['month'])).to.deep.equal([ base[0], predicate('month', 'eq', '9') ]) }) it('ambiguous equalities both stay as chips and leave the field empty', () => { const base = [predicate('year', 'eq', 2025), predicate('year', 'eq', 2026)] expect(ownedFilterIndex(base, year)).to.equal(-1) expect(externalFilters(base, [year]).length).to.equal(2) }) it('a scalar field does not claim an array predicate', () => { expect(ownedFilterIndex([predicate('year', 'eq', [2025, 2026])], year)).to.equal(-1) }) }) describe('search predicates', () => { const name = { name: 'name', filter: { operator: 'eq' } } as ColumnConfig const search = (field: string) => ({ name: field, operator: 'search', value: '%박%' }) as FilterValue it('belong to the search term, never to the chips', () => { const filters = [search('code'), search('name'), { name: 'name', operator: 'eq', value: 'x' } as FilterValue] expect(externalFilters(filters, [name])).to.deep.equal([]) }) }) describe('an empty filter value', () => { it('is no value, an empty string, or a list of only those', () => { for (const value of [undefined, null, '', [], [''], ['', null]]) expect(isEmptyFilterValue(value), String(value)).to.equal(true) }) it('is not false, 0, or a list holding one choice', () => { for (const value of [false, 0, 'main', ['main'], ['', 'main'], [0]]) expect(isEmptyFilterValue(value), String(value)).to.equal(false) }) }) describe('filters entering the grid', () => { it('drop a list operator with no choice and keep the rest', () => { const filters = [ { name: 'type', operator: 'in', value: 'main' }, { name: 'state', operator: 'in', value: [] }, { name: 'kind', operator: 'notin', value: [''] }, { name: 'owner', operator: 'is_null', value: null } ] as FilterValue[] expect(normalizeFilters(filters)).to.deep.equal([ { name: 'type', operator: 'in', value: ['main'] }, { name: 'owner', operator: 'is_null', value: null } ]) }) }) describe('a filter that is a condition', () => { it('an operator that takes no value is one; an empty choice is not', async () => { const { isFilterCondition } = await import('../src/filters/filter-ownership.js') expect(isFilterCondition({ operator: 'is_true', value: null })).to.equal(true) expect(isFilterCondition({ operator: 'is_empty' })).to.equal(true) expect(isFilterCondition({ operator: 'eq', value: '' })).to.equal(false) expect(isFilterCondition({ operator: 'in', value: [] })).to.equal(false) expect(isFilterCondition({ operator: 'eq', value: false })).to.equal(true) expect(isFilterCondition({ operator: 'noteq', value: 'u-1' })).to.equal(true) }) })