Skip to content

Commit c7a24d1

Browse files
Tests: simplify scrollGrid (#4016)
* Tests: simplify `scrollGrid` * fix lints --------- Co-authored-by: Aman Mahajan <amahajan@stratag.com>
1 parent eabd151 commit c7a24d1

4 files changed

Lines changed: 37 additions & 35 deletions

File tree

test/browser/column/renderEditCell.test.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo, useState } from 'react';
22
import { createPortal } from 'react-dom';
3-
import { page, userEvent } from 'vitest/browser';
3+
import { page, server, userEvent } from 'vitest/browser';
44

55
import { DataGrid } from '../../../src';
66
import type { Column, DataGridProps } from '../../../src';
@@ -96,7 +96,7 @@ describe('Editor', () => {
9696
await userEvent.click(getCellsAtRowIndex(0).nth(0));
9797
const activeRowCells = getRowWithCell(page.getActiveCell()).getCell();
9898
await testCount(activeRowCells, 2);
99-
await scrollGrid({ top: 2001 });
99+
scrollGrid({ top: 2001 });
100100
await testCount(activeRowCells, 1);
101101
await expect.element(col1Editor).not.toBeInTheDocument();
102102
await expect.element(grid).toHaveProperty('scrollTop', 2001);
@@ -258,11 +258,19 @@ describe('Editor', () => {
258258

259259
await userEvent.dblClick(page.getCell({ name: 'name0' }));
260260
await userEvent.keyboard('abc');
261-
262-
await scrollGrid({ top: 1500 });
261+
if (server.browser === 'firefox') {
262+
// When typing, Firefox scroll to the caret asynchronously,
263+
// but does not to cancel the scroll task when calling `.scroll()` on the grid
264+
// https://github.com/mozilla-firefox/firefox/blob/287c6cf323492ae0cc031e468c1d87f623413f50/dom/html/TextControlElement.cpp#L330
265+
// https://github.com/mozilla-firefox/firefox/blob/287c6cf323492ae0cc031e468c1d87f623413f50/dom/base/Selection.cpp#L3828
266+
await new Promise(requestAnimationFrame);
267+
// Alternatively, configuring playwright's launchOptions.slowMo to 1 works,
268+
// but slows down the tests.
269+
}
270+
scrollGrid({ top: 1500 });
263271
await userEvent.click(page.getCell({ name: 'name43' }));
264272
await expect.element(page.getActiveCell()).toHaveTextContent(/^name43$/);
265-
await scrollGrid({ top: 0 });
273+
scrollGrid({ top: 0 });
266274
await expect.element(page.getCell({ name: 'name0abc' })).toBeVisible();
267275
});
268276

test/browser/keyboardNavigation.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,21 @@ test('navigation when active cell not in the viewport', async () => {
279279
await userEvent.keyboard('{Control>}{end}{/Control}{arrowup}{arrowup}');
280280
await validateCellPosition(99, 100);
281281
await expect.element(activeRowCells).not.toHaveLength(1);
282-
await scrollGrid({ top: 0 });
282+
scrollGrid({ top: 0 });
283283
await testCount(activeRowCells, 1);
284284
await userEvent.keyboard('{arrowup}');
285285
await validateCellPosition(99, 99);
286286
await expect.element(activeRowCells).not.toHaveLength(1);
287287

288-
await scrollGrid({ left: 0 });
288+
scrollGrid({ left: 0 });
289289
await userEvent.keyboard('{arrowdown}');
290290
await validateCellPosition(99, 100);
291291

292292
await userEvent.keyboard(
293293
'{home}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}{arrowright}'
294294
);
295295
await validateCellPosition(7, 100);
296-
await scrollGrid({ left: 2000 });
296+
scrollGrid({ left: 2000 });
297297
await userEvent.keyboard('{arrowleft}');
298298
await validateCellPosition(6, 100);
299299
});

test/browser/utils.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ export async function validateCellPosition(columnIdx: number, rowIdx: number) {
2525
await expect.element(row).toHaveAttribute('aria-rowindex', `${rowIdx + 1}`);
2626
}
2727

28-
export async function scrollGrid(options: ScrollToOptions) {
29-
await new Promise((resolve) => {
30-
// wait for browser state to stablize before scrolling, to avoid flaky scroll-related tests
31-
requestAnimationFrame(() => {
32-
const gridElement = page.getGrid().element();
33-
gridElement.addEventListener('scrollend', resolve, { once: true });
34-
gridElement.scroll(options);
35-
});
36-
});
28+
export function scrollGrid(options: ScrollToOptions) {
29+
const grid = page.getGrid().element();
30+
grid.scroll(options);
3731
}
3832

3933
export function testCount(locator: Locator, expectedCount: number) {

test/browser/virtualization.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,50 +102,50 @@ test('virtualization is enabled', async () => {
102102
await assertHeaderCells(18, 0, 17);
103103
await assertRows(34, 0, 33);
104104
await assertCells(0, 18, 0, 17);
105-
await scrollGrid({ top: 244 });
105+
scrollGrid({ top: 244 });
106106
await assertRows(39, 2, 40);
107107

108-
await scrollGrid({ top: 245 });
108+
scrollGrid({ top: 245 });
109109
await assertRows(38, 3, 40);
110110

111-
await scrollGrid({ top: 419 });
111+
scrollGrid({ top: 419 });
112112
await assertRows(39, 7, 45);
113113

114-
await scrollGrid({ top: 420 });
114+
scrollGrid({ top: 420 });
115115
await assertRows(38, 8, 45);
116116

117-
await scrollGrid({ top: 524 });
117+
scrollGrid({ top: 524 });
118118
await assertRows(39, 10, 48);
119119

120-
await scrollGrid({ top: 525 });
120+
scrollGrid({ top: 525 });
121121
await assertRows(38, 11, 48);
122122

123-
await scrollGrid({ top: 1000 });
123+
scrollGrid({ top: 1000 });
124124
await assertRows(39, 24, 62);
125125

126126
// scroll height = header height + row height * row count
127127
// max top = scroll height - grid height
128-
await scrollGrid({ top: rowHeight + rowHeight * 100 - 1080 });
128+
scrollGrid({ top: rowHeight + rowHeight * 100 - 1080 });
129129
await assertRows(34, 66, 99);
130130

131-
await scrollGrid({ left: 92 });
131+
scrollGrid({ left: 92 });
132132
await assertHeaderCells(18, 0, 17);
133133
await assertCells(66, 18, 0, 17);
134134

135-
await scrollGrid({ left: 93 });
135+
scrollGrid({ left: 93 });
136136
await assertHeaderCells(19, 0, 18);
137137
await assertCells(66, 19, 0, 18);
138138

139-
await scrollGrid({ left: 209 });
139+
scrollGrid({ left: 209 });
140140
await assertHeaderCells(19, 0, 18);
141141
await assertCells(66, 19, 0, 18);
142142

143-
await scrollGrid({ left: 210 });
143+
scrollGrid({ left: 210 });
144144
await assertHeaderCells(18, 1, 18);
145145
await assertCells(66, 18, 1, 18);
146146

147147
// max left = row width - grid width
148-
await scrollGrid({ left: 3600 - 1920 });
148+
scrollGrid({ left: 3600 - 1920 });
149149
await assertHeaderCells(17, 13, 29);
150150
await assertCells(66, 17, 13, 29);
151151
});
@@ -157,13 +157,13 @@ test('virtualization is enabled with 4 frozen columns', async () => {
157157
await assertHeaderCellIndexes(indexes);
158158
await assertCellIndexes(0, indexes);
159159

160-
await scrollGrid({ left: 1000 });
160+
scrollGrid({ left: 1000 });
161161
indexes = [0, 1, 2, 3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
162162
await assertHeaderCellIndexes(indexes);
163163
await assertCellIndexes(0, indexes);
164164

165165
// max left = row width - grid width
166-
await scrollGrid({ left: 3600 - 1920 });
166+
scrollGrid({ left: 3600 - 1920 });
167167
indexes = [0, 1, 2, 3, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
168168
await assertHeaderCellIndexes(indexes);
169169
await assertCellIndexes(0, indexes);
@@ -179,12 +179,12 @@ test('virtualization is enabled with all columns frozen', async () => {
179179
await assertHeaderCellIndexes(indexes);
180180
await assertCellIndexes(0, indexes);
181181

182-
await scrollGrid({ left: 1000 });
182+
scrollGrid({ left: 1000 });
183183
await assertHeaderCellIndexes(indexes);
184184
await assertCellIndexes(0, indexes);
185185

186186
// max left = row width - grid width
187-
await scrollGrid({ left: 3600 - 1920 });
187+
scrollGrid({ left: 3600 - 1920 });
188188
await assertHeaderCellIndexes(indexes);
189189
await assertCellIndexes(0, indexes);
190190
});
@@ -197,7 +197,7 @@ test('virtualization is enabled with 2 summary rows', async () => {
197197
26, 27, 28, 29, 30, 31, 102, 103
198198
]);
199199

200-
await scrollGrid({ top: 1000 });
200+
scrollGrid({ top: 1000 });
201201
await assertRowIndexes([
202202
0, 1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
203203
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 102, 103

0 commit comments

Comments
 (0)