Skip to content
Draft
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
121 changes: 121 additions & 0 deletions packages/react/src/components/Comments/Comment.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { en } from "@blocknote/core/locales";
import type { CommentData, ThreadData } from "@blocknote/core/comments";
import type { ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it, vi } from "vite-plus/test";

import { Comment } from "./Comment.js";

const { passThrough, components, allow } = vi.hoisted(() => {
const passThrough = ({ children }: { children?: ReactNode }) => (
<>{children}</>
);

// Stand-ins for the UI library components. Like the real Mantine, Ariakit
// and ShadCN toolbar buttons, this one uses `label` as its `aria-label`.
const components = {
Comments: {
Comment: ({ actions }: { actions?: ReactNode }) => <div>{actions}</div>,
},
Generic: {
Toolbar: {
Root: passThrough,
Button: ({
label,
children,
}: {
label?: string;
children?: ReactNode;
}) => <button aria-label={label}>{children}</button>,
},
Menu: {
Root: passThrough,
Trigger: passThrough,
Dropdown: () => null,
Item: () => null,
},
},
};

return { passThrough, components, allow: () => true };
});

vi.mock("../../editor/ComponentsContext.js", () => ({
useComponentsContext: () => components,
}));
vi.mock("../../editor/PortalElementOverride.js", () => ({
usePortalElement: () => undefined,
}));
vi.mock("../../i18n/dictionary.js", () => ({
useDictionary: () => en,
}));
vi.mock("../../hooks/useExtension.js", () => ({
useExtension: () => ({
threadStore: {
auth: {
canAddReaction: allow,
canDeleteComment: allow,
canUpdateComment: allow,
canResolveThread: allow,
canUnresolveThread: allow,
},
},
}),
}));
vi.mock("../../hooks/useCreateBlockNote.js", () => ({
useCreateBlockNote: () => ({ getExtension: () => ({ submit: vi.fn() }) }),
}));
vi.mock("./useCommentUsers.js", () => ({ useCommentUser: () => undefined }));
vi.mock("./CommentEditor.js", () => ({ CommentEditor: () => null }));
vi.mock("./EmojiPicker.js", () => ({ EmojiPicker: passThrough }));

const date = new Date(2026, 0, 1);
const comment = {
type: "comment",
id: "comment-1",
userId: "user-1",
createdAt: date,
updatedAt: date,
reactions: [],
metadata: {},
body: [],
} as unknown as CommentData;

function thread(resolved: boolean) {
return {
type: "thread",
id: "thread-1",
createdAt: date,
updatedAt: date,
comments: [comment],
resolved,
metadata: {},
} as unknown as ThreadData;
}

describe("Comment actions", () => {
it.each([
[false, en.comments.actions.resolve],
[true, en.comments.actions.reopen],
])(
"icon-only buttons have an accessible name (resolved: %s)",
(resolved, resolveOrReopen) => {
const html = renderToStaticMarkup(
<Comment
comment={comment}
thread={thread(resolved)}
showResolveButton
/>,
);

const buttons = html.match(/<button[^>]*>/g) ?? [];
expect(
buttons.map((button) => button.match(/aria-label="([^"]*)"/)?.[1]),
).toEqual([
en.comments.actions.add_reaction,
resolveOrReopen,
en.comments.actions.more_actions,
]);
},
);
});
4 changes: 4 additions & 0 deletions packages/react/src/components/Comments/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export const Comment = ({
>
<Components.Generic.Toolbar.Button
key={"add-reaction"}
label={dict.comments.actions.add_reaction}
mainTooltip={dict.comments.actions.add_reaction}
variant="compact"
>
Expand All @@ -281,6 +282,7 @@ export const Comment = ({
(thread.resolved ? (
<Components.Generic.Toolbar.Button
key={"reopen"}
label={dict.comments.actions.reopen}
mainTooltip={dict.comments.actions.reopen}
variant="compact"
onClick={onReopen}
Expand All @@ -290,6 +292,7 @@ export const Comment = ({
) : (
<Components.Generic.Toolbar.Button
key={"resolve"}
label={dict.comments.actions.resolve}
mainTooltip={dict.comments.actions.resolve}
variant="compact"
onClick={onResolve}
Expand All @@ -306,6 +309,7 @@ export const Comment = ({
<Components.Generic.Menu.Trigger>
<Components.Generic.Toolbar.Button
key={"more-actions"}
label={dict.comments.actions.more_actions}
mainTooltip={dict.comments.actions.more_actions}
variant="compact"
>
Expand Down
Loading