-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
index.test.ts
290 lines (242 loc) · 10 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
import { expect } from 'chai';
import * as playwright from 'playwright';
import type {
ByRoleMatcher,
ByRoleOptions,
Matcher,
MatcherOptions,
SelectorMatcherOptions,
} from '@testing-library/dom';
import '../utils/initPlaywrightMatchers';
function sleep(duration: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, duration);
});
}
interface PlaywrightScreen {
getByLabelText: (
labelText: Matcher,
options?: SelectorMatcherOptions,
) => Promise<playwright.ElementHandle<HTMLElement>>;
getByRole: (
role: ByRoleMatcher,
options?: ByRoleOptions,
) => Promise<playwright.ElementHandle<HTMLElement>>;
getByTestId: (
testId: string,
options?: MatcherOptions,
) => Promise<playwright.ElementHandle<HTMLElement>>;
getByText: (
text: Matcher,
options?: SelectorMatcherOptions,
) => Promise<playwright.ElementHandle<HTMLElement>>;
}
/**
* 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;
const screen: PlaywrightScreen = {
getByLabelText: (...inputArgs) => {
return page.evaluateHandle(
(args) => window.DomTestingLibrary.getByLabelText(document.body, ...args),
inputArgs,
);
},
getByRole: (...inputArgs) => {
return page.evaluateHandle(
(args) => window.DomTestingLibrary.getByRole(document.body, ...args),
inputArgs,
);
},
getByText: (...inputArgs) => {
return page.evaluateHandle(
(args) => window.DomTestingLibrary.getByText(document.body, ...args),
inputArgs,
);
},
getByTestId: (...inputArgs) => {
return page.evaluateHandle(
(args) => window.DomTestingLibrary.getByTestId(document.body, ...args),
inputArgs,
);
},
};
async function renderFixture(fixturePath: string) {
await page.goto(`${baseUrl}/e2e/${fixturePath}#no-dev`);
await page.waitForSelector('[data-testid="testcase"]:not([aria-busy="true"])');
}
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('<FocusTrap />', () => {
it('should loop the tab key', async () => {
await renderFixture('FocusTrap/OpenFocusTrap');
await expect(screen.getByTestId('root')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('x')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('cancel')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('ok')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('x')).toHaveFocus();
await screen.getByTestId('initial-focus').then(($element) => $element.focus());
await expect(screen.getByTestId('root')).toHaveFocus();
await screen.getByText('x').then(($element) => $element.focus());
await page.keyboard.press('Shift+Tab');
await expect(screen.getByText('ok')).toHaveFocus();
});
it('should loop the tab key after activation', async () => {
await renderFixture('FocusTrap/DefaultOpenLazyFocusTrap');
await expect(screen.getByTestId('initial-focus')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('close')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('noop')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('close')).toHaveFocus();
await page.keyboard.press('Enter');
await expect(screen.getByTestId('initial-focus')).toHaveFocus();
});
it('should focus on first focus element after last has received a tab click', async () => {
await renderFixture('FocusTrap/OpenFocusTrap');
await page.keyboard.press('Tab');
await expect(screen.getByText('x')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('cancel')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('ok')).toHaveFocus();
});
it('should be able to be tabbed straight through when rendered closed', async () => {
await renderFixture('FocusTrap/ClosedFocusTrap');
await expect(screen.getByText('initial focus')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('inside focusable')).toHaveFocus();
await page.keyboard.press('Tab');
await expect(screen.getByText('final tab target')).toHaveFocus();
});
it('should not trap focus when clicking outside when disableEnforceFocus is set', async () => {
await renderFixture('FocusTrap/DisableEnforceFocusFocusTrap');
// initial focus is on the button outside of the trap focus
await expect(screen.getByTestId('initial-focus')).toHaveFocus();
// focus the button inside the trap focus
await page.keyboard.press('Tab');
await expect(screen.getByTestId('inside-trap-focus')).toHaveFocus();
// the focus is now trapped inside
await page.keyboard.press('Tab');
await expect(screen.getByTestId('inside-trap-focus')).toHaveFocus();
const initialFocus = (await screen.getByTestId('initial-focus'))!;
await initialFocus.click();
await expect(screen.getByTestId('initial-focus')).toHaveFocus();
});
});
describe('<Rating />', () => {
it('should loop the arrow key', async () => {
await renderFixture('Rating/BasicRating');
await page.focus('input[name="rating-test"]:checked');
await expect(page.evaluateHandle(() => document.activeElement)).toHaveAttribute('value', '1');
await page.keyboard.press('ArrowLeft');
await expect(page.evaluateHandle(() => document.activeElement)).to.toHaveAttribute(
'value',
'',
);
await page.keyboard.press('ArrowLeft');
await expect(page.evaluateHandle(() => document.activeElement)).to.toHaveAttribute(
'value',
'5',
);
});
});
describe('<Autocomplete/>', () => {
it('[Material Autocomplete] should highlight correct option when initial navigation through options starts from mouse move', async () => {
await renderFixture('Autocomplete/HoverMaterialAutocomplete');
const combobox = (await screen.getByRole('combobox'))!;
await combobox.click();
const firstOption = (await screen.getByText('one'))!;
const dimensions = (await firstOption.boundingBox())!;
await page.mouse.move(dimensions.x + 10, dimensions.y + 10); // moves to 1st option
await page.keyboard.down('ArrowDown'); // moves to 2nd option
await page.keyboard.down('ArrowDown'); // moves to 3rd option
await page.keyboard.down('ArrowDown'); // moves to 4th option
const listbox = (await screen.getByRole('listbox'))!;
const focusedOption = (await listbox.$('.Mui-focused'))!;
const focusedOptionText = await focusedOption.innerHTML();
expect(focusedOptionText).to.equal('four');
});
it('[Joy Autocomplete] should highlight correct option when initial navigation through options starts from mouse move', async () => {
await renderFixture('Autocomplete/HoverJoyAutocomplete');
const combobox = (await screen.getByRole('combobox'))!;
await combobox.click();
const firstOption = (await screen.getByText('one'))!;
const dimensions = (await firstOption.boundingBox())!;
await page.mouse.move(dimensions.x + 10, dimensions.y + 10); // moves to 1st option
await page.keyboard.down('ArrowDown'); // moves to 2nd option
await page.keyboard.down('ArrowDown'); // moves to 3rd option
await page.keyboard.down('ArrowDown'); // moves to 4th option
const listbox = (await screen.getByRole('listbox'))!;
const focusedOption = (await listbox.$('.Mui-focused'))!;
const focusedOptionText = await focusedOption.innerHTML();
expect(focusedOptionText).to.equal('four');
});
});
describe('<TextareaAutosize />', () => {
// https://github.com/mui/material-ui/issues/32640
it('should handle suspense without error', async () => {
const pageErrors: string[] = [];
page.on('pageerror', (err) => pageErrors.push(err.name));
await renderFixture('TextareaAutosize/TextareaAutosizeSuspense');
expect(await page.isVisible('textarea')).to.equal(true);
await page.click('button');
expect(await page.isVisible('textarea')).to.equal(false);
await page.waitForTimeout(200); // Wait for debounce to fire (166)
expect(pageErrors.length).to.equal(0);
});
});
describe('<TextField />', () => {
it('should fire `onClick` when clicking on the focused label position', async () => {
await renderFixture('TextField/OutlinedTextFieldOnClick');
// execute the click on the focused label position
await page.getByRole('textbox').click({ position: { x: 10, y: 10 } });
const errorSelector = page.locator('.MuiInputBase-root.Mui-error');
await errorSelector.waitFor();
});
});
});