import { create } from "@bufbuild/protobuf"; import { render, waitFor } from "@testing-library/react"; import { createRef } from "react"; import { describe, expect, it, vi } from "vitest"; import Editor from "@/components/MemoEditor/edit/Editor"; import type { EditorController } from "@/components/MemoEditor/edit/types/editorController"; import { UserSetting_TagsSettingSchema } from "@/types/proto/api/user_service_pb"; import { ColorSchema } from "@/types/proto/google/type/color_pb"; const queries = vi.hoisted(() => ({ useTagCounts: vi.fn(() => ({ data: {} })), users: new Map(), tagsSetting: undefined as unknown, })); vi.mock("@/hooks/useUserQueries", () => ({ useTagCounts: queries.useTagCounts, useUsersByUsernames: (usernames: string[]) => ({ data: usernames.length ? new Map(usernames.map((username) => [username, queries.users.get(username)])) : undefined, }), userDetailQueryOptions: () => ({}), })); vi.mock("@/contexts/AuthContext", () => ({ useAuth: () => ({ userTagsSetting: queries.tagsSetting }) })); const baseProps = { className: "x", placeholder: "memo", onContentChange: () => {}, onFiles: () => {}, onSubmit: () => {}, }; describe("Editor", () => { it("scopes tag autocomplete stats to the current user", () => { render(); expect(queries.useTagCounts).toHaveBeenCalledWith(true); }); it("loads markdown and serializes it back verbatim", () => { const ref = createRef(); render(); expect(ref.current?.getMarkdown()).toBe("# Title\n\n- a\n 1. b"); }); it("emits edits through onContentChange", async () => { const ref = createRef(); const onContentChange = vi.fn(); render(); ref.current?.setSelection({ anchor: { block: 0, offset: 5 }, focus: { block: 0, offset: 5 } }); ref.current?.insertMarkdown("beta"); await waitFor(() => expect(onContentChange).toHaveBeenLastCalledWith("alpha\n\nbeta")); }); it("applies a new value from outside the editor", () => { const ref = createRef(); const { rerender } = render(); rerender(); expect(ref.current?.getMarkdown()).toBe(""); }); it("does not replace newer editor text with a stale local echo", async () => { const ref = createRef(); const emitted: string[] = []; const props = { ...baseProps, ref, onContentChange: (markdown: string) => emitted.push(markdown) }; const { rerender } = render(); ref.current?.insertMarkdown("first"); await waitFor(() => expect(emitted).toHaveLength(1)); ref.current?.insertMarkdown("second"); await waitFor(() => expect(emitted).toHaveLength(2)); rerender(); expect(ref.current?.getMarkdown()).toBe(emitted[1]); }); it("enables native text assistance for prose input", () => { const { container } = render(); const root = container.querySelector(".memo-editor-root"); expect(root).toHaveAttribute("autocorrect", "on"); expect(root).toHaveAttribute("autocapitalize", "on"); expect(root).toHaveAttribute("spellcheck", "true"); }); it("updates the placeholder when its translation changes", () => { const { container, rerender } = render(); const root = container.querySelector(".memo-editor-root"); expect(root?.style.getPropertyValue("--mbe-placeholder")).toBe('"Any thoughts?"'); rerender(); expect(root?.style.getPropertyValue("--mbe-placeholder")).toBe('"有什么想法?"'); }); it("draws tags as the memo view does, in the viewer's custom colors", async () => { queries.tagsSetting = undefined; const { container, rerender } = render(); const tag = (name: string) => container.querySelector(`.mbe-tag[data-tag="${name}"]`); expect(tag("life")).toHaveClass("rich:rounded-full", "rich:border-primary"); expect(tag("work")).not.toHaveAttribute("style"); queries.tagsSetting = create(UserSetting_TagsSettingSchema, { tags: { work: { backgroundColor: create(ColorSchema, { red: 1, green: 0, blue: 0 }) } }, }); rerender(); await waitFor(() => expect(tag("work")?.style.getPropertyValue("--tag-color")).toBe("#ff0000")); expect(tag("work")).not.toHaveClass("rich:border-primary"); // The pill is rich text's: every memos class but the text color takes the `rich:` // variant, so source mode, where the editor applies the same classes, keeps plain text. for (const name of ["work", "life"]) { const classes = Array.from(tag(name)!.classList).filter((cls) => !cls.startsWith("mbe-")); expect(classes.filter((cls) => cls.startsWith("text-"))).toHaveLength(1); expect(classes.filter((cls) => !cls.startsWith("text-") && !cls.startsWith("rich:"))).toEqual([]); } queries.tagsSetting = undefined; }); it("keeps a tag's text color and nothing of its pill in source mode", async () => { queries.tagsSetting = create(UserSetting_TagsSettingSchema, { tags: { work: { backgroundColor: create(ColorSchema, { red: 1, green: 0, blue: 0 }) } }, }); const { container } = render(); const tag = (name: string) => container.querySelector(`.mbe-source-mode .mbe-tag[data-tag="${name}"]`); // The editor decorates tags in the source text with the same classes as in rich text; // the `rich:` variant is what keeps the pill out of it. await waitFor(() => expect(tag("work")?.style.getPropertyValue("--tag-color")).toBe("#ff0000")); for (const name of ["work", "life"]) { const classes = Array.from(tag(name)!.classList).filter((cls) => !cls.startsWith("mbe-")); expect(classes.filter((cls) => cls.startsWith("text-"))).toHaveLength(1); expect(classes.filter((cls) => !cls.startsWith("text-") && !cls.startsWith("rich:"))).toEqual([]); } queries.tagsSetting = undefined; }); it("draws mentions of people who exist as the memo view does", async () => { queries.users = new Map([["alice", { name: "users/alice" }]]); const { container } = render(); const mention = (name: string) => container.querySelector(`.mbe-mention[data-mention="${name}"]`); await waitFor(() => expect(mention("alice")).toHaveAttribute("data-resolved"), { timeout: 2000 }); expect(mention("alice")).toHaveClass("text-primary"); expect(mention("nobody")).not.toHaveAttribute("data-resolved"); expect(mention("nobody")).not.toHaveClass("text-primary"); }); });