Skip to content

Commit 27ca5f9

Browse files
test: add E2E tests for markdown shortcuts in editor (#224) (#231)
Co-authored-by: Ona <no-reply@ona.com>
1 parent d576c90 commit 27ca5f9

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { test, expect } from "./fixtures/auth";
2+
import { navigateToEditorPage } from "./fixtures/editor-helpers";
3+
4+
test.describe("Editor markdown shortcuts", () => {
5+
test.beforeEach(async ({ authenticatedPage: page }) => {
6+
await navigateToEditorPage(page);
7+
});
8+
9+
test("typing '# ' converts to H1", async ({
10+
authenticatedPage: page,
11+
}) => {
12+
const editor = page.locator('[contenteditable="true"]');
13+
await expect(editor).toBeVisible({ timeout: 10_000 });
14+
15+
await editor.click();
16+
await page.keyboard.press("End");
17+
await page.keyboard.press("Enter");
18+
19+
// Count existing h1 elements before the shortcut
20+
const h1CountBefore = await editor.locator("h1").count();
21+
22+
// Type the markdown shortcut: "# " triggers heading conversion
23+
await page.keyboard.type("# ");
24+
25+
// Lexical should convert the paragraph to an h1
26+
const h1CountAfter = await editor.locator("h1").count();
27+
expect(h1CountAfter).toBeGreaterThan(h1CountBefore);
28+
29+
// Type text into the heading and verify it renders inside the h1
30+
const uid = Date.now().toString();
31+
const marker = `heading ${uid}`;
32+
await page.keyboard.type(marker);
33+
34+
const heading = editor.locator("h1").filter({ hasText: marker });
35+
await expect(heading).toBeVisible({ timeout: 2_000 });
36+
});
37+
38+
test("typing '> ' converts to blockquote", async ({
39+
authenticatedPage: page,
40+
}) => {
41+
const editor = page.locator('[contenteditable="true"]');
42+
await expect(editor).toBeVisible({ timeout: 10_000 });
43+
44+
await editor.click();
45+
await page.keyboard.press("End");
46+
await page.keyboard.press("Enter");
47+
48+
const bqCountBefore = await editor.locator("blockquote").count();
49+
50+
// Type the markdown shortcut: "> " triggers blockquote conversion
51+
await page.keyboard.type("> ");
52+
53+
const bqCountAfter = await editor.locator("blockquote").count();
54+
expect(bqCountAfter).toBeGreaterThan(bqCountBefore);
55+
56+
// Type text and verify it renders inside the blockquote
57+
const uid = Date.now().toString();
58+
const marker = `quoted ${uid}`;
59+
await page.keyboard.type(marker);
60+
61+
const blockquote = editor.locator("blockquote").filter({ hasText: marker });
62+
await expect(blockquote).toBeVisible({ timeout: 2_000 });
63+
});
64+
65+
test("typing '- ' converts to bullet list", async ({
66+
authenticatedPage: page,
67+
}) => {
68+
const editor = page.locator('[contenteditable="true"]');
69+
await expect(editor).toBeVisible({ timeout: 10_000 });
70+
71+
await editor.click();
72+
await page.keyboard.press("End");
73+
await page.keyboard.press("Enter");
74+
75+
const ulCountBefore = await editor.locator("ul").count();
76+
77+
// Type the markdown shortcut: "- " triggers unordered list conversion
78+
await page.keyboard.type("- ");
79+
80+
const ulCountAfter = await editor.locator("ul").count();
81+
expect(ulCountAfter).toBeGreaterThan(ulCountBefore);
82+
83+
// Type text and verify it renders inside a list item
84+
const uid = Date.now().toString();
85+
const marker = `bullet ${uid}`;
86+
await page.keyboard.type(marker);
87+
88+
const listItem = editor.locator("ul > li").filter({ hasText: marker });
89+
await expect(listItem).toBeVisible({ timeout: 2_000 });
90+
});
91+
92+
test("typing '```' converts to code block", async ({
93+
authenticatedPage: page,
94+
}) => {
95+
const editor = page.locator('[contenteditable="true"]');
96+
await expect(editor).toBeVisible({ timeout: 10_000 });
97+
98+
await editor.click();
99+
await page.keyboard.press("End");
100+
await page.keyboard.press("Enter");
101+
102+
const codeCountBefore = await editor.locator("code").count();
103+
104+
// Type the markdown shortcut: "```" followed by Enter/space triggers code block
105+
// Lexical's CODE transformer is a multiline-element that triggers on ```
106+
await page.keyboard.type("```");
107+
await page.keyboard.press("Space");
108+
109+
// Wait for the code block to appear
110+
await page.waitForTimeout(500);
111+
112+
const codeCountAfter = await editor.locator("code").count();
113+
expect(codeCountAfter).toBeGreaterThan(codeCountBefore);
114+
115+
// The code block element should be visible
116+
const codeBlock = editor.locator("code").last();
117+
await expect(codeBlock).toBeVisible({ timeout: 2_000 });
118+
});
119+
});

0 commit comments

Comments
 (0)