From 7c74b0f531bb96800e20d00eea825c61428df43f Mon Sep 17 00:00:00 2001 From: Ura Date: Sun, 27 Oct 2024 01:23:52 +0900 Subject: [PATCH 1/2] Tested Task --- task_yell/test/task.test.ts | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 task_yell/test/task.test.ts diff --git a/task_yell/test/task.test.ts b/task_yell/test/task.test.ts new file mode 100644 index 0000000..fc0955a --- /dev/null +++ b/task_yell/test/task.test.ts @@ -0,0 +1,99 @@ +import { + createTask, + readTasks, + readSingleTask, + updateTask, + deleteTask, +} from "@/lib/tasks"; +import { Task } from "@/lib/types"; +import { + createData, + readData, + readSingleData, + updateData, + deleteData, +} from "@/firebase/firestore"; + +// Firebase 認証をモック +jest.mock("@/firebase/client-app", () => ({ + auth: { + signInWithEmailAndPassword: jest.fn(), + signOut: jest.fn(), + onAuthStateChanged: jest.fn(), + }, + db: jest.fn(), + storage: jest.fn(), + firebaseApp: jest.fn(), +})); + +jest.mock("@/firebase/firestore", () => ({ + createData: jest.fn(), + readData: jest.fn(), + readSingleData: jest.fn(), + updateData: jest.fn(), + deleteData: jest.fn(), +})); + +describe("Task CRUD operations", () => { + const mockTask: Task = { + title: "Test Task", + description: "This is a test task", + due: new Date(), + importance: "high", + reccurence: ["weekly"], + }; + + const mockUserId = "mockUserId"; + + it("should create a task", async () => { + (createData as jest.Mock).mockResolvedValue("mockTaskId"); + + await createTask(mockUserId, mockTask); + expect(createData).toHaveBeenCalledWith( + `users/${mockUserId}/tasks`, + mockTask + ); + }); + + it("should read all tasks", async () => { + (readData as jest.Mock).mockResolvedValue([mockTask]); + + const tasks = await readTasks(mockUserId); + expect(tasks).toEqual([mockTask]); + expect(readData).toHaveBeenCalledWith(`users/${mockUserId}/tasks`); + }); + + it("should read a single task", async () => { + (readSingleData as jest.Mock).mockResolvedValue(mockTask); + + const task = await readSingleTask(mockUserId, "mockTaskId"); + expect(task).toEqual(mockTask); + expect(readSingleData).toHaveBeenCalledWith( + `users/${mockUserId}/tasks`, + "mockTaskId" + ); + }); + + it("should update a task", async () => { + (updateData as jest.Mock).mockResolvedValue(undefined); + + await updateTask(mockUserId, "mockTaskId", { title: "Updated Task" }); + expect(updateData).toHaveBeenCalledWith( + `users/${mockUserId}/tasks`, + "mockTaskId", + { + title: "Updated Task", + } + ); + }); + + it("should delete a task", async () => { + (deleteData as jest.Mock).mockResolvedValue(undefined); + + await deleteTask(mockUserId, "mockTaskId"); + expect(deleteData).toHaveBeenCalledWith( + `users/${mockUserId}/tasks`, + "mockTaskId" + ); + }); +}); From d5563572be21fdccd9a713724f2283e80fef2a64 Mon Sep 17 00:00:00 2001 From: Ura Date: Sun, 27 Oct 2024 01:28:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E?= =?UTF-8?q?=E3=83=83=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task_yell/test/task.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/task_yell/test/task.test.ts b/task_yell/test/task.test.ts index fc0955a..b957ed2 100644 --- a/task_yell/test/task.test.ts +++ b/task_yell/test/task.test.ts @@ -51,7 +51,7 @@ describe("Task CRUD operations", () => { await createTask(mockUserId, mockTask); expect(createData).toHaveBeenCalledWith( `users/${mockUserId}/tasks`, - mockTask + mockTask, ); }); @@ -70,7 +70,7 @@ describe("Task CRUD operations", () => { expect(task).toEqual(mockTask); expect(readSingleData).toHaveBeenCalledWith( `users/${mockUserId}/tasks`, - "mockTaskId" + "mockTaskId", ); }); @@ -83,7 +83,7 @@ describe("Task CRUD operations", () => { "mockTaskId", { title: "Updated Task", - } + }, ); }); @@ -93,7 +93,7 @@ describe("Task CRUD operations", () => { await deleteTask(mockUserId, "mockTaskId"); expect(deleteData).toHaveBeenCalledWith( `users/${mockUserId}/tasks`, - "mockTaskId" + "mockTaskId", ); }); });