import { expect, fixture } from '@open-wc/testing' import { html } from 'lit' import '../src/data-grid/data-grid.js' import '../src/data-grid/data-grid-body.js' import { buildColumn } from '../src/configure/column-builder.js' import { columnText } from '../src/utils/column-text.js' import { ZERO_CONFIG, ZERO_DATA } from '../src/configure/zero-config.js' /** * Accessible names on the inputs inside the grid. * * ## Why this is tested * * Several inputs here had no name: the row-select checkboxes, the header filter boxes, and the IME * buffer that holds focus. Position carries the meaning for a sighted reader — a narrow leftmost * column means row select, a box under a header name filters that column — but position is a * picture, not a relationship, so nothing of it reaches a screen reader. * * ## What has to hold * * The name has one source. Editing `header` must change what is announced, which means callers * cannot spell the name out again. `columnText()` is that source. * * `buildColumn` turns a string header into `() => header`, and that function returns a lit template * the name cannot be read back out of. The name was disappearing right there. */ describe('column name has one source', () => { it('keeps the plain text after a string header becomes a function', () => { const column = buildColumn({ type: 'string', name: 'palletId', header: '파렛트 번호' }) expect(columnText(column)).to.equal('파렛트 번호') }) it('keeps it when the string arrives as header.renderer', () => { const column = buildColumn({ type: 'string', name: 'palletId', header: { renderer: '파렛트 번호' } }) expect(columnText(column)).to.equal('파렛트 번호') }) it('lets label: true inherit the header name', () => { const column = buildColumn({ type: 'string', name: 'palletId', header: '파렛트 번호', label: true }) expect(columnText(column)).to.equal('파렛트 번호') }) it('falls back to column.name when the header is drawn by a function', () => { const column = buildColumn({ type: 'string', name: 'palletId', header: { renderer: () => html`x` } }) expect(columnText(column)).to.equal('palletId') }) it('invents no name when there is no column', () => { expect(columnText(undefined)).to.equal('') expect(columnText(null)).to.equal('') }) }) describe('row-select boxes are named', () => { const columns = [ { type: 'gutter', gutterName: 'row-selector', name: '__selected__', multiple: true }, { type: 'string', name: 'palletId', header: '파렛트 번호' } ].map(buildColumn) it('says which row each checkbox selects', async () => { const body: any = await fixture(html` `) await body.updateComplete /* The checkboxes live one shadow root deeper, inside each ox-grid-field. */ const fields = Array.from(body.renderRoot.querySelectorAll('ox-grid-field')) as any[] await Promise.all(fields.map(field => field.updateComplete)) const boxes = fields.flatMap(field => Array.from(field.renderRoot?.querySelectorAll('input[type=checkbox], input[type=radio]') || []) ) as HTMLInputElement[] expect(boxes.length, 'found no row-select box').to.be.greaterThan(0) boxes.forEach(box => expect(box.getAttribute('aria-label'), 'an unnamed checkbox is left').to.be.a('string').and.not.equal('') ) }) }) describe('IME buffer names the cell it stands in for', () => { const columns = [ { type: 'string', name: 'palletId', header: '파렛트 번호' }, { type: 'string', name: 'location', header: '보관 위치' } ].map(buildColumn) async function bodyFocusedAt(column: number) { const body: any = await fixture(html` `) await body.updateComplete return body.renderRoot.querySelector('#ime-buffer') as HTMLInputElement } it('reads the first column while focus sits there', async () => { expect((await bodyFocusedAt(0)).getAttribute('aria-label')).to.contain('파렛트 번호') }) it('changes what it reads when focus moves a column over', async () => { expect((await bodyFocusedAt(1)).getAttribute('aria-label')).to.contain('보관 위치') }) it('stays visible to assistive tech — it is where focus lands', async () => { expect((await bodyFocusedAt(0)).getAttribute('aria-hidden')).to.be.null }) }) /** * Table semantics. * * ## Why this is tested * * The grid is drawn with CSS grid: cells sit flat and there is no row element. Rows are a picture * made of equal offsetTop, so a screen reader saw a pile of inputs rather than a table — no "this * is a table", no "row 3 column 3", no "40 rows in all". * * Adding row wrappers can break two things at once: * * ``` * semantics grid / row / columnheader / gridcell have to nest correctly * layout display: contents must generate no box; if it does, the columns collapse * ``` * * The second one is why geometry is asserted: cells in a row must step right, and rows must step * down. */ describe('grid declares itself a grid', () => { /* ox-grid takes already-compiled columns; compiling them is ox-grist's job. */ const columns = [ { type: 'gutter', gutterName: 'row-selector', name: '__selected__', multiple: true }, { type: 'string', name: 'palletId', header: '파렛트 번호', sortable: true }, { type: 'string', name: 'location', header: '보관 위치', sortable: true } ].map(buildColumn) async function grid(overrides: any = {}) { const el: any = await fixture(html` `) await el.updateComplete const header = el.renderRoot.querySelector('ox-grid-header') const body = el.renderRoot.querySelector('ox-grid-body') await header?.updateComplete await body?.updateComplete return { el, header, body } } it('nests grid, row and cell', async () => { const { el, header, body } = await grid() const grid_ = el.renderRoot.querySelector('[role=grid]') expect(grid_, 'no role=grid').to.exist expect(header.getAttribute('role')).to.equal('rowgroup') expect(body.getAttribute('role')).to.equal('rowgroup') expect(header.renderRoot.querySelectorAll('[role=row]').length).to.equal(1) expect(body.renderRoot.querySelectorAll('[role=row]').length).to.equal(2) expect(header.renderRoot.querySelectorAll('[role=columnheader]').length).to.equal(3) }) it('counts the whole table, not the page on screen', async () => { const { el } = await grid() const grid_ = el.renderRoot.querySelector('[role=grid]') /* One header row plus 40 records. Two rows drawn is still a 40-row table. */ expect(grid_.getAttribute('aria-rowcount')).to.equal('41') expect(grid_.getAttribute('aria-colcount')).to.equal('3') expect(grid_.getAttribute('aria-label')).to.equal('입고 대기 목록') }) it('numbers page 2 from row 22, not back at 1', async () => { const { body } = await grid({ page: 2 }) const rows = Array.from(body.renderRoot.querySelectorAll('[role=row]')) as HTMLElement[] /* One header row plus 20 records on page 1. */ expect(rows[0].getAttribute('aria-rowindex')).to.equal('22') expect(rows[1].getAttribute('aria-rowindex')).to.equal('23') }) it('numbers columns and leaves the width filler uncounted', async () => { const { body } = await grid() const row = body.renderRoot.querySelector('[role=row]') as HTMLElement const cells = Array.from(row.querySelectorAll('[role=gridcell]')) as HTMLElement[] expect(cells.map(cell => cell.getAttribute('aria-colindex'))).to.deep.equal(['1', '2', '3']) expect(row.querySelectorAll('[role=presentation]').length, 'the filler was counted as a cell').to.equal(1) }) it('states sort direction in text, not only as an arrow', async () => { const el: any = await fixture(html` `) await el.updateComplete const header = el.renderRoot.querySelector('ox-grid-header') await header.updateComplete const headers = Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] const sorts = headers.map(cell => cell.getAttribute('aria-sort')) /* A gutter cannot be sorted; claiming otherwise announces a control that is not there. */ expect(sorts[0]).to.be.null expect(sorts[1], 'sorted descending').to.equal('descending') expect(sorts[2], 'sortable but not currently sorted').to.equal('none') }) it('keeps the layout — a row wrapper that generates a box collapses the grid', async () => { const { body } = await grid() const rows = Array.from(body.renderRoot.querySelectorAll('[role=row]')) as HTMLElement[] const first = Array.from(rows[0].children) as HTMLElement[] expect(getComputedStyle(rows[0]).display).to.equal('contents') /* Cells step right; stacked at one x means the columns are gone. */ const lefts = first.map(cell => cell.offsetLeft) expect(new Set(lefts).size, 'cells stacked at the same x').to.be.greaterThan(1) lefts.slice(1).forEach((left, index) => expect(left).to.be.greaterThan(lefts[index])) /* Rows step down. */ const firstTop = (rows[0].children[0] as HTMLElement).offsetTop const secondTop = (rows[1].children[0] as HTMLElement).offsetTop expect(secondTop, 'two rows at the same y').to.be.greaterThan(firstTop) }) }) /** * Grouped headers. * * The header becomes two rows and ungrouped columns span both (`.span-both`). Placement runs * through CSS `grid-row` / `grid-column`, which makes this the riskiest spot for a row wrapper. */ describe('grouped header spans two rows', () => { const columns = [ { type: 'string', name: 'palletId', header: '파렛트 번호' }, { type: 'string', name: 'width', header: { renderer: '폭', group: '치수' } }, { type: 'string', name: 'height', header: { renderer: '높이', group: '치수' } } ].map(buildColumn) async function grouped() { const el: any = await fixture(html` `) await el.updateComplete const header = el.renderRoot.querySelector('ox-grid-header') const body = el.renderRoot.querySelector('ox-grid-body') await header.updateComplete await body.updateComplete return { el, header, body } } it('counts two header rows so the body starts at row 3', async () => { const { el, header, body } = await grouped() expect(header.renderRoot.querySelectorAll('[role=row]').length).to.equal(2) /* Two header rows plus one record. */ expect(el.renderRoot.querySelector('[role=grid]').getAttribute('aria-rowcount')).to.equal('3') const rows = Array.from(body.renderRoot.querySelectorAll('[role=row]')) as HTMLElement[] expect(rows[0].getAttribute('aria-rowindex')).to.equal('3') }) it('says how many columns a group covers', async () => { const { header } = await grouped() const cells = Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] const first = cells[0] const group = cells.find(cell => cell.getAttribute('aria-colspan')) /* An ungrouped column spans both header rows. */ expect(first.getAttribute('aria-rowspan')).to.equal('2') expect(first.getAttribute('aria-colindex')).to.equal('1') /* The group header covers columns 2 and 3. */ expect(group, 'found no group header').to.exist expect(group!.getAttribute('aria-colindex')).to.equal('2') expect(group!.getAttribute('aria-colspan')).to.equal('2') }) it('keeps two-row header placement — group sits above its leaf columns', async () => { const { header } = await grouped() const cells = Array.from(header.renderRoot.querySelectorAll('[role=columnheader]')) as HTMLElement[] const group = cells.find(cell => cell.getAttribute('aria-colspan'))! const leaf = cells.find(cell => !cell.getAttribute('aria-colspan') && cell !== cells[0])! expect(leaf.offsetTop, 'group header and its leaf column at the same height').to.be.greaterThan(group.offsetTop) expect(group.offsetWidth, 'group header does not cover two columns').to.be.greaterThan(leaf.offsetWidth) }) }) /** * The grid wrapper must not disturb the surrounding layout. * * `role=grid` admits only rows, so the settings-button slot and the pager moved outside the * wrapper. Two things then depend on this structure: * * ``` * column layout the host is a flex column. The wrapper is itself a flex column (it is the one * scroller that header and body share), so the body still takes the height * left under the header * sibling selector "show the settings button on header hover" is an adjacent-sibling rule, so * the button has to sit directly after the wrapper * ``` */ describe('grid wrapper stays out of the layout', () => { const columns = [ { type: 'string', name: 'palletId', header: '파렛트 번호' }, { type: 'string', name: 'location', header: '보관 위치' } ].map(buildColumn) it('places the settings button after the grid, not inside it', async () => { const el: any = await fixture(html` `) await el.updateComplete const grid = el.renderRoot.querySelector('[role=grid]') as HTMLElement const setting = el.renderRoot.querySelector('div[setting]') as HTMLElement expect(setting, 'no settings slot').to.exist expect(grid.contains(setting), 'settings button inside the grid breaks table reading').to.be.false expect(grid.nextElementSibling, 'adjacent-sibling rule no longer holds').to.equal(setting) }) it('lays header and body out as a column that fills the host', async () => { const el: any = await fixture(html`
`) const grid = el.querySelector('ox-grid') as any await grid.updateComplete const wrapper = grid.renderRoot.querySelector('[role=grid]') as HTMLElement const header = grid.renderRoot.querySelector('ox-grid-header') as HTMLElement const body = grid.renderRoot.querySelector('ox-grid-body') as HTMLElement expect(getComputedStyle(wrapper).display).to.equal('flex') expect(getComputedStyle(wrapper).flexDirection).to.equal('column') /* The body takes the remaining height; a wrapper with a box shrinks it to header height. */ expect(body.offsetHeight, 'body does not take the remaining height').to.be.greaterThan(100) expect(body.offsetTop, 'body is not below the header').to.be.greaterThan(0) expect(header.offsetTop).to.equal(0) }) })