UNPKG

local-fake-api

Version:

A simple async local mock API without backend.

77 lines (76 loc) 3.65 kB
import { createFakeApi } from "../fakeApi/createFakeApi"; describe("createFakeApi", () => { const api = createFakeApi("test_notes"); beforeAll(() => { const store = {}; global.localStorage = { getItem: (key) => { var _a; return (_a = store[key]) !== null && _a !== void 0 ? _a : null; }, setItem: (key, value) => { store[key] = value; }, removeItem: (key) => { delete store[key]; }, clear: () => { Object.keys(store).forEach((key) => delete store[key]); }, }; }); let note1; let note2; let note3; beforeEach(async () => { await api.clearAll(); note1 = await api.add({ title: "First Title", content: "First Content" }); note2 = await api.add({ title: "Second Title", content: "Second Content" }); note3 = await api.add({ title: "Third Title", content: "Third Content" }); }); it("lists all added items", async () => { const notes = await api.list(); expect(notes).toHaveLength(3); expect(notes).toEqual(expect.arrayContaining([ expect.objectContaining({ title: "First Title", content: "First Content" }), expect.objectContaining({ title: "Second Title", content: "Second Content" }), expect.objectContaining({ title: "Third Title", content: "Third Content" }), ])); }); it("fetches items by their ids", async () => { const fetched1 = await api.get(note1.id); expect(fetched1).not.toBeNull(); expect(fetched1 === null || fetched1 === void 0 ? void 0 : fetched1.title).toBe("First Title"); expect(fetched1 === null || fetched1 === void 0 ? void 0 : fetched1.content).toBe("First Content"); const fetched2 = await api.get(note2.id); expect(fetched2).not.toBeNull(); expect(fetched2 === null || fetched2 === void 0 ? void 0 : fetched2.title).toBe("Second Title"); expect(fetched2 === null || fetched2 === void 0 ? void 0 : fetched2.content).toBe("Second Content"); const fetched3 = await api.get(note3.id); expect(fetched3).not.toBeNull(); expect(fetched3 === null || fetched3 === void 0 ? void 0 : fetched3.title).toBe("Third Title"); expect(fetched3 === null || fetched3 === void 0 ? void 0 : fetched3.content).toBe("Third Content"); }); it("updates an existing item", async () => { const updated = await api.update(note2.id, { title: "Updated Title" }); expect(updated).not.toBeNull(); expect(updated === null || updated === void 0 ? void 0 : updated.title).toBe("Updated Title"); // Confirm other properties remain unchanged expect(updated === null || updated === void 0 ? void 0 : updated.content).toBe("Second Content"); // Verify update is persisted const fetched = await api.get(note2.id); expect(fetched === null || fetched === void 0 ? void 0 : fetched.title).toBe("Updated Title"); }); it("returns null when updating a non-existent item", async () => { const updated = await api.update("non-existent-id", { title: "Nope" }); expect(updated).toBeNull(); }); it("deletes an item", async () => { await api.delete(note1.id); const notes = await api.list(); expect(notes).toHaveLength(2); expect(notes.find((n) => n.id === note1.id)).toBeUndefined(); }); it("clears all items", async () => { await api.clearAll(); const notes = await api.list(); expect(notes).toHaveLength(0); }); });