-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
355 lines (306 loc) · 12.6 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { expect } from 'chai';
import * as playwright from 'playwright';
function sleep(timeoutMS: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => resolve(), timeoutMS);
});
}
// A simplified version of https://github.com/testing-library/dom-testing-library/blob/main/src/wait-for.js
function waitFor(callback: () => Promise<void>): Promise<void> {
return new Promise((resolve, reject) => {
let intervalId: NodeJS.Timer | null = null;
let timeoutId: NodeJS.Timer | null = null;
let lastError: any = null;
function handleTimeout() {
clearTimeout(timeoutId!);
clearInterval(intervalId!);
reject(lastError);
}
async function checkCallback() {
try {
await callback();
clearTimeout(timeoutId!);
clearInterval(intervalId!);
resolve();
} catch (error) {
lastError = error;
}
}
timeoutId = setTimeout(handleTimeout, 1000);
intervalId = setTimeout(checkCallback, 50);
});
}
/**
* Attempts page.goto with retries
*
* @remarks The server and runner can be started up simultaneously
* @param page
* @param url
*/
async function attemptGoto(page: playwright.Page, url: string): Promise<boolean> {
const maxAttempts = 10;
const retryTimeoutMS = 250;
let didNavigate = false;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
try {
// eslint-disable-next-line no-await-in-loop
await page.goto(url);
didNavigate = true;
} catch (error) {
// eslint-disable-next-line no-await-in-loop
await sleep(retryTimeoutMS);
}
}
return didNavigate;
}
describe('e2e', () => {
const baseUrl = 'http://localhost:5001';
let browser: playwright.Browser;
let page: playwright.Page;
async function renderFixture(fixturePath: string) {
await page.goto(`${baseUrl}/e2e/${fixturePath}#no-dev`);
}
before(async function beforeHook() {
this.timeout(20000);
browser = await playwright.chromium.launch({
headless: true,
});
page = await browser.newPage();
const isServerRunning = await attemptGoto(page, `${baseUrl}#no-dev`);
if (!isServerRunning) {
throw new Error(
`Unable to navigate to ${baseUrl} after multiple attempts. Did you forget to run \`yarn test:e2e:server\` and \`yarn test:e2e:build\`?`,
);
}
});
after(async () => {
await browser.close();
});
describe('<DataGrid />', () => {
it('should select the first column header when pressing tab key', async () => {
await renderFixture('DataGrid/KeyboardNavigationFocus');
const button = page.locator('text=initial focus');
expect(await button.evaluate((node) => document.activeElement === node));
await page.keyboard.press('Tab');
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('brand');
});
it('should implement the roving tabindex pattern', async () => {
await renderFixture('DataGrid/KeyboardNavigationFocus');
const button = page.locator('text=initial focus');
expect(await button.evaluate((node) => document.activeElement === node));
await page.keyboard.press('Tab');
await waitFor(async () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('brand');
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'columnheader',
);
});
await page.keyboard.press('ArrowDown');
await waitFor(async () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('Nike');
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'cell',
);
});
await page.keyboard.press('ArrowDown');
await waitFor(async () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('Adidas');
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'cell',
);
});
await page.keyboard.press('Tab');
await waitFor(async () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('100');
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'button',
);
});
await page.keyboard.press('Shift+Tab');
await waitFor(async () => {
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('Adidas');
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'cell',
);
});
await page.keyboard.press('Shift+Tab');
await waitFor(async () => {
expect(
await page.evaluate(() => document.activeElement?.getAttribute('data-testid')),
).to.equal('initial-focus');
});
});
it('should display the rows', async () => {
await renderFixture('DataGrid/ConcurrentReactUpdate');
const cells = page.locator('[role="cell"]');
await cells.first().waitFor();
expect(await cells.allTextContents()).to.deep.equal(['1', '2']);
});
it('should work with a select as the edit cell', async () => {
await renderFixture('DataGrid/SelectEditCell');
await page.dblclick('"Nike"');
await page.click('"Gucci"');
expect(
await page.evaluate(() => {
const selector = '[role="row"][data-rowindex="0"] [role="cell"][data-colindex="0"]';
return document.querySelector(selector)!.textContent!;
}),
).to.equal('Gucci');
});
it('should reorder columns by dropping into the header', async () => {
await renderFixture('DataGrid/ColumnReorder');
expect(await page.locator('[role="row"]').first().textContent()).to.equal('brandyear');
const brand = await page.$('[role="columnheader"][aria-colindex="1"] > [draggable]');
const brandBoundingBox = await brand?.boundingBox();
const year = await page.$('[role="columnheader"][aria-colindex="2"] > [draggable]');
const yearBoundingBox = await year?.boundingBox();
if (brandBoundingBox && yearBoundingBox) {
// Based on https://stackoverflow.com/a/64746679/2801714
await page.mouse.move(
brandBoundingBox.x + brandBoundingBox.width / 2,
brandBoundingBox.y + brandBoundingBox.height / 2,
{ steps: 5 },
);
await page.mouse.down();
await page.mouse.move(
yearBoundingBox.x + yearBoundingBox.width / 2,
yearBoundingBox.y + yearBoundingBox.height / 2,
{ steps: 5 },
);
await page.mouse.up();
}
expect(
await page.evaluate(() => document.querySelector('[role="row"]')!.textContent!),
).to.equal('yearbrand');
});
it('should reorder columns by dropping into the body', async () => {
await renderFixture('DataGrid/ColumnReorder');
expect(await page.locator('[role="row"]').first().textContent()).to.equal('brandyear');
const brand = await page.$('[role="columnheader"][aria-colindex="1"] > [draggable]');
const brandBoundingBox = await brand?.boundingBox();
const cell = await page.$('[role="row"][data-rowindex="0"] [role="cell"][data-colindex="1"]');
const cellBoundingBox = await cell?.boundingBox();
if (brandBoundingBox && cellBoundingBox) {
// Based on https://stackoverflow.com/a/64746679/2801714
await page.mouse.move(
brandBoundingBox.x + brandBoundingBox.width / 2,
brandBoundingBox.y + brandBoundingBox.height / 2,
{ steps: 5 },
);
await page.mouse.down();
await page.mouse.move(
cellBoundingBox.x + cellBoundingBox.width / 2,
cellBoundingBox.y + cellBoundingBox.height / 2,
{ steps: 5 },
);
await page.mouse.up();
}
expect(await page.locator('[role="row"]').first().textContent()).to.equal('yearbrand');
});
it('should select one row', async () => {
await renderFixture('DataGrid/CheckboxSelection');
await page.click('[role="row"][data-rowindex="0"] [role="cell"] input');
expect(
await page.evaluate(
() => document.querySelector('[role="row"][data-rowindex="0"]')!.className!,
),
).to.contain('Mui-selected');
});
it('should not scroll when changing the selected row', async () => {
await renderFixture('DataGrid/RowSelection');
await page.click('[role="row"][data-rowindex="0"] [role="cell"]');
await page.evaluate(() =>
document.querySelector('[role="row"][data-rowindex="3"] [role="cell"]')!.scrollIntoView(),
);
const scrollTop = await page.evaluate(
() => document.querySelector('.MuiDataGrid-virtualScroller')!.scrollTop!,
);
expect(scrollTop).not.to.equal(0);
await page.click('[role="row"][data-rowindex="3"] [role="cell"]');
expect(
await page.evaluate(
() => document.querySelector('.MuiDataGrid-virtualScroller')!.scrollTop!,
),
).to.equal(scrollTop);
});
it('should edit date cells', async () => {
await renderFixture('DataGrid/KeyboardEditDate');
// Edit date column
expect(await page.locator('[role="cell"][data-field="birthday"]').textContent()).to.equal(
'2/29/1984',
);
// set 06/25/1986
await page.dblclick('[role="cell"][data-field="birthday"]');
await page.type('[role="cell"][data-field="birthday"] input', '06251986');
await page.keyboard.press('Enter');
expect(await page.locator('[role="cell"][data-field="birthday"]').textContent()).to.equal(
'6/25/1986',
);
// Edit dateTime column
expect(
await page.locator('[role="cell"][data-field="lastConnection"]').textContent(),
).to.equal('2/20/2022, 6:50:00 AM');
// start editing lastConnection
await page.keyboard.press('ArrowRight');
await page.keyboard.press('Enter');
// set 01/31/2025 16:05
await page.type('[role="cell"][data-field="lastConnection"] input', '01312025165');
await page.keyboard.press('Enter');
expect(
await page.locator('[role="cell"][data-field="lastConnection"]').textContent(),
).to.equal('1/31/2025, 4:05:00 PM');
});
// https://github.com/mui/mui-x/issues/3613
it('should not lose cell focus when scrolling with arrow down', async () => {
await renderFixture('DataGridPro/KeyboardNavigationFocus');
async function keyDown() {
await page.keyboard.down('ArrowDown');
// wait between keydown events for virtualization
await sleep(100);
}
const button = page.locator('text=initial focus');
expect(await button.evaluate((node) => document.activeElement === node));
await page.keyboard.down('Tab');
await keyDown(); // 0
await keyDown(); // 1
await keyDown(); // 2
await keyDown(); // 3
expect(await page.evaluate(() => document.activeElement?.getAttribute('role'))).to.equal(
'cell',
'Expected cell to be focused',
);
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('3');
});
// https://github.com/mui/mui-x/issues/3795#issuecomment-1025628771
it('should allow horizontal scroll when there are more columns and no rows', async () => {
await renderFixture('DataGrid/EmptyGrid');
await page.mouse.move(150, 150);
await page.mouse.wheel(50, 0);
await sleep(50);
const scrollLeft = await page.evaluate(() => {
return document.querySelector('.MuiDataGrid-virtualScroller')!.scrollLeft;
});
expect(scrollLeft).not.to.equal(0);
});
// https://github.com/mui/mui-x/issues/4190
it('should move the focus from left pinned column to the cell in main render zone after pressing Tab during row editing', async () => {
await renderFixture('DataGridPro/RowEditingWithPinnedColumns');
await page.dblclick('[role="cell"][data-field="column0"]');
await page.keyboard.down('Tab');
expect(
await page.evaluate(() => (document.activeElement as HTMLInputElement).value),
).to.equal('0-1');
});
it('should start editing a cell when a printable char is pressed', async () => {
await renderFixture('DataGrid/KeyboardEditInput');
expect(await page.locator('[role="cell"][data-field="brand"]').textContent()).to.equal(
'Nike',
);
await page.click('[role="cell"][data-field="brand"]');
await page.type('[role="cell"][data-field="brand"]', 'p');
expect(await page.locator('[role="cell"][data-field="brand"] input').inputValue()).to.equal(
'p',
);
});
});
});