Skip to content

Commit ef7e808

Browse files
author
Enzo M
committed
Added more tab order tests
1 parent a909c29 commit ef7e808

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

test/griddle-grid.test.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { html } from 'lit'
44
import { page, userEvent } from '@vitest/browser/context';
55
import '../src/index';
66

7-
test('renders grid with text cells', async () => {
8-
const screen = render(html`
7+
const baseTemplate = html`
98
<gdl-data-grid>
109
<gdl-grid-column slot="headers">First Name</gdl-grid-column>
1110
<gdl-grid-column slot="headers">Last Name</gdl-grid-column>
@@ -21,7 +20,10 @@ test('renders grid with text cells', async () => {
2120
<gdl-grid-cell>True</gdl-grid-cell>
2221
</gdl-grid-row>
2322
</gdl-data-grid>
24-
`);
23+
`;
24+
25+
test('renders grid with text cells', async () => {
26+
const screen = render(baseTemplate);
2527

2628
const element = screen.getByRole('grid');
2729
await expect.element(element).toBeInTheDocument();
@@ -30,33 +32,33 @@ test('renders grid with text cells', async () => {
3032
});
3133

3234
test('Tabs to grid at first', async () => {
33-
const screen = page.render(html`
34-
<gdl-data-grid>
35-
<gdl-grid-column slot="headers">First Name</gdl-grid-column>
36-
<gdl-grid-column slot="headers">Last Name</gdl-grid-column>
37-
<gdl-grid-column slot="headers">Is Manager</gdl-grid-column>
38-
<gdl-grid-row>
39-
<gdl-grid-cell>John</gdl-grid-cell>
40-
<gdl-grid-cell>Doe</gdl-grid-cell>
41-
<gdl-grid-cell>False</gdl-grid-cell>
42-
</gdl-grid-row>
43-
<gdl-grid-row>
44-
<gdl-grid-cell>Mary</gdl-grid-cell>
45-
<gdl-grid-cell>Jane</gdl-grid-cell>
46-
<gdl-grid-cell>True</gdl-grid-cell>
47-
</gdl-grid-row>
48-
</gdl-data-grid>
49-
`);
50-
console.debug('Base Element: ', screen.baseElement.tagName);
51-
console.debug('Container: ', screen.container);
52-
screen.container.focus();
35+
const screen = page.render(baseTemplate);
5336
const gridLocator = screen.getByRole('grid');
54-
let grid = gridLocator.element() as HTMLElement;
55-
grid.focus();
56-
console.debug(` Before tabbing: ${document.activeElement}`)
37+
// Fixes the focus not being in the iframe when Tab key events are fired.
38+
// Might not be a problem in headless mode.
39+
await gridLocator.click();
40+
await userEvent.tab({shift:true});
41+
// Sets us up for actually testing the tab order.
42+
// Tab from the body element to the data grid.
5743
await userEvent.tab();
58-
console.debug(`After tabbing: ${document.activeElement}`)
59-
await userEvent.tab({shift: true});
60-
await expect.element(gridLocator, {timeout: 1000}).toHaveFocus();
44+
await expect.element(gridLocator).toHaveFocus();
45+
46+
});
47+
48+
test('Tabs to first cell of the grid from focus on the grid', async () => {
49+
const screen = page.render(baseTemplate);
50+
const gridLocator = screen.getByRole('grid');
51+
// Fixes the focus not being in the iframe when Tab key events are fired.
52+
// Might not be a problem in headless mode.
53+
await gridLocator.click();
54+
await userEvent.tab({shift:true});
55+
// Sets us up for actually testing the tab order.
56+
const firstCell = screen.getByRole('gridcell').first();
57+
// Tab from the body element to the data grid.
58+
await userEvent.tab();
59+
await expect.element(gridLocator).toHaveFocus();
6160

62-
});
61+
// Tab to the first available cell of the grid.
62+
await userEvent.tab();
63+
await expect.element(firstCell, {timeout: 1000}).toHaveFocus();
64+
})

0 commit comments

Comments
 (0)