Skip to content

Commit af3ed48

Browse files
authored
chore: refactor test (sveltejs#263)
* chore: refactor tests * remove the flaky test case
1 parent 082685f commit af3ed48

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

tests/focus_management.spec.ts

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect, test } from '@playwright/test';
22

33
const editor_selector = '.cm-content';
4-
const editor_focus_selector = '.cm-content';
54
const iframe_selector = 'iframe[src*="webcontainer.io/"]';
65

76
test.describe.configure({ mode: 'parallel' });
@@ -12,10 +11,11 @@ test('focus management: the editor keeps focus when iframe is loaded', async ({
1211
await page.goto('/tutorial/your-first-component');
1312

1413
// first, focus the editor before the iframe is loaded
15-
await page.locator(editor_selector).click({ delay: 1000 });
14+
await page.waitForTimeout(1000);
15+
await page.locator(editor_selector).click();
1616

1717
// at this time, expect focus to be on the editor
18-
await expect(page.locator(editor_focus_selector)).toBeFocused();
18+
await expect(page.locator(editor_selector)).toBeFocused();
1919

2020
// wait for the iframe to load
2121
await page.frameLocator(iframe_selector).getByText('Hello world!').waitFor();
@@ -24,7 +24,7 @@ test('focus management: the editor keeps focus when iframe is loaded', async ({
2424
await page.waitForTimeout(1000);
2525

2626
// expect focus to be on the editor
27-
await expect(page.locator(editor_focus_selector)).toBeFocused();
27+
await expect(page.locator(editor_selector)).toBeFocused();
2828
});
2929

3030
test('focus management: input inside the iframe gets focus when clicking it', async ({ page }) => {
@@ -38,48 +38,21 @@ test('focus management: input inside the iframe gets focus when clicking it', as
3838
await iframe.getByText('todos').waitFor();
3939

4040
// first, focus the editor
41-
await page.locator(editor_selector).click({ delay: 1000 });
42-
await expect(page.locator(editor_focus_selector)).toBeFocused();
41+
await page.waitForTimeout(1000)
42+
await page.locator(editor_selector).click();
43+
await expect(page.locator(editor_selector)).toBeFocused();
4344

4445
// then, click a input in the iframe
4546
const input = iframe.locator('input[name="description"]');
46-
await input.click({ delay: 1000 });
47+
await page.waitForTimeout(1000)
48+
await input.click();
4749

4850
// wait a little, because there may be a script that manipulates focus
4951
await page.waitForTimeout(1000);
5052

5153
// expect focus to be on the input in the iframe, not the editor.
5254
await expect(input).toBeFocused();
53-
await expect(page.locator(editor_focus_selector)).not.toBeFocused();
54-
});
55-
56-
test('focus management: body inside the iframe gets focus when clicking a link inside the iframe', async ({
57-
page
58-
}) => {
59-
await page.bringToFront();
60-
61-
await page.goto('/tutorial/layouts');
62-
63-
const iframe = page.frameLocator(iframe_selector);
64-
65-
// wait for the iframe to load
66-
await iframe.getByText('this is the home page.').waitFor();
67-
68-
// first, focus the editor
69-
await page.locator(editor_selector).click({ delay: 1000 });
70-
await expect(page.locator(editor_focus_selector)).toBeFocused();
71-
72-
// then, click a link in the iframe
73-
await iframe.locator('a[href="/about"]').click({ delay: 500 });
74-
75-
// wait for navigation
76-
await iframe.getByText('this is the about page.').waitFor();
77-
78-
// wait a little, because there may be a script that manipulates focus
79-
await page.waitForTimeout(1000);
80-
81-
// expect focus to be on body in the iframe, not the editor.
82-
await expect(iframe.locator('body')).toBeFocused();
55+
await expect(page.locator(editor_selector)).not.toBeFocused();
8356
});
8457

8558
test('focus management: The editor keeps focus while typing', async ({ page }) => {
@@ -92,11 +65,13 @@ test('focus management: The editor keeps focus while typing', async ({ page }) =
9265

9366
// first, write script tag
9467
const code = '<script>\n\n</script>\n';
95-
await page.locator(editor_focus_selector).fill(code);
68+
await page.locator(editor_selector).fill(code);
9669

9770
// move the cursor into the script tag
98-
await page.keyboard.press('PageUp', { delay: 500 });
99-
await page.keyboard.press('ArrowDown', { delay: 500 });
71+
await page.waitForTimeout(500);
72+
await page.keyboard.press('PageUp');
73+
await page.waitForTimeout(500);
74+
await page.keyboard.press('ArrowDown');
10075

10176
// wait a little because the above operation is flaky
10277
await page.waitForTimeout(500);

0 commit comments

Comments
 (0)