File
Blob: tests/e2e/specs/26-code-block-selection.spec.ts
| 1 | import type { Page } from "@playwright/test"; |
| 2 | import { test, expect, createTestPage } from "../fixtures/bland-test"; |
| 3 | |
| 4 | async function getSelectionText(page: Page): Promise<string> { |
| 5 | return page.evaluate(() => window.getSelection()?.toString() ?? ""); |
| 6 | } |
| 7 | |
| 8 | async function clickCodeBlockText(page: Page, text: string, clickCount: 2 | 3): Promise<void> { |
| 9 | const point = await page.locator(".tiptap-code-block-content").evaluate((codeBlock, targetText) => { |
| 10 | const walker = document.createTreeWalker(codeBlock, NodeFilter.SHOW_TEXT); |
| 11 | |
| 12 | while (walker.nextNode()) { |
| 13 | const textNode = walker.currentNode; |
| 14 | const nodeText = textNode.textContent ?? ""; |
| 15 | const index = nodeText.indexOf(targetText); |
| 16 | if (index === -1) continue; |
| 17 | |
| 18 | const range = document.createRange(); |
| 19 | range.setStart(textNode, index); |
| 20 | range.setEnd(textNode, index + targetText.length); |
| 21 | |
| 22 | const rect = range.getClientRects()[0]; |
| 23 | range.detach(); |
| 24 | |
| 25 | if (!rect) continue; |
| 26 | return { |
| 27 | x: rect.left + rect.width / 2, |
| 28 | y: rect.top + rect.height / 2, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | return null; |
| 33 | }, text); |
| 34 | |
| 35 | expect(point).not.toBeNull(); |
| 36 | await page.mouse.click(point!.x, point!.y, { clickCount }); |
| 37 | } |
| 38 | |
| 39 | test.describe("code block selection", () => { |
| 40 | test("double-click selects a word and triple-click selects the clicked line", async ({ |
| 41 | authenticatedPage: { page, accessToken }, |
| 42 | e2eWorkspace, |
| 43 | }) => { |
| 44 | const testPage = await createTestPage(page, accessToken, "Code Block Selection Test", e2eWorkspace); |
| 45 | |
| 46 | await page.goto(`/${testPage.workspaceSlug}/${testPage.pageId}`); |
| 47 | |
| 48 | const editor = page.locator(".tiptap[contenteditable='true']"); |
| 49 | await editor.waitFor({ timeout: 30_000 }); |
| 50 | |
| 51 | await editor.click(); |
| 52 | await page.keyboard.type("```ts "); |
| 53 | await page.keyboard.type("const alpha = 1;"); |
| 54 | await page.keyboard.press("Enter"); |
| 55 | await page.keyboard.type("let betaValue = alpha + 1;"); |
| 56 | |
| 57 | const codeBlock = page.locator(".tiptap-code-block-content"); |
| 58 | await expect(codeBlock).toContainText("const alpha = 1;"); |
| 59 | await expect(codeBlock).toContainText("let betaValue = alpha + 1;"); |
| 60 | |
| 61 | await clickCodeBlockText(page, "alpha", 2); |
| 62 | await expect.poll(() => getSelectionText(page)).toBe("alpha"); |
| 63 | |
| 64 | await clickCodeBlockText(page, "betaValue", 3); |
| 65 | await expect.poll(() => getSelectionText(page)).toBe("let betaValue = alpha + 1;"); |
| 66 | }); |
| 67 | }); |