|
| 1 | +import { test, expect } from "./fixtures/auth"; |
| 2 | +import { navigateToEditorPage } from "./fixtures/editor-helpers"; |
| 3 | + |
| 4 | +test.describe("Code block paste", () => { |
| 5 | + test.beforeEach(async ({ authenticatedPage: page }) => { |
| 6 | + await navigateToEditorPage(page); |
| 7 | + }); |
| 8 | + |
| 9 | + test("pasting multi-line text into a code block preserves all lines", async ({ |
| 10 | + authenticatedPage: page, |
| 11 | + }) => { |
| 12 | + const editor = page.locator('[contenteditable="true"]'); |
| 13 | + await expect(editor).toBeVisible({ timeout: 10_000 }); |
| 14 | + |
| 15 | + // Insert a code block via slash command |
| 16 | + await editor.click(); |
| 17 | + await page.keyboard.press("End"); |
| 18 | + await page.keyboard.press("Enter"); |
| 19 | + await page.keyboard.type("/code"); |
| 20 | + |
| 21 | + const codeOption = page.locator('[role="option"]', { |
| 22 | + hasText: /code block/i, |
| 23 | + }); |
| 24 | + await expect(codeOption).toBeVisible({ timeout: 3_000 }); |
| 25 | + await codeOption.click(); |
| 26 | + |
| 27 | + // Wait for the code block to appear |
| 28 | + const codeBlock = editor.locator("code"); |
| 29 | + await expect(codeBlock).toBeVisible({ timeout: 3_000 }); |
| 30 | + |
| 31 | + // Focus inside the code block |
| 32 | + await codeBlock.click(); |
| 33 | + |
| 34 | + // Write multi-line text to the clipboard and paste it |
| 35 | + const multiLineCode = "const a = 1;\nconst b = 2;\nconst c = 3;"; |
| 36 | + |
| 37 | + // Grant clipboard permissions and write to clipboard |
| 38 | + await page.context().grantPermissions(["clipboard-read", "clipboard-write"]); |
| 39 | + await page.evaluate(async (text) => { |
| 40 | + await navigator.clipboard.writeText(text); |
| 41 | + }, multiLineCode); |
| 42 | + |
| 43 | + // Paste using keyboard shortcut |
| 44 | + const modifier = process.platform === "darwin" ? "Meta" : "Control"; |
| 45 | + await page.keyboard.press(`${modifier}+v`); |
| 46 | + |
| 47 | + // Verify all three lines are present inside the code block |
| 48 | + await expect(codeBlock).toContainText("const a = 1;", { timeout: 3_000 }); |
| 49 | + await expect(codeBlock).toContainText("const b = 2;"); |
| 50 | + await expect(codeBlock).toContainText("const c = 3;"); |
| 51 | + |
| 52 | + // Verify line breaks exist between lines. Lexical renders LineBreakNodes as |
| 53 | + // <br> elements, so innerText (which respects <br>) should contain newlines. |
| 54 | + const codeText = await codeBlock.evaluate( |
| 55 | + (el) => (el as HTMLElement).innerText |
| 56 | + ); |
| 57 | + const lines = codeText.split("\n").filter((l: string) => l.trim() !== ""); |
| 58 | + expect(lines.length).toBeGreaterThanOrEqual(3); |
| 59 | + }); |
| 60 | + |
| 61 | + test("pasting single-line text into a code block works normally", async ({ |
| 62 | + authenticatedPage: page, |
| 63 | + }) => { |
| 64 | + const editor = page.locator('[contenteditable="true"]'); |
| 65 | + await expect(editor).toBeVisible({ timeout: 10_000 }); |
| 66 | + |
| 67 | + // Insert a code block via slash command |
| 68 | + await editor.click(); |
| 69 | + await page.keyboard.press("End"); |
| 70 | + await page.keyboard.press("Enter"); |
| 71 | + await page.keyboard.type("/code"); |
| 72 | + |
| 73 | + const codeOption = page.locator('[role="option"]', { |
| 74 | + hasText: /code block/i, |
| 75 | + }); |
| 76 | + await expect(codeOption).toBeVisible({ timeout: 3_000 }); |
| 77 | + await codeOption.click(); |
| 78 | + |
| 79 | + const codeBlock = editor.locator("code"); |
| 80 | + await expect(codeBlock).toBeVisible({ timeout: 3_000 }); |
| 81 | + await codeBlock.click(); |
| 82 | + |
| 83 | + // Paste single-line text |
| 84 | + await page.context().grantPermissions(["clipboard-read", "clipboard-write"]); |
| 85 | + await page.evaluate(async (text) => { |
| 86 | + await navigator.clipboard.writeText(text); |
| 87 | + }, "const x = 42;"); |
| 88 | + |
| 89 | + const modifier = process.platform === "darwin" ? "Meta" : "Control"; |
| 90 | + await page.keyboard.press(`${modifier}+v`); |
| 91 | + |
| 92 | + await expect(codeBlock).toContainText("const x = 42;", { timeout: 3_000 }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments