import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import { addressController, writeAddress } from '@operato/utils/address.js' import type { DataGrist } from '../src/data-grist.js' import '../src/data-grist.js' import '../src/filters/filters-form.js' /* * An address without `filters` means the list's configured defaults, applied and written into the address; an * address with `filters=[]` means no conditions. A list operator's value is always an array, also when a default, * a link or a form gives one choice as a scalar. (address-centralization-problem-2026-10-19 §7 D7) */ async function until(check: () => boolean, what: string) { for (let i = 0; i < 200; i++) { if (check()) return await new Promise(resolve => setTimeout(resolve, 5)) } throw new Error(`did not settle: ${what}`) } const filtersInAddress = () => new URLSearchParams(location.search).get('filters') describe('list defaults and the shape of list operators', () => { let previousOnError: typeof window.onerror const resizeNotice = (event: ErrorEvent) => { if (event.message.includes('ResizeObserver loop')) { event.preventDefault() event.stopImmediatePropagation() } } before(() => { window.addEventListener('error', resizeNotice, true) previousOnError = window.onerror window.onerror = (message, ...rest) => String(message).includes('ResizeObserver loop') ? true : previousOnError?.call(window, message, ...rest) }) after(() => { window.removeEventListener('error', resizeNotice, true) window.onerror = previousOnError }) let reads: any[][] = [] const typeColumn = { name: 'type', header: 'type', filter: { operator: 'in', value: 'main' } } const mount = async (columns: any[] = [typeColumn]) => { addressController().registerQuery() reads = [] const grist = await fixture( html` { reads.push(filters) return { records: [], total: 0 } }} >` ) return grist } it('an address without filters reads with the defaults and then says so', async () => { await writeAddress('?defaults=start') await mount() await until(() => reads.length > 0, 'first read') expect(reads[reads.length - 1]).to.deep.equal([{ name: 'type', operator: 'in', value: ['main'] }]) await until(() => filtersInAddress() !== null, 'defaults written') expect(filtersInAddress()).to.equal("[{name:'type',operator:'in',value:['main']}]") expect(reads.every(read => read.length === 1)).to.equal(true) }) it('a default whose list holds no choice is no condition: it is neither read nor written', async () => { // A board list's state default was `in []`; it reached the server as `state IN ()`, which postgres refuses. await writeAddress('?defaults=empty-list') await mount([typeColumn, { name: 'state', header: 'state', filter: { operator: 'in', value: [] } }]) await until(() => reads.length > 0, 'first read') expect(reads[reads.length - 1]).to.deep.equal([{ name: 'type', operator: 'in', value: ['main'] }]) await until(() => filtersInAddress() !== null, 'defaults written') expect(filtersInAddress()).to.equal("[{name:'type',operator:'in',value:['main']}]") }) it('an address holding a list operator with no choice reads without it', async () => { await writeAddress("?filters=[{name:'type',operator:'in',value:['main']},{name:'state',operator:'in',value:[]}]") await mount([typeColumn, { name: 'state', header: 'state', filter: { operator: 'in', value: [] } }]) await until(() => reads.length > 0, 'first read') expect(reads.every(read => read.every((filter: any) => filter.name !== 'state'))).to.equal(true) expect(reads[reads.length - 1]).to.deep.equal([{ name: 'type', operator: 'in', value: ['main'] }]) }) it('a default of false or 0 is a condition', async () => { await writeAddress('?defaults=falsy-values') await mount([ { name: 'active', header: 'active', type: 'boolean', filter: { operator: 'eq', value: false } }, { name: 'count', header: 'count', type: 'integer', filter: { operator: 'eq', value: 0 } } ]) await until(() => reads.length > 0, 'first read') expect(reads[reads.length - 1]).to.deep.equal([ { name: 'active', operator: 'eq', value: false }, { name: 'count', operator: 'eq', value: 0 } ]) }) it('an address with filters=[] reads with no conditions and keeps it', async () => { await writeAddress('?filters=[]') await mount() await until(() => reads.length > 0, 'first read') expect(reads[reads.length - 1]).to.deep.equal([]) expect(filtersInAddress()).to.equal('[]') }) it('clearing every condition of a list with defaults writes filters=[]', async () => { await writeAddress("?filters=[{name:'type',operator:'in',value:['sub']}]") const grist = await mount() await until(() => reads.length > 0, 'first read') grist.dispatchEvent( new CustomEvent('fetch-params-change', { bubbles: true, composed: true, detail: { filters: [], from: 'filters-form' } }) ) await until(() => filtersInAddress() === '[]', 'empty written') await until(() => reads[reads.length - 1]?.length === 0, 'read with none') }) /* * The form draws the conditions the list is read with. A select whose default was chosen away (filters=[]) shows no * choice - it once drew its default again, so 「진행」 sat above a list of every item. */ it('a filter chosen away shows no choice in the form, its default neither', async () => { const stateColumn = { name: 'state', header: 'state', type: 'select', filter: { operator: 'eq', value: 'GOING', options: ['GOING', 'ENDED'] } } const shown = async (address: string) => { await writeAddress(address) addressController().registerQuery() reads = [] const grist = await fixture( html` { reads.push(filters) return { records: [], total: 0 } }} >
` ) await until(() => reads.length > 0, 'first read') const form = grist.querySelector('ox-filters-form') as any await form.updateComplete /* Two choices are chips now (pill-choice); the chooser is read by its name, whatever draws it. */ return (form.renderRoot as ShadowRoot).querySelector('[name="state"]') as any } expect((await shown('?defaults=select'))?.value).to.equal('GOING') expect((await shown('?filters=[]'))?.value ?? '').to.equal('') expect(reads[reads.length - 1]).to.deep.equal([]) }) it('a single choice given as a scalar is read as a one-element array', async () => { await writeAddress("?filters=[{name:'type',operator:'in',value:'sub'}]") await mount() await until(() => reads.length > 0, 'first read') expect(reads[reads.length - 1]).to.deep.equal([{ name: 'type', operator: 'in', value: ['sub'] }]) }) })