|
| 1 | +import { type PartialBlock, type User } from "@blocknote/core"; |
| 2 | +import { |
| 3 | + CommentsExtension, |
| 4 | + DefaultThreadStoreAuth, |
| 5 | +} from "@blocknote/core/comments"; |
| 6 | +import { YjsThreadStore } from "@blocknote/core/y"; |
| 7 | +import * as Y from "@y/y"; |
| 8 | +import { describe, expect } from "vite-plus/test"; |
| 9 | +import { page } from "vite-plus/test/browser"; |
| 10 | +import { ThreadsSidebar } from "./ThreadsSidebar.js"; |
| 11 | + |
| 12 | +const user: User = { id: "user", username: "Test user", avatarUrl: "" }; |
| 13 | + |
| 14 | +function setupThreadStore(onTestFinished: (cleanup: () => void) => void) { |
| 15 | + const doc = new Y.Doc(); |
| 16 | + onTestFinished(() => doc.destroy()); |
| 17 | + const threads = doc.get("threads"); |
| 18 | + const threadStore = new YjsThreadStore( |
| 19 | + user.id, |
| 20 | + threads, |
| 21 | + new DefaultThreadStoreAuth(user.id, "editor"), |
| 22 | + ); |
| 23 | + |
| 24 | + return { |
| 25 | + threadStore, |
| 26 | + clearComments(this: void, threadId: string, createdAt?: Date) { |
| 27 | + // Publish an empty comment list without deleting the thread, as a custom |
| 28 | + // store can do while synchronizing comments separately from threads. |
| 29 | + doc.transact(() => { |
| 30 | + const thread = threads.getAttr(threadId); |
| 31 | + if (createdAt) { |
| 32 | + thread.setAttr("createdAt", createdAt.getTime()); |
| 33 | + } |
| 34 | + thread.getAttr("comments").delete(0, 1); |
| 35 | + }); |
| 36 | + }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function commentBody(content: string): PartialBlock[] { |
| 41 | + return [{ type: "paragraph", content }]; |
| 42 | +} |
| 43 | + |
| 44 | +describe("ThreadsSidebar", () => { |
| 45 | + describe("integration", () => { |
| 46 | + it("keeps an empty thread selectable until its comments arrive", async ({ |
| 47 | + setupEditor, |
| 48 | + selectText, |
| 49 | + onTestFinished, |
| 50 | + }) => { |
| 51 | + const { threadStore, clearComments } = setupThreadStore(onTestFinished); |
| 52 | + const { editor } = await setupEditor({ |
| 53 | + initialContent: commentBody("First anchor and Second anchor"), |
| 54 | + extensions: [ |
| 55 | + CommentsExtension({ |
| 56 | + threadStore, |
| 57 | + resolveUsers: async () => [user], |
| 58 | + }), |
| 59 | + ], |
| 60 | + children: <ThreadsSidebar filter="all" sort="recent-activity" />, |
| 61 | + }); |
| 62 | + const comments = editor.getExtension(CommentsExtension)!; |
| 63 | + selectText(editor, "First anchor"); |
| 64 | + await comments.createThread({ |
| 65 | + initialComment: { body: commentBody("Initial comment") }, |
| 66 | + }); |
| 67 | + const [threadId] = threadStore.getThreads().keys(); |
| 68 | + selectText(editor, "Second anchor"); |
| 69 | + await comments.createThread({ |
| 70 | + initialComment: { body: commentBody("Another thread") }, |
| 71 | + }); |
| 72 | + |
| 73 | + clearComments(threadId, new Date("2025-01-01")); |
| 74 | + |
| 75 | + function cards() { |
| 76 | + return Array.from( |
| 77 | + document.querySelectorAll(".bn-threads-sidebar .bn-thread"), |
| 78 | + (card) => card.textContent, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + await expect |
| 83 | + .poll(cards) |
| 84 | + .toEqual([ |
| 85 | + expect.stringContaining("Second anchor"), |
| 86 | + expect.stringContaining("First anchor"), |
| 87 | + ]); |
| 88 | + await expect |
| 89 | + .element(page.getByText("Initial comment", { exact: true })) |
| 90 | + .not.toBeInTheDocument(); |
| 91 | + function firstMark() { |
| 92 | + return Array.from(document.querySelectorAll(".bn-thread-mark")).find( |
| 93 | + (mark) => mark.textContent === "First anchor", |
| 94 | + ); |
| 95 | + } |
| 96 | + await page.elementLocator(firstMark()!).click(); |
| 97 | + function floatingThread() { |
| 98 | + return document.querySelector( |
| 99 | + ".bn-thread:not(.bn-threads-sidebar .bn-thread)", |
| 100 | + ); |
| 101 | + } |
| 102 | + await expect |
| 103 | + .poll(() => floatingThread()?.querySelector(".bn-thread-composer")) |
| 104 | + .toBeTruthy(); |
| 105 | + |
| 106 | + await threadStore.addComment({ |
| 107 | + threadId, |
| 108 | + comment: { body: commentBody("Comment arrived") }, |
| 109 | + }); |
| 110 | + |
| 111 | + await expect.poll(() => cards()[0]).toContain("Comment arrived"); |
| 112 | + await expect |
| 113 | + .poll(() => floatingThread()?.textContent) |
| 114 | + .toContain("Comment arrived"); |
| 115 | + |
| 116 | + await threadStore.deleteThread({ threadId }); |
| 117 | + |
| 118 | + await expect |
| 119 | + .poll(() => firstMark()?.getAttribute("data-orphan")) |
| 120 | + .toBe("true"); |
| 121 | + await expect.poll(floatingThread).toBeNull(); |
| 122 | + await expect.poll(cards).toHaveLength(1); |
| 123 | + }); |
| 124 | + |
| 125 | + it("shows a resolved empty thread while its resolving user loads", async ({ |
| 126 | + setupEditor, |
| 127 | + onTestFinished, |
| 128 | + }) => { |
| 129 | + const { threadStore, clearComments } = setupThreadStore(onTestFinished); |
| 130 | + const thread = await threadStore.createThread({ |
| 131 | + initialComment: { body: commentBody("Initial comment") }, |
| 132 | + }); |
| 133 | + clearComments(thread.id); |
| 134 | + await threadStore.resolveThread({ threadId: thread.id }); |
| 135 | + const users = Promise.withResolvers<User[]>(); |
| 136 | + await setupEditor({ |
| 137 | + extensions: [ |
| 138 | + CommentsExtension({ |
| 139 | + threadStore, |
| 140 | + resolveUsers: () => users.promise, |
| 141 | + }), |
| 142 | + ], |
| 143 | + children: <ThreadsSidebar filter="resolved" sort="recent-activity" />, |
| 144 | + }); |
| 145 | + |
| 146 | + await expect |
| 147 | + .poll(() => |
| 148 | + document.querySelector(".bn-threads-sidebar .bn-resolved-text"), |
| 149 | + ) |
| 150 | + .not.toBeNull(); |
| 151 | + |
| 152 | + users.resolve([user]); |
| 153 | + |
| 154 | + await expect.element(page.getByText(user.username)).toBeVisible(); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments