/* Elements are compared by presence, not by value: a failing element comparison makes chai render the DOM and hang the runner. */ import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import { i18next } from '@operato/i18n' import '../src/data-grist.js' import '../src/filters/filters-form.js' import { fitsAsPills, hasAllChip, wordWidth } from '../src/filters/pill-choice.js' /* * A filter of two or three fixed choices with short words is chips - one press, where a select takes two - with a first * 「전체」 chip when nothing may be chosen. More choices, read choices or long words stay a searchable select * (2026-11-10: the inquiry list's 보기·종류·상태 were three selects). */ const kinds = [ { display: '', value: '' }, { display: '질문', value: 'QUESTION' }, { display: '버그 신고', value: 'BUG' }, { display: '기능 제안', value: 'PROPOSAL' } ] const columns = [ { type: 'string', name: 'kind', header: '종류', filter: { type: 'select', operator: 'eq', options: kinds } }, { type: 'string', name: 'status', header: '상태', filter: { type: 'select', operator: 'eq', options: ['', 'RECEIVED', 'IN_PROGRESS', 'ANSWERED', 'CLOSED'] } }, { type: 'string', name: 'owner', header: '담당', filter: { type: 'select', operator: 'eq', options: async () => [{ display: 'A', value: 'a' }, { display: 'B', value: 'b' }] } }, { type: 'string', name: 'long', header: '긴 글', filter: { type: 'select', operator: 'eq', options: ['', { display: '아주 긴 이름의 선택지', value: 'L' }, { display: '짧음', value: 'S' }] } }, { type: 'select', name: 'direction', header: '방향', record: { options: ['', 'SALE', 'PURCHASE'] }, filter: 'in' }, { type: 'string', name: 'view', header: '보기', filter: { type: 'select', operator: 'eq', value: 'mine', options: [{ display: '내 것', value: 'mine' }, { display: '처리할 것', value: 'turn' }] } } ] const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) describe('filters as pills', () => { const ignoreResizeLoop = (e: ErrorEvent) => { if (String(e.message).includes('ResizeObserver loop')) { e.stopImmediatePropagation() e.preventDefault() } } let previousOnError: OnErrorEventHandler = null let previousLanguage: string before(async () => { /* The 「전체」 chip's own words count toward the line: the test stands in Korean. */ previousLanguage = i18next.language await i18next.changeLanguage('ko') window.addEventListener('error', ignoreResizeLoop, true) previousOnError = window.onerror window.onerror = (message, ...rest) => (String(message).includes('ResizeObserver loop') ? true : previousOnError?.call(window, message, ...rest)) }) after(async () => { window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError /* The language is kept in the browser's storage; the next test file must not start in Korean. */ await i18next.changeLanguage(previousLanguage) }) async function form() { const asked: any[] = [] const grist = (await fixture(html` (asked.push(filters), { page: 1, limit: 20, total: 0, records: [] })} >
`)) as any await settle() const filters = grist.querySelector('ox-filters-form') as any await filters.updateComplete const root = filters.renderRoot as ShadowRoot const chooser = (name: string) => root.querySelector(`[name="${name}"]`) as any return { grist, chooser, asked } } const chipWords = async (buttons: any) => { await buttons.updateComplete return [...buttons.renderRoot.querySelectorAll('md-filter-chip')].map((chip: any) => chip.label || chip.textContent.trim()) } it('draws two or three short fixed choices as chips, 「전체」 first', async () => { const { chooser } = await form() expect(chooser('kind')?.tagName).to.equal('OX-INPUT-SELECT-BUTTONS') expect(await chipWords(chooser('kind'))).to.deep.equal(['전체', '질문', '버그 신고', '기능 제안']) expect(chooser('direction')?.tagName).to.equal('OX-INPUT-SELECT-BUTTONS') expect(chooser('direction').multiple).to.equal(true) expect(await chipWords(chooser('direction'))).to.deep.equal(['전체', 'SALE', 'PURCHASE']) }) it('keeps a select for more choices, read choices and long words', async () => { const { chooser } = await form() expect(chooser('status')?.tagName).to.equal('OX-SELECT') expect(chooser('owner')?.tagName).to.equal('OX-SELECT') expect(chooser('long')?.tagName).to.equal('OX-SELECT') }) it('a single choice that opens with a value and has no empty line has no 「전체」 and stays chosen', async () => { const { chooser } = await form() const view = chooser('view') expect(view?.tagName).to.equal('OX-INPUT-SELECT-BUTTONS') expect(await chipWords(view)).to.deep.equal(['내 것', '처리할 것']) expect(view.required).to.equal(true) }) it('a chip pressed reads the list with it, and 「전체」 reads it without', async () => { const { chooser, asked } = await form() const kind = chooser('kind') const chips = () => [...kind.renderRoot.querySelectorAll('md-filter-chip')] as any[] chips()[2].click() await settle() expect(asked[asked.length - 1].find((one: any) => one.name === 'kind')?.value).to.equal('BUG') chips()[0].click() await settle() expect(!!asked[asked.length - 1].find((one: any) => one.name === 'kind')).to.equal(false) }) }) describe('pill choice rules', () => { it('counts a wide character as 2', () => { expect(wordWidth('버그 신고')).to.equal(9) expect(wordWidth('Bug reports')).to.equal(11) }) it('fits up to three short choices, two chips at least with 「전체」 counted', () => { const eq = { type: 'select', operator: 'eq' as const } const one = { type: 'select', operator: 'in' as const } expect(fitsAsPills(eq, ['', 'A', 'B'], '전체')).to.equal(true) expect(fitsAsPills(eq, ['', 'A'], '전체')).to.equal(true) expect(fitsAsPills(one, ['A'], '전체')).to.equal(false) expect(fitsAsPills(eq, ['A', 'B', 'C', 'D'], '전체')).to.equal(false) expect(fitsAsPills(eq, ['일이삼사오육칠', 'B'], '전체')).to.equal(false) expect(fitsAsPills(eq, ['일이삼사오육', '일이삼사오육', '일이삼'], '전체')).to.equal(false) }) it('has 「전체」 unless a single choice must always have one, or there is one chip', () => { expect(hasAllChip({ type: 'select', operator: 'in' }, ['A', 'B'])).to.equal(true) expect(hasAllChip({ type: 'select', operator: 'eq' }, ['', 'A', 'B'])).to.equal(true) expect(hasAllChip({ type: 'select', operator: 'eq' }, ['A', 'B'])).to.equal(true) expect(hasAllChip({ type: 'select', operator: 'eq', value: 'A' } as any, ['A', 'B'])).to.equal(false) expect(hasAllChip({ type: 'select', operator: 'eq' }, ['', 'MINE'])).to.equal(true) expect(hasAllChip({ type: 'select', operator: 'in' }, ['CANCELLED'])).to.equal(false) }) })