-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathsetupBrowser.ts
More file actions
100 lines (82 loc) · 2.56 KB
/
setupBrowser.ts
File metadata and controls
100 lines (82 loc) · 2.56 KB
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
// vitest-browser-react also automatically injects render method on the page
// need to import it so TypeScript can pick up types
import 'vitest-browser-react';
import { configure } from 'vitest-browser-react/pure';
import { locators, userEvent, type Locator, type LocatorByRoleOptions } from 'vitest/browser';
configure({
reactStrictMode: true
});
declare module 'vitest/browser' {
interface LocatorSelectors {
getGrid: () => Locator;
getTreeGrid: () => Locator;
getHeaderRow: (opts?: LocatorByRoleOptions) => Locator;
getHeaderCell: (opts?: LocatorByRoleOptions) => Locator;
getRow: (opts?: LocatorByRoleOptions) => Locator;
getCell: (opts?: LocatorByRoleOptions) => Locator;
getSelectAllCheckbox: () => Locator;
getActiveCell: () => Locator;
getDragHandle: () => Locator;
getBySelector: (selector: string) => Locator;
}
}
locators.extend({
getGrid() {
return this.getByRole('grid');
},
getTreeGrid() {
return this.getByRole('treegrid');
},
getHeaderRow(opts?: LocatorByRoleOptions) {
return this.getByRole('row', defaultToExactOpts(opts)).and(
this.getBySelector('.rdg-header-row')
);
},
getHeaderCell(opts?: LocatorByRoleOptions) {
return this.getByRole('columnheader', defaultToExactOpts(opts));
},
getRow(opts?: LocatorByRoleOptions) {
return this.getByRole('row', defaultToExactOpts(opts)).and(this.getBySelector('.rdg-row'));
},
getCell(opts?: LocatorByRoleOptions) {
return this.getByRole('gridcell', defaultToExactOpts(opts));
},
getSelectAllCheckbox() {
return this.getByRole('checkbox', { name: 'Select All' });
},
getActiveCell() {
return this.getCell({ selected: true }).or(this.getHeaderCell({ selected: true }));
},
getDragHandle() {
return '.rdg-cell-drag-handle';
},
getBySelector(selector: string) {
return selector;
}
});
function defaultToExactOpts(
opts: LocatorByRoleOptions | undefined
): LocatorByRoleOptions | undefined {
if (opts != null && opts.exact == null && typeof opts.name === 'string') {
return {
...opts,
exact: true
};
}
return opts;
}
beforeEach(async () => {
// 1. reset cursor position to avoid hover issues
// 2. force focus to be on the page
await userEvent.click(document.body, { position: { x: 0, y: 0 } });
});
afterEach(() => {
vi.useRealTimers();
// eslint-disable-next-line vitest/no-standalone-expect
expect
.soft(
document.hasFocus(),
'Focus is set on a browser UI element at the end of a test. Use safeTab() to return focus to the page.'
)
.toBe(true);
});