memos/web/tests/use-memo-init.test.tsx

62 lines
2 KiB
TypeScript

import { create } from "@bufbuild/protobuf";
import { render, waitFor } from "@testing-library/react";
import type { RefObject } from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useMemoInit } from "@/components/MemoEditor/hooks/useMemoInit";
import { cacheService } from "@/components/MemoEditor/services/cacheService";
import { EditorProvider, useEditorContext } from "@/components/MemoEditor/state";
import type { EditorController } from "@/components/MemoEditor/types/editorController";
import { AttachmentSchema } from "@/types/proto/api/v1/attachment_service_pb";
const editorRef = { current: null } as RefObject<EditorController | null>;
let getEditorState: ReturnType<typeof useEditorContext>["getState"];
function Probe() {
getEditorState = useEditorContext().getState;
useMemoInit({
editorRef,
username: "users/steven",
cacheKey: "restored-draft",
});
return null;
}
describe("useMemoInit", () => {
beforeEach(() => {
const storage = new Map<string, string>();
vi.stubGlobal("localStorage", {
getItem: vi.fn((key: string) => storage.get(key) ?? null),
setItem: vi.fn((key: string, value: string) => storage.set(key, value)),
removeItem: vi.fn((key: string) => storage.delete(key)),
});
cacheService.clearAll();
});
afterEach(() => {
vi.unstubAllGlobals();
});
it("restores uploaded attachment bindings with a new memo draft", async () => {
const attachment = create(AttachmentSchema, {
name: "attachments/image-one",
filename: "image.png",
type: "image/png",
});
cacheService.saveNow(
cacheService.key("users/steven", "restored-draft"),
"![image](/file/attachments/image-one)",
[attachment],
);
render(
<EditorProvider>
<Probe />
</EditorProvider>,
);
await waitFor(() => {
expect(getEditorState().content).toBe("![image](/file/attachments/image-one)");
expect(getEditorState().metadata.attachments).toEqual([attachment]);
});
});
});