notesService.test.ts75 lines, 1.9 KB
| 1 | import { ServiceError } from "../../../shared/types"; |
| 2 | import { NotesService } from "../src/notesService"; |
| 3 | import { resetAllMocks, testNote } from "./setup"; |
| 4 | |
| 5 | async function expectServiceError( |
| 6 | asyncFn: () => Promise<any>, |
| 7 | expectedMessage: string, |
| 8 | expectedStatusCode: number |
| 9 | ) { |
| 10 | try { |
| 11 | await asyncFn(); |
| 12 | fail("Expected function to throw ServiceError"); |
| 13 | } catch (error) { |
| 14 | expect(error).toBeInstanceOf(ServiceError); |
| 15 | expect(error.message).toBe(expectedMessage); |
| 16 | expect(error.statusCode).toBe(expectedStatusCode); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | describe("NotesService", () => { |
| 21 | let notesService: NotesService; |
| 22 | |
| 23 | beforeEach(() => { |
| 24 | resetAllMocks(); |
| 25 | notesService = new NotesService(); |
| 26 | }); |
| 27 | |
| 28 | describe("createNote", () => { |
| 29 | const userId = "test-user-id-123"; |
| 30 | |
| 31 | it("should successfully create a note without tags", async () => { |
| 32 | global.mockPrisma.note.create.mockResolvedValue(testNote); |
| 33 | |
| 34 | const result = await notesService.createNote(userId, { |
| 35 | title: "Test Note", |
| 36 | content: "This is a test note", |
| 37 | }); |
| 38 | |
| 39 | expect(global.mockPrisma.note.create).toHaveBeenCalledWith({ |
| 40 | data: { |
| 41 | userId, |
| 42 | title: "Test Note", |
| 43 | content: "This is a test note", |
| 44 | }, |
| 45 | include: { |
| 46 | noteTags: true, |
| 47 | }, |
| 48 | }); |
| 49 | expect(result).toEqual(testNote); |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | describe("getNoteById", () => { |
| 54 | const noteId = "test-note-id-123"; |
| 55 | const userId = "test-user-id-123"; |
| 56 | |
| 57 | it("should successfully retrieve a note", async () => { |
| 58 | global.mockPrisma.note.findFirst.mockResolvedValue(testNote); |
| 59 | |
| 60 | const result = await notesService.getNoteById(noteId, userId); |
| 61 | |
| 62 | expect(global.mockPrisma.note.findFirst).toHaveBeenCalledWith({ |
| 63 | where: { |
| 64 | id: noteId, |
| 65 | userId, |
| 66 | isDeleted: false, |
| 67 | }, |
| 68 | include: { |
| 69 | noteTags: true, |
| 70 | }, |
| 71 | }); |
| 72 | expect(result).toEqual(testNote); |
| 73 | }); |
| 74 | }); |
| 75 | }); |