From 7d504724f05c514ae699cf5dbc2706d929f2f4fc Mon Sep 17 00:00:00 2001 From: Ura Date: Sun, 27 Oct 2024 02:24:07 +0900 Subject: [PATCH] notification tested --- task_yell/test/notification.test.ts | 96 +++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 task_yell/test/notification.test.ts diff --git a/task_yell/test/notification.test.ts b/task_yell/test/notification.test.ts new file mode 100644 index 0000000..fd32394 --- /dev/null +++ b/task_yell/test/notification.test.ts @@ -0,0 +1,96 @@ +import { + createNotification, + readNotification, + readSingleNotification, + updateNotification, + deleteNotification, +} from "../src/lib/notifications"; +import { Notification } from "../src/lib/types"; +import { + createData, + readData, + readSingleData, + updateData, + deleteData, +} from "../src/firebase/firestore"; + +// Firebase 認証をモック +jest.mock("../src/firebase/client-app", () => ({ + auth: { + signInWithEmailAndPassword: jest.fn(), + signOut: jest.fn(), + onAuthStateChanged: jest.fn(), + }, + db: jest.fn(), + storage: jest.fn(), + firebaseApp: jest.fn(), +})); + +jest.mock("../src/firebase/firestore", () => ({ + createData: jest.fn(), + readData: jest.fn(), + readSingleData: jest.fn(), + updateData: jest.fn(), + deleteData: jest.fn(), +})); + +describe("Notification CRUD operations", () => { + const mockNotification: Notification = { + datetime: new Date(), + type: "push", + eventOrTaskRef: "mockEventId", + userId: "mockUserId", + }; + + it("should create a notification", async () => { + (createData as jest.Mock).mockResolvedValue("mockNotificationId"); + + const notificationId = await createNotification(mockNotification); + expect(notificationId).toBe("mockNotificationId"); + expect(createData).toHaveBeenCalledWith("notifications", mockNotification); + }); + + it("should read all notifications", async () => { + (readData as jest.Mock).mockResolvedValue([mockNotification]); + + const notifications = await readNotification(); + expect(notifications).toEqual([mockNotification]); + expect(readData).toHaveBeenCalledWith("notifications"); + }); + + it("should read a single notification", async () => { + (readSingleData as jest.Mock).mockResolvedValue(mockNotification); + + const notification = await readSingleNotification("mockNotificationId"); + expect(notification).toEqual(mockNotification); + expect(readSingleData).toHaveBeenCalledWith( + "notifications", + "mockNotificationId", + ); + }); + + it("should update a notification", async () => { + (updateData as jest.Mock).mockResolvedValue(undefined); + + await updateNotification("mockNotificationId", { + eventOrTaskRef: "Updated Event ID", + }); + expect(updateData).toHaveBeenCalledWith( + "notifications", + "mockNotificationId", + { + eventOrTaskRef: "Updated Event ID", + }, + ); + }); + + it("should delete a notification", async () => { + (deleteData as jest.Mock).mockResolvedValue(undefined); + + await deleteNotification("mockNotificationId"); + expect(deleteData).toHaveBeenCalledWith( + "notifications", + "mockNotificationId", + ); + }); +});