diff options
| author | Leo Goetz <dev@leogtz.de> | 2025-11-10 10:26:15 +0100 |
|---|---|---|
| committer | Leo Goetz <dev@leogtz.de> | 2025-11-10 10:26:15 +0100 |
| commit | 51cead8d816d16cc025e4a189040bca7c0dc3729 (patch) | |
| tree | 2da07e9f5d2224006677460bc29737aa42acdace /tests/notes.test.js | |
| parent | 789d28b83af165258dc7e8b3a44bbd2d48fcc370 (diff) | |
completed course
Diffstat (limited to 'tests/notes.test.js')
| -rw-r--r-- | tests/notes.test.js | 51 |
1 files changed, 51 insertions, 0 deletions
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(); +}); |
