|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + query: vi.fn(), |
| 5 | + getCurrentUser: vi.fn(), |
| 6 | + revalidatePath: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("@cap/database", async () => { |
| 10 | + const { drizzle } = await import("drizzle-orm/mysql-proxy"); |
| 11 | + const client = drizzle(mocks.query); |
| 12 | + return { db: () => client }; |
| 13 | +}); |
| 14 | +vi.mock("@cap/database/auth/session", () => ({ |
| 15 | + getCurrentUser: mocks.getCurrentUser, |
| 16 | +})); |
| 17 | +vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath })); |
| 18 | + |
| 19 | +import { markAsRead } from "@/actions/notifications/mark-as-read"; |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + mocks.getCurrentUser.mockResolvedValue({ id: "user-1" }); |
| 23 | + mocks.query.mockResolvedValue({ rows: [{ affectedRows: 1, insertId: 0 }] }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("notification read state", () => { |
| 27 | + it.each(["own-notification", "another-users-notification"])( |
| 28 | + "scopes an individual update to the current recipient for %s", |
| 29 | + async (notificationId) => { |
| 30 | + await markAsRead(notificationId); |
| 31 | + |
| 32 | + expect(mocks.query).toHaveBeenCalledExactlyOnceWith( |
| 33 | + expect.stringContaining( |
| 34 | + "where (`notifications`.`recipientId` = ? and `notifications`.`id` = ?)", |
| 35 | + ), |
| 36 | + [expect.any(String), "user-1", notificationId], |
| 37 | + "execute", |
| 38 | + ); |
| 39 | + expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard"); |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + it("marks all notifications for the current recipient when no ID is supplied", async () => { |
| 44 | + await markAsRead(); |
| 45 | + |
| 46 | + expect(mocks.query).toHaveBeenCalledExactlyOnceWith( |
| 47 | + "update `notifications` set `readAt` = ? where `notifications`.`recipientId` = ?", |
| 48 | + [expect.any(String), "user-1"], |
| 49 | + "execute", |
| 50 | + ); |
| 51 | + expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("succeeds without exposing whether a notification matched", async () => { |
| 55 | + mocks.query.mockResolvedValueOnce({ |
| 56 | + rows: [{ affectedRows: 0, insertId: 0 }], |
| 57 | + }); |
| 58 | + |
| 59 | + await expect(markAsRead("missing-notification")).resolves.toBeUndefined(); |
| 60 | + |
| 61 | + expect(mocks.revalidatePath).toHaveBeenCalledWith("/dashboard"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not write when the requester is not authenticated", async () => { |
| 65 | + mocks.getCurrentUser.mockResolvedValueOnce(null); |
| 66 | + |
| 67 | + await expect(markAsRead("notification-1")).rejects.toThrow( |
| 68 | + "User not found", |
| 69 | + ); |
| 70 | + |
| 71 | + expect(mocks.query).not.toHaveBeenCalled(); |
| 72 | + expect(mocks.revalidatePath).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("reports a failed write without revalidating the dashboard", async () => { |
| 76 | + mocks.query.mockRejectedValueOnce(new Error("Database unavailable")); |
| 77 | + vi.spyOn(console, "log").mockImplementation(() => {}); |
| 78 | + |
| 79 | + await expect(markAsRead("notification-1")).rejects.toThrow( |
| 80 | + "Error marking notification(s) as read", |
| 81 | + ); |
| 82 | + |
| 83 | + expect(mocks.revalidatePath).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments