Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions packages/react/src/components/Comments/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export const Comments = ({

// Adds "resolved by" comment if needed.
if (thread.resolved && thread.resolvedUpdatedAt && thread.resolvedBy) {
const resolvedByUser = users.get(thread.resolvedBy);
if (!resolvedByUser) {
throw new Error(
`User ${thread.resolvedBy} resolved thread ${thread.id}, but their data could not be found.`,
);
}

const resolvedCommentIndex =
thread.comments.findLastIndex(
(comment) =>
Expand All @@ -49,9 +42,7 @@ export const Comments = ({
<Components.Comments.Comment
key={"resolved-comment"}
className={"bn-thread-comment"}
authorInfo={
(thread.resolvedBy && users.get(thread.resolvedBy)) || "loading"
}
authorInfo={users.get(thread.resolvedBy) ?? "loading"}
timeString={thread.resolvedUpdatedAt.toLocaleDateString(undefined, {
month: "short",
day: "numeric",
Expand Down
157 changes: 157 additions & 0 deletions packages/react/src/components/Comments/ThreadsSidebar.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { type PartialBlock, type User } from "@blocknote/core";
import {
CommentsExtension,
DefaultThreadStoreAuth,
} from "@blocknote/core/comments";
import { YjsThreadStore } from "@blocknote/core/y";
import * as Y from "@y/y";
import { describe, expect } from "vite-plus/test";
import { page } from "vite-plus/test/browser";
import { ThreadsSidebar } from "./ThreadsSidebar.js";

const user: User = { id: "user", username: "Test user", avatarUrl: "" };

function setupThreadStore(onTestFinished: (cleanup: () => void) => void) {
const doc = new Y.Doc();
onTestFinished(() => doc.destroy());
const threads = doc.get("threads");
const threadStore = new YjsThreadStore(
user.id,
threads,
new DefaultThreadStoreAuth(user.id, "editor"),
);

return {
threadStore,
clearComments(this: void, threadId: string, createdAt?: Date) {
// Publish an empty comment list without deleting the thread, as a custom
// store can do while synchronizing comments separately from threads.
doc.transact(() => {
const thread = threads.getAttr(threadId);
if (createdAt) {
thread.setAttr("createdAt", createdAt.getTime());
}
thread.getAttr("comments").delete(0, 1);
});
},
};
}

function commentBody(content: string): PartialBlock[] {
return [{ type: "paragraph", content }];
}

describe("ThreadsSidebar", () => {
describe("integration", () => {
it("keeps an empty thread selectable until its comments arrive", async ({
setupEditor,
selectText,
onTestFinished,
}) => {
const { threadStore, clearComments } = setupThreadStore(onTestFinished);
const { editor } = await setupEditor({
initialContent: commentBody("First anchor and Second anchor"),
extensions: [
CommentsExtension({
threadStore,
resolveUsers: async () => [user],
}),
],
children: <ThreadsSidebar filter="all" sort="recent-activity" />,
});
const comments = editor.getExtension(CommentsExtension)!;
selectText(editor, "First anchor");
await comments.createThread({
initialComment: { body: commentBody("Initial comment") },
});
const [threadId] = threadStore.getThreads().keys();
selectText(editor, "Second anchor");
await comments.createThread({
initialComment: { body: commentBody("Another thread") },
});

clearComments(threadId, new Date("2025-01-01"));

function cards() {
return Array.from(
document.querySelectorAll(".bn-threads-sidebar .bn-thread"),
(card) => card.textContent,
);
}

await expect
.poll(cards)
.toEqual([
expect.stringContaining("Second anchor"),
expect.stringContaining("First anchor"),
]);
await expect
.element(page.getByText("Initial comment", { exact: true }))
.not.toBeInTheDocument();
function firstMark() {
return Array.from(document.querySelectorAll(".bn-thread-mark")).find(
(mark) => mark.textContent === "First anchor",
);
}
await page.elementLocator(firstMark()!).click();
function floatingThread() {
return document.querySelector(
".bn-thread:not(.bn-threads-sidebar .bn-thread)",
);
}
await expect
.poll(() => floatingThread()?.querySelector(".bn-thread-composer"))
.toBeTruthy();

await threadStore.addComment({
threadId,
comment: { body: commentBody("Comment arrived") },
});

await expect.poll(() => cards()[0]).toContain("Comment arrived");
await expect
.poll(() => floatingThread()?.textContent)
.toContain("Comment arrived");

await threadStore.deleteThread({ threadId });

await expect
.poll(() => firstMark()?.getAttribute("data-orphan"))
.toBe("true");
await expect.poll(floatingThread).toBeNull();
await expect.poll(cards).toHaveLength(1);
});

it("shows a resolved empty thread while its resolving user loads", async ({
setupEditor,
onTestFinished,
}) => {
const { threadStore, clearComments } = setupThreadStore(onTestFinished);
const thread = await threadStore.createThread({
initialComment: { body: commentBody("Initial comment") },
});
clearComments(thread.id);
await threadStore.resolveThread({ threadId: thread.id });
const users = Promise.withResolvers<User[]>();
await setupEditor({
extensions: [
CommentsExtension({
threadStore,
resolveUsers: () => users.promise,
}),
],
children: <ThreadsSidebar filter="resolved" sort="recent-activity" />,
});

await expect
.poll(() =>
document.querySelector(".bn-threads-sidebar .bn-resolved-text"),
)
.not.toBeNull();

users.resolve([user]);

await expect.element(page.getByText(user.username)).toBeVisible();
});
});
});
1 change: 1 addition & 0 deletions tests/vite.config.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const LOCAL_BROWSER_FILES = [
"../packages/core/src/extensions/FormattingToolbar/FormattingToolbar.browser.test.ts",
"../packages/core/src/extensions/SuggestionMenu/SuggestionMenu.browser.test.ts",
"../packages/react/src/blocks/File/block.browser.test.tsx",
"../packages/react/src/components/Comments/ThreadsSidebar.browser.test.tsx",
"../packages/react/src/components/FormattingToolbar/DefaultButtons/BasicTextStyleButton.browser.test.tsx",
"../packages/react/src/components/FormattingToolbar/DefaultButtons/ColorStyleButton.browser.test.tsx",
"../packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.browser.test.tsx",
Expand Down
Loading