import { expect, fixture } from '@open-wc/testing' import { sendKeys } from '@web/test-runner-commands' import { html, render } from 'lit' import { i18next } from '@operato/i18n' import { OxInputDate } from '@operato/input/ox-input-date.js' import '../src/editors/ox-grist-editor-date.js' import '../src/editors/ox-grist-editor-week.js' import { OxFiltersForm } from '../src/filters/filters-form.js' import { FilterInput } from '../src/filters/filter-input.js' import { FilterRangeDate } from '../src/filters/filter-range-date.js' /* * Days, weeks and months in a grid - its filters and its editors - are the one field for them, ox-input-date: typed * at a desk, the phone's own picker on touch. Tests stand at a desk. */ const inner = (field: Element) => (field as OxInputDate).renderRoot.querySelector('input') as HTMLInputElement describe('date fields in a grid', () => { const touchQuery = OxInputDate.touchQuery let previousLanguage: string before(async () => { OxInputDate.touchQuery = 'not all' previousLanguage = i18next.language await i18next.changeLanguage('ko') }) after(async () => { OxInputDate.touchQuery = touchQuery /* The language is kept in the browser's storage; the next test file must not start in Korean. */ await i18next.changeLanguage(previousLanguage) }) describe('filters', () => { const column = (type: string, operator = 'between') => ({ name: 'day', type, header: 'day', record: {}, filter: { operator } }) as any it('bounds a range of days, weeks or months by two of the one field; a time keeps the browser\'s', async () => { for (const type of ['date', 'week', 'month']) { const form = await fixture(html`
`) const fields = form.querySelectorAll('ox-input-date') expect(fields.length, type).to.equal(2) expect((fields[0] as OxInputDate).type).to.equal(type) expect(form.querySelector('input'), type).to.equal(null) } const time = await fixture(html`
`) expect(time.querySelectorAll('input[type="datetime-local"]').length).to.equal(2) }) it('shows a default bound given as a time as its day, and a week as it is', async () => { const from = new Date(2026, 10, 2).getTime() const days = await fixture(html`
`) const [first, last] = Array.from(days.querySelectorAll('ox-input-date')) as OxInputDate[] expect(first.value).to.equal('2026-11-02') expect(last.value).to.equal('2026-11-10') const weeks = await fixture(html`
`) expect((Array.from(weeks.querySelectorAll('ox-input-date')) as OxInputDate[]).map(field => field.value)).to.deep.equal(['2026-W40', '2026-W45']) }) it('gives the form both bounds, from first, and narrows the column when one is typed', async () => { const form = await fixture(html`
`) expect(new FormData(form).getAll('day')).to.deep.equal(['2026-11-01', '2026-11-30']) let asked: any form.addEventListener('filter-change', e => (asked = (e as CustomEvent).detail)) const to = form.querySelector('ox-input-date[to]') as OxInputDate inner(to).focus() inner(to).select() await sendKeys({ type: '1115' }) await sendKeys({ press: 'Enter' }) expect(asked).to.deep.equal({ name: 'day', operator: 'between', value: ['2026-11-01', '2026-11-15'] }) }) it('names its two empty bounds 시작일 and 종료일, not a format pattern', async () => { const form = await fixture(html`
`) const fields = Array.from(form.querySelectorAll('ox-input-date')) as OxInputDate[] await Promise.all(fields.map(field => field.updateComplete)) expect(fields.map(field => inner(field).placeholder)).to.deep.equal(['시작일', '종료일']) }) it('narrows by one month typed into a single month filter', async () => { const form = await fixture(html`
${FilterInput(column('month', 'eq'), '', {} as any)}
`) let asked: any form.addEventListener('filter-change', e => (asked = (e as CustomEvent).detail)) const field = form.querySelector('ox-input-date') as OxInputDate inner(field).focus() await sendKeys({ type: '3월' }) await sendKeys({ press: 'Enter' }) expect(asked.value).to.match(/^\d{4}-03$/) }) /* The phone rule of 398caeefe holds for the field: a range on a 375px screen ends inside its room. */ it('keeps a range of the field inside a phone\'s width', async () => { const room = await fixture(html`
`) const host = document.createElement('div') host.className = 'host' room.appendChild(host) const root = host.attachShadow({ mode: 'open' }) const style = document.createElement('style') style.textContent = (Array.isArray(OxFiltersForm.styles) ? OxFiltersForm.styles : [OxFiltersForm.styles]) .map(s => String((s as any).cssText ?? s)) .join('\n') root.appendChild(style) const form = document.createElement('form') root.appendChild(form) render( html``, form ) await (form.querySelector('ox-input-date[to]') as OxInputDate).updateComplete const right = Math.round(form.querySelector('ox-input-date[to]')!.getBoundingClientRect().right) expect(right, 'the last date runs past the room').to.be.at.most(Math.round(room.getBoundingClientRect().right)) }) }) describe('editors', () => { const open = async (tag: string, value: string) => { const editor = await fixture( tag === 'week' ? html`` : html`` ) await editor.updateComplete await new Promise(r => requestAnimationFrame(r)) return editor } it('edits a day cell in the field and commits what is typed when it is left', async () => { const editor = await open('date', '2026-01-01') const field = editor.editor as OxInputDate expect(field.tagName).to.equal('OX-INPUT-DATE') await field.updateComplete expect(inner(field).value).to.equal('2026-01-01') let commit: any editor.addEventListener('field-change', (e: CustomEvent) => (commit = e.detail)) inner(field).focus() inner(field).select() await sendKeys({ type: '1110' }) inner(field).blur() expect(commit?.after).to.equal('2026-11-10') }) it('commits the week a key moved to, and nothing when nothing was entered', async () => { const editor = await open('week', '2026-W45') const field = editor.editor as OxInputDate let commit: any editor.addEventListener('field-change', (e: CustomEvent) => (commit = e.detail)) inner(field).focus() inner(field).blur() expect(commit, 'an editor left untouched commits nothing').to.equal(undefined) inner(field).focus() await sendKeys({ press: 'ArrowUp' }) await field.updateComplete editor.flush() expect(commit?.after).to.equal('2026-W46') }) }) })