|
| 1 | +import { createUserStore, type PartialBlock } from "@blocknote/core"; |
| 2 | +import { |
| 3 | + CommentsExtension, |
| 4 | + DefaultThreadStoreAuth, |
| 5 | +} from "@blocknote/core/comments"; |
| 6 | +import { YjsThreadStore } from "@blocknote/core/y"; |
| 7 | +import { ThreadsSidebar } from "@blocknote/react"; |
| 8 | +import * as Y from "@y/y"; |
| 9 | + |
| 10 | +import { describe, expect, type TestContext } from "vite-plus/test"; |
| 11 | +import { page } from "vite-plus/test/browser"; |
| 12 | +import type { BrowserContext } from "../../../../../tests/src/fixtures/editor.js"; |
| 13 | + |
| 14 | +const commentDocument: PartialBlock[] = [ |
| 15 | + { type: "paragraph", content: "A comment" }, |
| 16 | +]; |
| 17 | + |
| 18 | +const editorDocument: PartialBlock[] = [ |
| 19 | + { type: "paragraph", content: "Commented document" }, |
| 20 | +]; |
| 21 | + |
| 22 | +async function openComments( |
| 23 | + { |
| 24 | + setupEditor, |
| 25 | + onTestFinished, |
| 26 | + }: Pick<BrowserContext, "setupEditor"> & Pick<TestContext, "onTestFinished">, |
| 27 | + { resolved = false } = {}, |
| 28 | +) { |
| 29 | + const doc = new Y.Doc(); |
| 30 | + onTestFinished(() => doc.destroy()); |
| 31 | + const threadStore = new YjsThreadStore( |
| 32 | + "user", |
| 33 | + doc.get("threads"), |
| 34 | + new DefaultThreadStoreAuth("user", "editor"), |
| 35 | + ); |
| 36 | + const thread = await threadStore.createThread({ |
| 37 | + initialComment: { body: commentDocument }, |
| 38 | + }); |
| 39 | + if (resolved) { |
| 40 | + await threadStore.resolveThread({ threadId: thread.id }); |
| 41 | + } |
| 42 | + const users = createUserStore(async () => [ |
| 43 | + { |
| 44 | + id: "user", |
| 45 | + username: "Test user", |
| 46 | + avatarUrl: "/image.svg", |
| 47 | + }, |
| 48 | + ]); |
| 49 | + await users.loadUsers(["user"]); |
| 50 | + await setupEditor({ |
| 51 | + initialContent: editorDocument, |
| 52 | + extensions: [CommentsExtension({ threadStore, resolveUsers: users })], |
| 53 | + children: <ThreadsSidebar filter="all" />, |
| 54 | + outside: ({ children }) => ( |
| 55 | + <main> |
| 56 | + <button type="button">Before editor</button> |
| 57 | + {children} |
| 58 | + <button type="button">After editor</button> |
| 59 | + </main> |
| 60 | + ), |
| 61 | + }); |
| 62 | + await page |
| 63 | + .getByRole("button", { name: "Before editor", exact: true }) |
| 64 | + .click(); |
| 65 | + return { threadStore, threadId: thread.id }; |
| 66 | +} |
| 67 | + |
| 68 | +async function focusCommentActionWithAssertions( |
| 69 | + focusUntil: BrowserContext["focusUntil"], |
| 70 | + toolbarButton: string, |
| 71 | +) { |
| 72 | + const toolbar = page.getByRole("toolbar"); |
| 73 | + const button = toolbar.getByRole("button", { |
| 74 | + name: toolbarButton, |
| 75 | + exact: true, |
| 76 | + }); |
| 77 | + await expect.element(button).toBeEnabled(); |
| 78 | + // Start at "Before editor" and reach the action without hovering the comment. |
| 79 | + await focusUntil(button); |
| 80 | + await expect |
| 81 | + .element(button, { message: `${toolbarButton} is reachable with Tab` }) |
| 82 | + .toHaveFocus(); |
| 83 | + await expect |
| 84 | + .poll( |
| 85 | + () => |
| 86 | + getComputedStyle(button.element().closest(".bn-comment-toolbar")!) |
| 87 | + .opacity, |
| 88 | + ) |
| 89 | + .toBe("1"); |
| 90 | + return { toolbar, button }; |
| 91 | +} |
| 92 | + |
| 93 | +describe("keyboard", () => { |
| 94 | + it("comment actions toolbar: Add reaction", async ({ |
| 95 | + setupEditor, |
| 96 | + onTestFinished, |
| 97 | + focusUntil, |
| 98 | + pressKey, |
| 99 | + }) => { |
| 100 | + await openComments({ setupEditor, onTestFinished }); |
| 101 | + await focusCommentActionWithAssertions(focusUntil, "Add reaction"); |
| 102 | + await pressKey("Enter"); |
| 103 | + await expect.element(page.getByRole("dialog")).toBeVisible(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("comment actions toolbar: Resolve", async ({ |
| 107 | + setupEditor, |
| 108 | + onTestFinished, |
| 109 | + focusUntil, |
| 110 | + pressKey, |
| 111 | + }) => { |
| 112 | + const { threadStore, threadId } = await openComments({ |
| 113 | + setupEditor, |
| 114 | + onTestFinished, |
| 115 | + }); |
| 116 | + await focusCommentActionWithAssertions(focusUntil, "Resolve"); |
| 117 | + await pressKey("Enter"); |
| 118 | + await expect |
| 119 | + .poll(() => threadStore.getThread(threadId).resolved) |
| 120 | + .toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it("comment actions toolbar: Re-open", async ({ |
| 124 | + setupEditor, |
| 125 | + onTestFinished, |
| 126 | + focusUntil, |
| 127 | + pressKey, |
| 128 | + }) => { |
| 129 | + const { threadStore, threadId } = await openComments( |
| 130 | + { setupEditor, onTestFinished }, |
| 131 | + { |
| 132 | + resolved: true, |
| 133 | + }, |
| 134 | + ); |
| 135 | + await focusCommentActionWithAssertions(focusUntil, "Re-open"); |
| 136 | + await pressKey("Enter"); |
| 137 | + await expect |
| 138 | + .poll(() => threadStore.getThread(threadId).resolved) |
| 139 | + .toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + it("comment actions toolbar: More actions", async ({ |
| 143 | + setupEditor, |
| 144 | + onTestFinished, |
| 145 | + focusUntil, |
| 146 | + pressKey, |
| 147 | + }) => { |
| 148 | + await openComments({ setupEditor, onTestFinished }); |
| 149 | + await focusCommentActionWithAssertions(focusUntil, "More actions"); |
| 150 | + await pressKey("Enter"); |
| 151 | + await expect.element(page.getByRole("menu")).toBeVisible(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments