From 51cead8d816d16cc025e4a189040bca7c0dc3729 Mon Sep 17 00:00:00 2001 From: Leo Goetz Date: Mon, 10 Nov 2025 10:26:15 +0100 Subject: completed course --- tests/notes.test.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/notes.test.js (limited to 'tests') diff --git a/tests/notes.test.js b/tests/notes.test.js new file mode 100644 index 0000000..079642d --- /dev/null +++ b/tests/notes.test.js @@ -0,0 +1,51 @@ +import { jest } from "@jest/globals"; + +jest.unstable_mockModule("../src/db.js", () => ({ + insertDB: jest.fn(), + getDB: jest.fn(), + saveDB: jest.fn(), +})); + +const { insertDB, getDB, saveDB } = await import("../src/db.js"); +const { newNote, getAllNotes, removeNote } = await import("../src/notes.js"); + +beforeEach(() => { + insertDB.mockClear(); + getDB.mockClear(); + saveDB.mockClear(); +}); + +test("newNote inserts data and returns it", async () => { + const note = { + content: "this is a new note", + id: 1, + tags: ["test"], + }; + insertDB.mockResolvedValue(note); + const result = await newNote(note.content, note.tags); + expect(result.content).toEqual(note.content); + expect(result.tags).toEqual(note.tags); +}); + +test("getAllNotes returns all notes", async () => { + const db = { + notes: ["note1", "note2", "note3"], + }; + getDB.mockResolvedValue(db); + + const result = await getAllNotes(); + expect(result).toEqual(db.notes); +}); + +test("removeNote does nothing if id is not found", async () => { + const notes = [ + { id: 1, content: "note 1" }, + { id: 2, content: "note 2" }, + { id: 3, content: "note 3" }, + ]; + saveDB.mockResolvedValue(notes); + + const idToRemove = 4; + const result = await removeNote(idToRemove); + expect(result).toBeUndefined(); +}); -- cgit v1.3