/* 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 { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands' import { html } from 'lit' import '../src/data-grist.js' import { selectionChange } from '../src/utils/select-range.js' /** * Shift-click selects a range, as a list does: the rows from the last one clicked to the clicked one take its new * state - from a row's checkbox and, on a grid whose row click toggles selection, from any cell of the row. */ type Root = HTMLElement & { renderRoot: ShadowRoot; updateComplete: Promise } const settle = (ms = 150) => new Promise(resolve => setTimeout(resolve, ms)) describe('selection by range', () => { const rows = () => Array.from({ length: 6 }, (_, i) => ({ id: String(i) })) it('a plain click names its rows and becomes the anchor', () => { const records = rows() expect(selectionChange(records, { added: [records[2]] })).to.deep.equal({ on: [records[2]], off: [], anchor: records[2] }) }) it('a shift-click gives every row from the anchor the clicked row’s new state, either way round', () => { const records = rows() expect(selectionChange(records, { added: [records[4]], range: true }, records[1]).on.map(one => one.id)).to.deep.equal(['1', '2', '3', '4']) expect(selectionChange(records, { added: [records[0]], range: true }, records[3]).on.map(one => one.id)).to.deep.equal(['0', '1', '2', '3']) expect(selectionChange(records, { removed: [records[5]], range: true }, records[2]).off.map(one => one.id)).to.deep.equal(['2', '3', '4', '5']) }) it('without an anchor in the rows a shift-click is a plain click', () => { const records = rows() expect(selectionChange(records, { added: [records[3]], range: true }).on).to.deep.equal([records[3]]) expect(selectionChange(records, { added: [records[3]], range: true }, { id: 'read before' }).on).to.deep.equal([records[3]]) }) describe('on a grid', () => { const ignoreResizeLoop = (e: ErrorEvent) => { if (String(e.message).includes('ResizeObserver loop')) { e.stopImmediatePropagation() e.preventDefault() } } let previousOnError: OnErrorEventHandler = null before(() => { 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 function () { this.timeout(10000) await settle(300) window.removeEventListener('error', ignoreResizeLoop, true) window.onerror = previousOnError await resetMouse() }) const grid = async (rowClick: boolean) => { const records = Array.from({ length: 8 }, (_, i) => ({ id: String(i), name: 'row ' + i })) const el = (await fixture(html` ({ total: records.length, records })} > `)) as Root & { fetch(): Promise; selected: any[] } await el.fetch() await settle() const grist = el.renderRoot.querySelector('#grist') as Root const body = grist.renderRoot.querySelector('ox-grid-body') as Root await body.updateComplete const row = (index: number) => body.renderRoot.querySelectorAll('div[grid-row]')[index] as HTMLElement const click = async (target: HTMLElement, shift = false) => { const box = target.getBoundingClientRect() if (shift) await sendKeys({ down: 'Shift' }) await sendMouse({ type: 'click', position: [Math.round(box.left + box.width / 2), Math.round(box.top + box.height / 2)] }) if (shift) await sendKeys({ up: 'Shift' }) await settle() } return { el, row, click } } it('shift-click on a checkbox selects the rows between', async function () { this.timeout(10000) const { el, row, click } = await grid(false) await click(row(1).querySelector('ox-grid-field')!.shadowRoot?.querySelector('input') ?? (row(1).querySelector('input') as HTMLElement)) await click(row(4).querySelector('ox-grid-field')!.shadowRoot?.querySelector('input') ?? (row(4).querySelector('input') as HTMLElement), true) expect(el.selected.map(one => one.id)).to.deep.equal(['1', '2', '3', '4']) }) it('shift-click on a cell of a row-click grid selects the rows between, and clears them the same way', async function () { this.timeout(10000) const { el, row, click } = await grid(true) const cell = (index: number) => row(index).querySelectorAll('ox-grid-field')[1] as HTMLElement await click(cell(5)) await click(cell(2), true) expect(el.selected.map(one => one.id)).to.deep.equal(['2', '3', '4', '5']) await click(cell(3)) await click(cell(5), true) expect(el.selected.map(one => one.id)).to.deep.equal(['2']) }) }) })