Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

chore: write unit tests for add note

Khuram Niaz056c6b3d586d
diff --git a/__tests__/note.actions.spec.ts b/__tests__/note.actions.spec.ts
new file mode 100644
index 0000000..41724c2
--- /dev/null
+++ b/__tests__/note.actions.spec.ts
@@ -0,0 +1,290 @@
+import {
+ getNotesByJobId,
+ addNote,
+ updateNote,
+ deleteNote,
+} from "@/actions/note.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+jest.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ note: {
+ findMany: jest.fn(),
+ create: jest.fn(),
+ update: jest.fn(),
+ delete: jest.fn(),
+ },
+ job: {
+ findFirst: jest.fn(),
+ },
+ };
+ return { PrismaClient: jest.fn(() => mPrismaClient) };
+});
+
+jest.mock("@/utils/user.utils", () => ({
+ getCurrentUser: jest.fn(),
+}));
+
+describe("noteActions", () => {
+ const mockUser = { id: "user-id" };
+ const now = new Date();
+ const mockNote = {
+ id: "note-id",
+ jobId: "job-id",
+ userId: mockUser.id,
+ content: "<p>Interview went well</p>",
+ createdAt: now,
+ updatedAt: now,
+ };
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ describe("getNotesByJobId", () => {
+ it("should return notes ordered newest-first", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+ (prisma.note.findMany as jest.Mock).mockResolvedValue([mockNote]);
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result).toEqual({
+ success: true,
+ data: [{ ...mockNote, isEdited: false }],
+ });
+ expect(prisma.note.findMany).toHaveBeenCalledWith({
+ where: { jobId: "job-id", userId: mockUser.id },
+ orderBy: { createdAt: "desc" },
+ });
+ });
+
+ it("should mark notes as edited when updatedAt differs from createdAt by more than 1 second", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+
+ const createdAt = new Date("2026-01-01T10:00:00Z");
+ const updatedAt = new Date("2026-01-01T10:05:00Z");
+ const editedNote = { ...mockNote, createdAt, updatedAt };
+ (prisma.note.findMany as jest.Mock).mockResolvedValue([editedNote]);
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result.data[0].isEdited).toBe(true);
+ });
+
+ it("should not mark notes as edited when updatedAt is within 1 second of createdAt", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+
+ const createdAt = new Date("2026-01-01T10:00:00.000Z");
+ const updatedAt = new Date("2026-01-01T10:00:00.500Z");
+ const freshNote = { ...mockNote, createdAt, updatedAt };
+ (prisma.note.findMany as jest.Mock).mockResolvedValue([freshNote]);
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result.data[0].isEdited).toBe(false);
+ });
+
+ it("should return empty array when no notes exist", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+ (prisma.note.findMany as jest.Mock).mockResolvedValue([]);
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result).toEqual({ success: true, data: [] });
+ });
+
+ it("should return error when job is not found", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue(null);
+
+ const result = await getNotesByJobId("non-existent-job");
+
+ expect(result).toEqual({ success: false, message: "Job not found" });
+ });
+
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockRejectedValue(
+ new Error("Database error")
+ );
+
+ const result = await getNotesByJobId("job-id");
+
+ expect(result).toEqual({ success: false, message: "Database error" });
+ });
+ });
+
+ describe("addNote", () => {
+ const noteData = { jobId: "job-id", content: "<p>New note</p>" };
+
+ it("should create a note successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+ (prisma.note.create as jest.Mock).mockResolvedValue(mockNote);
+
+ const result = await addNote(noteData);
+
+ expect(result).toEqual({ success: true, data: mockNote });
+ expect(prisma.note.create).toHaveBeenCalledWith({
+ data: {
+ jobId: "job-id",
+ userId: mockUser.id,
+ content: "<p>New note</p>",
+ },
+ });
+ });
+
+ it("should verify job ownership before creating", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue(null);
+
+ const result = await addNote(noteData);
+
+ expect(result).toEqual({ success: false, message: "Job not found" });
+ expect(prisma.note.create).not.toHaveBeenCalled();
+ });
+
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await addNote(noteData);
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ });
+
+ it("should reject empty content via validation", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const result = await addNote({ jobId: "job-id", content: "" });
+
+ expect(result.success).toBe(false);
+ expect(result.message).toBeTruthy();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.findFirst as jest.Mock).mockResolvedValue({ id: "job-id" });
+ (prisma.note.create as jest.Mock).mockRejectedValue(
+ new Error("Database error")
+ );
+
+ const result = await addNote(noteData);
+
+ expect(result).toEqual({ success: false, message: "Database error" });
+ });
+ });
+
+ describe("updateNote", () => {
+ const updateData = {
+ id: "note-id",
+ jobId: "job-id",
+ content: "<p>Updated content</p>",
+ };
+
+ it("should update a note successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ const updatedNote = { ...mockNote, content: updateData.content };
+ (prisma.note.update as jest.Mock).mockResolvedValue(updatedNote);
+
+ const result = await updateNote(updateData);
+
+ expect(result).toEqual({ success: true, data: updatedNote });
+ expect(prisma.note.update).toHaveBeenCalledWith({
+ where: { id: "note-id", userId: mockUser.id },
+ data: { content: "<p>Updated content</p>" },
+ });
+ });
+
+ it("should return error when note ID is missing", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const result = await updateNote({ jobId: "job-id", content: "test" });
+
+ expect(result).toEqual({
+ success: false,
+ message: "Note ID is required for update",
+ });
+ });
+
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await updateNote(updateData);
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.note.update as jest.Mock).mockRejectedValue(
+ new Error("Database error")
+ );
+
+ const result = await updateNote(updateData);
+
+ expect(result).toEqual({ success: false, message: "Database error" });
+ });
+ });
+
+ describe("deleteNote", () => {
+ it("should delete a note successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.note.delete as jest.Mock).mockResolvedValue(mockNote);
+
+ const result = await deleteNote("note-id");
+
+ expect(result).toEqual({ success: true });
+ expect(prisma.note.delete).toHaveBeenCalledWith({
+ where: { id: "note-id", userId: mockUser.id },
+ });
+ });
+
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await deleteNote("note-id");
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.note.delete as jest.Mock).mockRejectedValue(
+ new Error("Database error")
+ );
+
+ const result = await deleteNote("note-id");
+
+ expect(result).toEqual({ success: false, message: "Database error" });
+ });
+
+ it("should handle deleting non-existent note", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.note.delete as jest.Mock).mockRejectedValue(
+ new Error("Record to delete does not exist.")
+ );
+
+ const result = await deleteNote("non-existent-id");
+
+ expect(result).toEqual({
+ success: false,
+ message: "Record to delete does not exist.",
+ });
+ });
+ });
+});
diff --git a/__tests__/note.schema.spec.ts b/__tests__/note.schema.spec.ts
new file mode 100644
index 0000000..1519b8a
--- /dev/null
+++ b/__tests__/note.schema.spec.ts
@@ -0,0 +1,58 @@
+import { NoteFormSchema } from "@/models/note.schema";
+
+describe("NoteFormSchema", () => {
+ describe("valid data", () => {
+ it("should accept valid note data", () => {
+ const data = { jobId: "job-123", content: "Some note content" };
+ const result = NoteFormSchema.parse(data);
+ expect(result.jobId).toBe("job-123");
+ expect(result.content).toBe("Some note content");
+ });
+
+ it("should accept data with optional id for editing", () => {
+ const data = {
+ id: "note-123",
+ jobId: "job-123",
+ content: "Updated content",
+ };
+ const result = NoteFormSchema.parse(data);
+ expect(result.id).toBe("note-123");
+ });
+
+ it("should accept HTML content from rich text editor", () => {
+ const data = {
+ jobId: "job-123",
+ content: "<p>Interview prep: <strong>focus on system design</strong></p>",
+ };
+ const result = NoteFormSchema.parse(data);
+ expect(result.content).toContain("<strong>");
+ });
+
+ it("should accept minimal content (1 character)", () => {
+ const data = { jobId: "job-123", content: "x" };
+ const result = NoteFormSchema.parse(data);
+ expect(result.content).toBe("x");
+ });
+ });
+
+ describe("invalid data", () => {
+ it("should reject empty content", () => {
+ const data = { jobId: "job-123", content: "" };
+ expect(() => NoteFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject missing jobId", () => {
+ const data = { content: "Some content" };
+ expect(() => NoteFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject missing content", () => {
+ const data = { jobId: "job-123" };
+ expect(() => NoteFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject empty object", () => {
+ expect(() => NoteFormSchema.parse({})).toThrow();
+ });
+ });
+});