diff --git a/test/browser/column/colSpan.test.ts b/test/browser/column/colSpan.test.ts index 438a6ab970..0e487cd0f0 100644 --- a/test/browser/column/colSpan.test.ts +++ b/test/browser/column/colSpan.test.ts @@ -96,78 +96,78 @@ describe('colSpan', () => { setupColSpanGrid(); // header row await userEvent.click(getHeaderCells()[7]); - validateCellPosition(7, 0); + await validateCellPosition(7, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(8, 0); + await validateCellPosition(8, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(11, 0); + await validateCellPosition(11, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(12, 0); + await validateCellPosition(12, 0); await userEvent.keyboard('{arrowleft}{arrowleft}{arrowleft}'); - validateCellPosition(7, 0); + await validateCellPosition(7, 0); // top summary rows await userEvent.click(getCellsAtRowIndex(0)[6]); - validateCellPosition(6, 1); + await validateCellPosition(6, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(7, 1); + await validateCellPosition(7, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(9, 1); + await validateCellPosition(9, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(10, 1); + await validateCellPosition(10, 1); await userEvent.keyboard('{arrowleft}{arrowleft}{arrowleft}'); - validateCellPosition(6, 1); + await validateCellPosition(6, 1); // viewport rows await userEvent.click(getCellsAtRowIndex(3)[1]); - validateCellPosition(1, 4); + await validateCellPosition(1, 4); await userEvent.keyboard('{arrowright}'); - validateCellPosition(2, 4); + await validateCellPosition(2, 4); await userEvent.keyboard('{arrowright}'); - validateCellPosition(3, 4); + await validateCellPosition(3, 4); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(2, 5); + await validateCellPosition(2, 5); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(1, 5); + await validateCellPosition(1, 5); await userEvent.keyboard('{arrowright}'); - validateCellPosition(2, 5); + await validateCellPosition(2, 5); await userEvent.keyboard('{arrowright}'); - validateCellPosition(5, 5); + await validateCellPosition(5, 5); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(2, 5); + await validateCellPosition(2, 5); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(2, 6); + await validateCellPosition(2, 6); await userEvent.keyboard('{arrowdown}{arrowdown}'); - validateCellPosition(0, 8); + await validateCellPosition(0, 8); await userEvent.keyboard('{arrowLeft}'); - validateCellPosition(0, 8); + await validateCellPosition(0, 8); await userEvent.keyboard('{arrowright}'); - validateCellPosition(5, 8); + await validateCellPosition(5, 8); await userEvent.tab({ shift: true }); await userEvent.tab({ shift: true }); - validateCellPosition(14, 7); + await validateCellPosition(14, 7); await userEvent.tab(); - validateCellPosition(0, 8); + await validateCellPosition(0, 8); await userEvent.click(getCellsAtRowIndex(10)[11]); - validateCellPosition(11, 11); + await validateCellPosition(11, 11); await userEvent.tab(); - validateCellPosition(12, 11); + await validateCellPosition(12, 11); await userEvent.tab(); - validateCellPosition(0, 12); + await validateCellPosition(0, 12); await userEvent.tab({ shift: true }); - validateCellPosition(12, 11); + await validateCellPosition(12, 11); // bottom summary rows await userEvent.click(getCellsAtRowIndex(12)[6]); - validateCellPosition(6, 13); + await validateCellPosition(6, 13); await userEvent.keyboard('{arrowright}'); - validateCellPosition(7, 13); + await validateCellPosition(7, 13); await userEvent.keyboard('{arrowright}'); - validateCellPosition(9, 13); + await validateCellPosition(9, 13); await userEvent.keyboard('{arrowright}'); - validateCellPosition(10, 13); + await validateCellPosition(10, 13); await userEvent.keyboard('{arrowleft}{arrowleft}{arrowleft}'); - validateCellPosition(6, 13); + await validateCellPosition(6, 13); }); it('should scroll to the merged cell when selected', async () => { @@ -182,13 +182,13 @@ describe('colSpan', () => { testScrollIntoView(); await navigate(1); testScrollIntoView(); // should bring the merged cell into view - validateCellPosition(27, 11); + await validateCellPosition(27, 11); await navigate(7); testScrollIntoView(); - validateCellPosition(6, 12); // should navigate to the next row + await validateCellPosition(6, 12); // should navigate to the next row await navigate(7, true); testScrollIntoView(); - validateCellPosition(27, 11); // should navigate to the previous row + await validateCellPosition(27, 11); // should navigate to the previous row await navigate(27); testScrollIntoView(); await navigate(1); diff --git a/test/browser/column/grouping.test.ts b/test/browser/column/grouping.test.ts index dbcbe4ba20..fbfe6a5aa3 100644 --- a/test/browser/column/grouping.test.ts +++ b/test/browser/column/grouping.test.ts @@ -254,86 +254,86 @@ test('keyboard navigation', async () => { await expect.element(getSelectedCell()).not.toBeInTheDocument(); await tabIntoGrid(); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); // arrow navigation await userEvent.keyboard('{arrowup}'); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); await userEvent.keyboard('{arrowright}'); - validateCellPosition(1, 3); + await validateCellPosition(1, 3); await userEvent.keyboard('{arrowup}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await userEvent.keyboard('{arrowup}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); await userEvent.keyboard('{arrowright}{arrowright}'); - validateCellPosition(2, 3); + await validateCellPosition(2, 3); await userEvent.keyboard('{arrowup}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(1, 3); + await validateCellPosition(1, 3); await userEvent.keyboard('{arrowright}{arrowright}'); - validateCellPosition(3, 3); + await validateCellPosition(3, 3); await userEvent.keyboard('{arrowright}'); - validateCellPosition(4, 3); + await validateCellPosition(4, 3); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(4, 4); + await validateCellPosition(4, 4); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 3); + await validateCellPosition(4, 3); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 2); + await validateCellPosition(4, 2); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 1); + await validateCellPosition(4, 1); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 0); + await validateCellPosition(4, 0); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(4, 1); + await validateCellPosition(4, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(5, 3); + await validateCellPosition(5, 3); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(4, 3); + await validateCellPosition(4, 3); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 2); + await validateCellPosition(4, 2); await userEvent.keyboard('{arrowright}'); - validateCellPosition(5, 3); + await validateCellPosition(5, 3); await userEvent.keyboard('{arrowright}'); - validateCellPosition(6, 3); + await validateCellPosition(6, 3); await userEvent.keyboard('{arrowright}'); - validateCellPosition(7, 3); + await validateCellPosition(7, 3); await userEvent.keyboard('{arrowup}'); - validateCellPosition(7, 2); + await validateCellPosition(7, 2); await userEvent.keyboard('{arrowup}'); - validateCellPosition(4, 0); + await validateCellPosition(4, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(8, 0); + await validateCellPosition(8, 0); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(4, 0); + await validateCellPosition(4, 0); // home/end navigation await userEvent.keyboard('{home}'); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); await userEvent.keyboard('{end}'); - validateCellPosition(11, 3); + await validateCellPosition(11, 3); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(10, 3); + await validateCellPosition(10, 3); // tab navigation await userEvent.tab(); - validateCellPosition(11, 3); + await validateCellPosition(11, 3); await userEvent.tab({ shift: true }); await userEvent.tab({ shift: true }); await userEvent.tab({ shift: true }); - validateCellPosition(8, 3); + await validateCellPosition(8, 3); await userEvent.keyboard('{arrowup}'); await userEvent.tab({ shift: true }); - validateCellPosition(4, 0); + await validateCellPosition(4, 0); await userEvent.tab(); - validateCellPosition(8, 0); + await validateCellPosition(8, 0); await userEvent.keyboard('{home}{end}'); await userEvent.tab(); - validateCellPosition(0, 4); + await validateCellPosition(0, 4); await userEvent.tab({ shift: true }); - validateCellPosition(11, 3); + await validateCellPosition(11, 3); }); diff --git a/test/browser/column/name.test.tsx b/test/browser/column/name.test.tsx index b968471270..73b681120d 100644 --- a/test/browser/column/name.test.tsx +++ b/test/browser/column/name.test.tsx @@ -1,7 +1,7 @@ import type { Column } from '../../../src'; -import { getHeaderCells, setup } from '../utils'; +import { getHeaderCellsNew, setup } from '../utils'; -test('name is either a string or an element', () => { +test('name is either a string or an element', async () => { function Header() { return 'Fancy'; } @@ -18,7 +18,7 @@ test('name is either a string or an element', () => { ]; setup({ columns, rows: [] }); - const [cell1, cell2] = getHeaderCells(); - expect(cell1).toHaveTextContent('ID'); - expect(cell2).toHaveTextContent('Fancy'); + const [cell1, cell2] = getHeaderCellsNew('ID', 'Fancy'); + await expect.element(cell1).toBeVisible(); + await expect.element(cell2).toBeVisible(); }); diff --git a/test/browser/column/renderEditCell.test.tsx b/test/browser/column/renderEditCell.test.tsx index c4b9a77d7b..3462621fbb 100644 --- a/test/browser/column/renderEditCell.test.tsx +++ b/test/browser/column/renderEditCell.test.tsx @@ -4,7 +4,7 @@ import { commands, page, userEvent } from '@vitest/browser/context'; import { DataGrid } from '../../../src'; import type { Column, DataGridProps } from '../../../src'; -import { getCell, getCellsAtRowIndex, getGrid, getSelectedCell, scrollGrid } from '../utils'; +import { getCell, getCellsAtRowIndex, getGrid, getSelectedCell } from '../utils'; interface Row { col1: number; @@ -94,15 +94,19 @@ describe('Editor', () => { page.render(); await userEvent.click(getCellsAtRowIndex(0)[0]); - expect(getCellsAtRowIndex(0)).toHaveLength(2); - await scrollGrid({ scrollTop: 2000 }); - expect(getCellsAtRowIndex(0)).toHaveLength(1); + const selectedRowCells = page + .getByRole('row') + .filter({ has: getSelectedCell() }) + .getByRole('gridcell'); + await expect.poll(() => selectedRowCells.elements().length).toBe(2); + await commands.scrollGrid({ scrollTop: 2000 }); + await expect.poll(() => selectedRowCells.elements().length).toBe(1); const editor = page.getByRole('spinbutton', { name: 'col1-editor' }); await expect.element(editor).not.toBeInTheDocument(); expect(getGrid().element().scrollTop).toBe(2000); // TODO: await userEvent.keyboard('123'); fails in FF await userEvent.keyboard('{enter}123'); - expect(getCellsAtRowIndex(0)).toHaveLength(2); + await expect.poll(() => selectedRowCells.elements().length).toBe(2); await expect.element(editor).toHaveValue(123); expect(getGrid().element().scrollTop).toBe(0); }); diff --git a/test/browser/keyboardNavigation.test.tsx b/test/browser/keyboardNavigation.test.tsx index 1265e13b57..40f3fe5000 100644 --- a/test/browser/keyboardNavigation.test.tsx +++ b/test/browser/keyboardNavigation.test.tsx @@ -1,15 +1,8 @@ -import { page, userEvent } from '@vitest/browser/context'; +import { commands, page, userEvent } from '@vitest/browser/context'; import { DataGrid, SelectColumn } from '../../src'; import type { Column } from '../../src'; -import { - getCellsAtRowIndex, - getSelectedCell, - scrollGrid, - setup, - tabIntoGrid, - validateCellPosition -} from './utils'; +import { getSelectedCell, setup, tabIntoGrid, validateCellPosition } from './utils'; type Row = undefined; @@ -35,72 +28,72 @@ test('keyboard navigation', async () => { // tab into the grid await tabIntoGrid(); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); // tab to the next cell await userEvent.tab(); - validateCellPosition(1, 0); + await validateCellPosition(1, 0); // tab back to the previous cell await userEvent.tab({ shift: true }); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); // arrow navigation await userEvent.keyboard('{arrowdown}'); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(1, 1); + await validateCellPosition(1, 1); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(0, 2); + await validateCellPosition(0, 2); await userEvent.keyboard('{arrowup}'); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); await userEvent.keyboard('{arrowup}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); // page {up,down} await userEvent.keyboard('{PageDown}'); - validateCellPosition(0, 26); + await validateCellPosition(0, 26); await userEvent.keyboard('{PageDown}'); - validateCellPosition(0, 52); + await validateCellPosition(0, 52); await userEvent.keyboard('{PageUp}'); - validateCellPosition(0, 26); + await validateCellPosition(0, 26); // home/end navigation await userEvent.keyboard('{end}'); - validateCellPosition(6, 26); + await validateCellPosition(6, 26); await userEvent.keyboard('{home}'); - validateCellPosition(0, 26); + await validateCellPosition(0, 26); await userEvent.keyboard('{Control>}{end}{/Control}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{arrowright}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{end}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{Control>}{end}{/Control}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{PageDown}'); - validateCellPosition(6, 103); + await validateCellPosition(6, 103); await userEvent.keyboard('{Control>}{home}{/Control}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{home}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{Control>}{home}{/Control}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{PageUp}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); // tab at the end of a row selects the first cell on the next row await userEvent.keyboard('{end}'); await userEvent.tab(); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); // shift tab should select the last cell of the previous row await userEvent.tab({ shift: true }); - validateCellPosition(6, 0); + await validateCellPosition(6, 0); }); test('arrow and tab navigation', async () => { @@ -109,23 +102,23 @@ test('arrow and tab navigation', async () => { // pressing arrowleft on the leftmost cell does nothing await tabIntoGrid(); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); // pressing arrowright on the rightmost cell does nothing await userEvent.keyboard('{end}'); - validateCellPosition(6, 1); + await validateCellPosition(6, 1); await userEvent.keyboard('{arrowright}'); - validateCellPosition(6, 1); + await validateCellPosition(6, 1); // pressing tab on the rightmost cell navigates to the leftmost cell on the next row await userEvent.tab(); - validateCellPosition(0, 2); + await validateCellPosition(0, 2); // pressing shift+tab on the leftmost cell navigates to the rightmost cell on the previous row await userEvent.tab({ shift: true }); - validateCellPosition(6, 1); + await validateCellPosition(6, 1); }); test('grid enter/exit', async () => { @@ -139,29 +132,29 @@ test('grid enter/exit', async () => { // tab into the grid await tabIntoGrid(); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); // shift+tab tabs out of the grid if we are at the first cell await userEvent.tab({ shift: true }); await expect.element(beforeButton).toHaveFocus(); await userEvent.tab(); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{arrowdown}{arrowdown}'); - validateCellPosition(0, 2); + await validateCellPosition(0, 2); // tab should select the last selected cell // click outside the grid await userEvent.click(beforeButton); await userEvent.tab(); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); // shift+tab should select the last selected cell await userEvent.click(afterButton); await userEvent.tab({ shift: true }); - validateCellPosition(0, 3); + await validateCellPosition(0, 3); await expect.element(getSelectedCell().getByRole('checkbox')).toHaveFocus(); // tab tabs out of the grid if we are at the last cell @@ -174,109 +167,113 @@ test('navigation with focusable cell renderer', async () => { setup({ columns, rows: new Array(1), bottomSummaryRows }, true); await tabIntoGrid(); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); // cell should not set tabIndex to 0 if it contains a focusable cell renderer await expect.element(getSelectedCell()).toHaveAttribute('tabIndex', '-1'); - const checkbox = getSelectedCell().getByRole('checkbox').element(); - expect(checkbox).toHaveFocus(); - expect(checkbox).toHaveAttribute('tabIndex', '0'); + const checkbox = getSelectedCell().getByRole('checkbox'); + await expect.element(checkbox).toHaveFocus(); + await expect.element(checkbox).toHaveAttribute('tabIndex', '0'); await userEvent.tab(); - validateCellPosition(1, 1); + await validateCellPosition(1, 1); // cell should set tabIndex to 0 if it does not have focusable cell renderer await expect.element(getSelectedCell()).toHaveAttribute('tabIndex', '0'); }); test('navigation when header and summary rows have focusable elements', async () => { - const columns: readonly Column[] = [ + const columns: readonly Column[] = [ { key: 'col2', name: 'col2', renderHeaderCell(p) { - return ; + return ; }, renderSummaryCell(p) { - return ; + return ; } }, { key: 'col3', name: 'col3', renderHeaderCell(p) { - return ; + return ; }, renderSummaryCell(p) { - return ; + return ; } } ]; - setup({ columns, rows: new Array(2), bottomSummaryRows }, true); + setup({ columns, rows: new Array(2), bottomSummaryRows: [1, 2] }, true); await tabIntoGrid(); // should set focus on the header filter - expect(document.getElementById('header-filter1')).toHaveFocus(); + await expect.element(page.getByTestId('header-filter1')).toHaveFocus(); await userEvent.tab(); - expect(document.getElementById('header-filter2')).toHaveFocus(); + await expect.element(page.getByTestId('header-filter2')).toHaveFocus(); await userEvent.tab(); - validateCellPosition(0, 1); + await validateCellPosition(0, 1); await userEvent.tab({ shift: true }); - expect(document.getElementById('header-filter2')).toHaveFocus(); + await expect.element(page.getByTestId('header-filter2')).toHaveFocus(); await userEvent.tab({ shift: true }); - expect(document.getElementById('header-filter1')).toHaveFocus(); + await expect.element(page.getByTestId('header-filter1')).toHaveFocus(); await userEvent.tab(); await userEvent.tab(); await userEvent.keyboard('{Control>}{end}{/Control}{arrowup}{arrowup}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await userEvent.tab(); - expect(document.getElementById('summary-1')).toHaveFocus(); + await expect.element(page.getByTestId('summary-col2-1')).toHaveFocus(); await userEvent.tab(); - expect(document.getElementById('summary-2')).toHaveFocus(); + await expect.element(page.getByTestId('summary-col3-1')).toHaveFocus(); await userEvent.tab({ shift: true }); await userEvent.tab({ shift: true }); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); await expect.element(getSelectedCell()).toHaveFocus(); }); test('navigation when selected cell not in the viewport', async () => { const columns: Column[] = [SelectColumn]; + const selectedRowCells = page + .getByRole('row') + .filter({ has: getSelectedCell() }) + .getByRole('gridcell'); for (let i = 0; i < 99; i++) { columns.push({ key: `col${i}`, name: `col${i}`, frozen: i < 5 }); } setup({ columns, rows, bottomSummaryRows }, true); await tabIntoGrid(); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{Control>}{end}{/Control}{arrowup}{arrowup}'); - validateCellPosition(99, 100); - expect(getCellsAtRowIndex(100)).not.toHaveLength(1); - - await scrollGrid({ scrollTop: 0 }); - expect(getCellsAtRowIndex(99)).toHaveLength(1); + await validateCellPosition(99, 100); + // TODO: replace with `toHaveLength` when migrating to v4 + await expect.poll(() => selectedRowCells.elements().length).not.toBe(1); + await commands.scrollGrid({ scrollTop: 0 }); + await expect.poll(() => selectedRowCells.elements().length).toBe(1); await userEvent.keyboard('{arrowup}'); - validateCellPosition(99, 99); - expect(getCellsAtRowIndex(99)).not.toHaveLength(1); + await validateCellPosition(99, 99); + await expect.poll(() => selectedRowCells.elements().length).not.toBe(1); - await scrollGrid({ scrollLeft: 0 }); + await commands.scrollGrid({ scrollLeft: 0 }); await userEvent.keyboard('{arrowdown}'); - validateCellPosition(99, 100); + await validateCellPosition(99, 100); await userEvent.keyboard( '{home}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}' ); - validateCellPosition(7, 100); - await scrollGrid({ scrollLeft: 2000 }); + await validateCellPosition(7, 100); + await commands.scrollGrid({ scrollLeft: 2000 }); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(6, 100); + await validateCellPosition(6, 100); }); test('reset selected cell when column is removed', async () => { @@ -294,7 +291,7 @@ test('reset selected cell when column is removed', async () => { await userEvent.tab(); await userEvent.keyboard('{arrowdown}{arrowright}'); - validateCellPosition(1, 1); + await validateCellPosition(1, 1); rerender(); @@ -316,7 +313,7 @@ test('reset selected cell when row is removed', async () => { await userEvent.tab(); await userEvent.keyboard('{arrowdown}{arrowdown}{arrowright}'); - validateCellPosition(1, 2); + await validateCellPosition(1, 2); rerender(); @@ -326,15 +323,15 @@ test('reset selected cell when row is removed', async () => { test('should not change the left and right arrow behavior for right to left languages', async () => { setup({ rows, columns, direction: 'rtl' }, true); await tabIntoGrid(); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.tab(); - validateCellPosition(1, 0); + await validateCellPosition(1, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{arrowright}'); - validateCellPosition(0, 0); + await validateCellPosition(0, 0); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(1, 0); + await validateCellPosition(1, 0); await userEvent.keyboard('{arrowleft}'); - validateCellPosition(2, 0); + await validateCellPosition(2, 0); }); diff --git a/test/browser/rowSelection.test.tsx b/test/browser/rowSelection.test.tsx index d0f6be5684..ed9462dfea 100644 --- a/test/browser/rowSelection.test.tsx +++ b/test/browser/rowSelection.test.tsx @@ -3,7 +3,7 @@ import { page, userEvent } from '@vitest/browser/context'; import { DataGrid, SelectColumn } from '../../src'; import type { Column } from '../../src'; -import { getCell, getRow, getSelectAllCheckbox } from './utils'; +import { getCell, getSelectAllCheckbox } from './utils'; interface Row { id: number; @@ -48,14 +48,18 @@ function setup(initialRows = defaultRows) { page.render(); } +function getRowByText(rowIdx: number) { + return page.getByRole('row').filter({ has: getCell(String(rowIdx + 1)) }); +} + function testSelection(rowIdx: number, isSelected: boolean) { return expect - .element(getRow(String(rowIdx + 1))) + .element(getRowByText(rowIdx)) .toHaveAttribute('aria-selected', isSelected ? 'true' : 'false'); } async function toggleSelection(rowIdx: number, shift = false, force = false) { - const checkbox = getRow(String(rowIdx + 1)).getByRole('checkbox', { name: 'Select' }); + const checkbox = getRowByText(rowIdx).getByRole('checkbox', { name: 'Select' }); if (shift) await userEvent.keyboard('{Shift>}'); await userEvent.click(checkbox, { force }); if (shift) await userEvent.keyboard('{/Shift}'); @@ -77,7 +81,7 @@ test('toggle selection when checkbox is clicked', async () => { test('toggle selection using keyboard', async () => { setup(); await testSelection(0, false); - await userEvent.click(getRow('1').getByRole('checkbox', { name: 'Select' })); + await userEvent.click(getRowByText(0).getByRole('checkbox', { name: 'Select' })); await testSelection(0, true); await userEvent.keyboard(' '); await testSelection(0, false); diff --git a/test/browser/utils.tsx b/test/browser/utils.tsx index 00b0b589b7..512d1a940d 100644 --- a/test/browser/utils.tsx +++ b/test/browser/utils.tsx @@ -49,7 +49,7 @@ export function getHeaderCellsNew(...names: readonly string[]) { } export function getRow(name: string) { - return page.getByRole('row', { name }); + return page.getByRole('row', { name, exact: true }); } export function getRowsNew(...names: readonly string[]) { @@ -93,10 +93,12 @@ export function getSelectedCell() { .first(); } -export function validateCellPosition(columnIdx: number, rowIdx: number) { - const cell = getSelectedCell().element(); - expect(cell).toHaveAttribute('aria-colindex', `${columnIdx + 1}`); - expect(cell.parentNode).toHaveAttribute('aria-rowindex', `${rowIdx + 1}`); +export async function validateCellPosition(columnIdx: number, rowIdx: number) { + const cell = getSelectedCell(); + await expect.element(cell).toHaveAttribute('aria-colindex', `${columnIdx + 1}`); + await expect + .element(page.getByRole('row').filter({ has: cell })) + .toHaveAttribute('aria-rowindex', `${rowIdx + 1}`); } export async function scrollGrid({