forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

note-document.test.tsx124 lines, 5.0 KiBHistoryRawDownload
1import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library";
2import type { JSX } from "solid-js";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4import { NoteDocument, stageFiles, uploadStagedFiles } from "@/components/notes/note-document";
5import { PreferencesProvider } from "@/stores/preferences-context";
6
7const { getSettings, navigate, postNote, postNoteFile, showToast } = vi.hoisted(() => ({
8 getSettings: vi.fn(),
9 navigate: vi.fn(),
10 postNote: vi.fn(),
11 postNoteFile: vi.fn(),
12 showToast: vi.fn(),
13}));
14
15vi.mock("@tanstack/solid-router", () => ({
16 Link: (props: { to: string; children: JSX.Element }) => <a href={props.to}>{props.children}</a>,
17 useBlocker: () => () => ({ status: "unblocked" }),
18 useNavigate: () => navigate,
19}));
20vi.mock("@/api/settings", () => ({ getSettings }));
21vi.mock("@/api/notes", () => ({
22 deleteNoteById: vi.fn(),
23 deleteNoteFileById: vi.fn(),
24 getNoteFileBlob: vi.fn(),
25 getNoteFileUrl: vi.fn(),
26 getNoteFiles: vi.fn(),
27 patchNoteById: vi.fn(),
28 postNote,
29 postNoteFile,
30}));
31vi.mock("@/components/notes/note-files-panel", () => ({ NoteFilesPanel: () => null }));
32vi.mock("@/components/notes/note-rich-editor", () => ({
33 NoteRichEditor: (props: { actions?: JSX.Element; onChange: (value: string) => void }) => (
34 <div>
35 {props.actions}
36 <textarea aria-label="note body" onInput={(event) => props.onChange(event.currentTarget.value)} />
37 </div>
38 ),
39}));
40vi.mock("@/components/ui/confirm-dialog", () => ({ ConfirmDialog: () => null }));
41vi.mock("@/components/ui/toast", () => ({ showToast }));
42
43const file = (name: string, size: number) => new File([new Uint8Array(size)], name, { type: "text/plain" });
44
45describe("NoteDocument staged attachments", () => {
46 beforeEach(() => {
47 getSettings.mockResolvedValue({ max_file_bytes: 10 });
48 navigate.mockResolvedValue(undefined);
49 postNote.mockResolvedValue({ id: "note-1", title: "Study", content: "" });
50 postNoteFile.mockResolvedValue({ id: "file-1" });
51 });
52
53 afterEach(() => {
54 cleanup();
55 vi.clearAllMocks();
56 });
57
58 it("keeps valid files, rejects oversized files, and stops at the count limit", () => {
59 const result = stageFiles([file("existing.txt", 1)], [file("ok.txt", 2), file("large.txt", 11), file("extra.txt", 2)], 2, 10);
60
61 expect(result.files.map((item) => item.name)).toEqual(["existing.txt", "ok.txt"]);
62 expect(result.tooLarge).toBe(true);
63 expect(result.atLimit).toBe(true);
64 });
65
66 it("shows staged files and lets the user remove one before saving", async () => {
67 const { container } = render(() => (
68 <PreferencesProvider>
69 <NoteDocument />
70 </PreferencesProvider>
71 ));
72 const input = container.querySelector('input[type="file"]') as HTMLInputElement;
73 fireEvent.change(input, { target: { files: [file("study.txt", 3), file("plan.txt", 4)] } });
74
75 expect(screen.getByText("study.txt")).toBeTruthy();
76 expect(screen.getByText("3 B")).toBeTruthy();
77 expect(screen.getByText(/staged files will upload|dosyalar notu kaydettiğinde/)).toBeTruthy();
78
79 fireEvent.click(screen.getByRole("button", { name: /Remove file: study.txt|Dosyayı kaldır: study.txt/ }));
80 expect(screen.queryByText("study.txt")).toBeNull();
81 expect(screen.getByText("plan.txt")).toBeTruthy();
82 });
83
84 it("creates the note before uploading staged files, sequentially", async () => {
85 const calls: string[] = [];
86 postNote.mockImplementation(async () => {
87 calls.push("create");
88 return { id: "note-1", title: "Study", content: "" };
89 });
90 postNoteFile.mockImplementation(async (_noteId: string, item: File) => {
91 calls.push(item.name);
92 if (item.name === "failed.txt") throw new Error("upload failed");
93 return { id: item.name };
94 });
95
96 const { container } = render(() => (
97 <PreferencesProvider>
98 <NoteDocument />
99 </PreferencesProvider>
100 ));
101 fireEvent.input(screen.getByLabelText(/Title|Başlık/), { target: { value: "Study" } });
102 fireEvent.change(container.querySelector('input[type="file"]') as HTMLInputElement, {
103 target: { files: [file("first.txt", 2), file("failed.txt", 2), file("last.txt", 2)] },
104 });
105 fireEvent.click(screen.getByRole("button", { name: /Save|Kaydet/ }));
106
107 await waitFor(() => expect(navigate).toHaveBeenCalled());
108 expect(calls).toEqual(["create", "first.txt", "failed.txt", "last.txt"]);
109 expect(navigate).toHaveBeenCalledWith({ to: "/notes/$id", params: { id: "note-1" }, replace: true });
110 expect(showToast).toHaveBeenCalledWith({ title: expect.stringContaining("failed.txt") });
111 });
112
113 it("reports upload failures by name while continuing in order", async () => {
114 const calls: string[] = [];
115 const failed = await uploadStagedFiles("note-1", [file("one.txt", 1), file("two.txt", 1)], async (_id, item) => {
116 calls.push(item.name);
117 if (item.name === "one.txt") throw new Error("failed");
118 return { id: "file-2" };
119 });
120
121 expect(calls).toEqual(["one.txt", "two.txt"]);
122 expect(failed).toEqual(["one.txt"]);
123 });
124});