forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

notes.test.ts135 lines, 4.5 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getNotes } from "../../notes";
3import { getNoteById } from "../../notes";
4import { postNote } from "../../notes";
5import { patchNoteById } from "../../notes";
6import { deleteNoteById } from "../../notes";
7import { getNoteFiles } from "../../notes";
8import { postNoteFile } from "../../notes";
9import { deleteNoteFileById } from "../../notes";
10import { getNoteFileUrl } from "../../notes";
11import { getNoteFileBlob } from "../../notes";
12import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch";
13
14describe("notes API", () => {
15 afterEach(() => {
16 vi.restoreAllMocks();
17 });
18
19 it("getNotes calls /notes with pagination", async () => {
20 const mockPage = { items: [{ id: "n1", title: "Note" }], total: 1 };
21 mockFetchSuccess(mockPage);
22
23 const result = await getNotes({ limit: 5 });
24 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
25
26 const [url, init] = lastFetchCall();
27 expect(url).toBe("/api/notes?limit=5");
28 expect(init?.method).toBe("GET");
29 });
30
31 it("postNote calls /notes with data", async () => {
32 const mockNote = { id: "n1", title: "Note", content: "Content" };
33 mockFetchSuccess(mockNote);
34
35 const data = { title: "Note", content: "Content" };
36 const result = await postNote(data);
37 expect(result).toEqual(mockNote);
38
39 const [url, init] = lastFetchCall();
40 expect(url).toBe("/api/notes");
41 expect(init?.method).toBe("POST");
42 expect(init?.body).toBe(JSON.stringify(data));
43 });
44
45 it("getNoteById calls /notes/:id", async () => {
46 const mockNote = { id: "n1", title: "Note", content: "Content" };
47 mockFetchSuccess(mockNote);
48
49 const result = await getNoteById("n1");
50 expect(result).toEqual(mockNote);
51
52 const [url, init] = lastFetchCall();
53 expect(url).toBe("/api/notes/n1");
54 expect(init?.method).toBe("GET");
55 });
56
57 it("patchNoteById calls /notes/:id with updates", async () => {
58 const mockNote = { id: "n1", title: "Updated", content: "Content" };
59 mockFetchSuccess(mockNote);
60
61 const updates = { title: "Updated" };
62 const result = await patchNoteById("n1", updates);
63 expect(result).toEqual(mockNote);
64
65 const [url, init] = lastFetchCall();
66 expect(url).toBe("/api/notes/n1");
67 expect(init?.method).toBe("PATCH");
68 expect(init?.body).toBe(JSON.stringify(updates));
69 });
70
71 it("deleteNoteById calls /notes/:id", async () => {
72 mockFetch204();
73
74 await deleteNoteById("n1");
75
76 const [url, init] = lastFetchCall();
77 expect(url).toBe("/api/notes/n1");
78 expect(init?.method).toBe("DELETE");
79 });
80
81 it("getNoteFiles calls /notes/:id/files with pagination", async () => {
82 const mockPage = { items: [{ id: "f1", name: "test.pdf" }], total: 1 };
83 mockFetchSuccess(mockPage);
84
85 const result = await getNoteFiles("n1", { limit: 10 });
86 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
87
88 const [url, init] = lastFetchCall();
89 expect(url).toBe("/api/notes/n1/files?limit=10");
90 expect(init?.method).toBe("GET");
91 });
92
93 it("postNoteFile calls /notes/:id/files with FormData", async () => {
94 const mockFile = { id: "f1", name: "test.pdf", content_type: "application/pdf", size: 100 };
95 mockFetchSuccess(mockFile);
96
97 const file = new File(["test"], "test.pdf", { type: "application/pdf" });
98 const result = await postNoteFile("n1", file);
99 expect(result).toEqual(mockFile);
100
101 const [url, init] = lastFetchCall();
102 expect(url).toBe("/api/notes/n1/files");
103 expect(init?.method).toBe("POST");
104 expect(init?.body).toBeInstanceOf(FormData);
105 expect((init?.body as FormData).get("file")).toBe(file);
106 });
107
108 it("deleteNoteFileById calls /notes/:id/files/:fileId", async () => {
109 mockFetch204();
110
111 await deleteNoteFileById("n1", "f1");
112
113 const [url, init] = lastFetchCall();
114 expect(url).toBe("/api/notes/n1/files/f1");
115 expect(init?.method).toBe("DELETE");
116 });
117
118 it("getNoteFileUrl returns correct URL path", () => {
119 const url = getNoteFileUrl("n1", "f1");
120 expect(url).toBe("/api/notes/n1/files/f1");
121 });
122
123 it("getNoteFileBlob fetches file blob", async () => {
124 const blob = new Blob(["test"], { type: "application/pdf" });
125 mockFetchBlob(blob);
126
127 const result = await getNoteFileBlob("n1", "f1");
128 expect(result).toBeInstanceOf(Blob);
129 expect(result.type).toBe("application/pdf");
130
131 const [url, init] = lastFetchCall();
132 expect(url).toBe("/api/notes/n1/files/f1");
133 expect(init?.credentials).toBe("same-origin");
134 });
135});