import { expect, fixture } from '@open-wc/testing' import { html, render } from 'lit' import { DataProvider } from '../src/data-provider.js' import type { DataConsumer } from '../src/data-consumer.js' import { FilterSelectButtons } from '../src/filters/filter-select-buttons.js' import type { FetchOption, FetchResult } from '../src/types.js' /** * The counts beside a list's filter choices (ADR-0190): read with the conditions of each fetch that replaces the rows, * under its signal - only the counts of the current read land, and a chip shows them when its option has none. */ function deferred() { let resolve!: (value: T) => void const promise = new Promise(yes => (resolve = yes)) return { promise, resolve } } function setup() { const counted: { options: FetchOption; answer: ReturnType>>> }[] = [] const consumer = Object.assign(new EventTarget(), { shownMode: 'GRID' as const, data: { records: [] }, _data: { records: [] }, checkDirties() {}, showSpinner() {}, hideSpinner() {}, fetchHandler: async (): Promise => ({ total: 0, records: [] }), countHandler(options: FetchOption) { const answer = deferred>>() counted.push({ options, answer }) return answer.promise } }) as unknown as DataConsumer return { provider: new DataProvider(consumer), consumer, counted } } describe('countHandler', () => { it("is read with the fetch's conditions and signal; a count of a read replaced never lands", async () => { const { provider, consumer, counted } = setup() provider.filters = [{ name: 'state', operator: 'in', value: ['draft'] }] as any void provider.fetch() void provider.fetch() expect(counted.length).to.equal(2) expect(counted[0].options.filters).to.deep.equal([{ name: 'state', operator: 'in', value: ['draft'] }]) expect(counted[0].options.signal?.aborted).to.equal(true) counted[1].answer.resolve({ state: { draft: 2 } }) await Promise.resolve() counted[0].answer.resolve({ state: { draft: 99 } }) await Promise.resolve() expect(consumer.counts).to.deep.equal({ state: { draft: 2 } }) }) it('a chip shows the grid count for its column and value, unless the option gives its own', async () => { const column = { name: 'state', filter: { operator: 'in', options: [{ display: 'Draft', value: 'draft' }, { display: 'Released', value: 'released', count: 7 }] }, record: {} } as any const host = await fixture(html`
`) render(FilterSelectButtons(column, [], { counts: { state: { draft: 3, released: 1 } } } as any), host) const buttons = host.querySelector('ox-input-select-buttons') as any expect(buttons.options.map((one: any) => one.count)).to.deep.equal([3, 7]) }) })