From 03f6c38277fe7eca5c4e925fb4e25c209bebdf5e Mon Sep 17 00:00:00 2001 From: Ryohei Kamei <231205751@ccmailg.meijo-u.ac.jp> Date: Sat, 12 Oct 2024 23:18:20 +0900 Subject: [PATCH] =?UTF-8?q?=E5=B1=A5=E4=BF=AE=E3=83=97=E3=83=A9=E3=83=B3?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=A8=98=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __test__/UpdatePage.test.tsx | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 __test__/UpdatePage.test.tsx diff --git a/__test__/UpdatePage.test.tsx b/__test__/UpdatePage.test.tsx new file mode 100644 index 0000000..3836abe --- /dev/null +++ b/__test__/UpdatePage.test.tsx @@ -0,0 +1,59 @@ +import { render, waitFor, screen, act } from "@testing-library/react"; +import { config } from "lib/config"; +import UpdatePage from "app/updatePlan/[id]/page"; +import { mockData } from "./mock/mock_UpdatePage"; + +jest.mock("next/headers", () => ({ + headers: jest.fn(() => ({ + get: jest.fn(() => "test-host"), + })), +})); + +jest.mock("next/navigation", () => ({ + useRouter: jest.fn(), +})); + +global.fetch = jest.fn(); //fetch関数をモック化 + +describe("UpdatePageコンポーネントに関するテスト", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + test("APIから正しくデータを取得する", async () => { + (global.fetch as jest.Mock).mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce(mockData), + }); + + render(await UpdatePage({ params: { id: 1 } })); + await act(async () => {}); + + await waitFor(() => { + expect(global.fetch).toHaveBeenCalledWith( + `${config.apiPrefix}test-host/api/plan/update/1`, //仮としてidが1のデータを取得する + expect.objectContaining({ + cache: "no-store", + method: "GET", + headers: { + "x-api-key": expect.any(String), + }, + }) + ); + }); + }); + + test("UpdatePageCoreに正しいpropsを渡し、それが画面に表示されていることを確認する", async () => { + (global.fetch as jest.Mock).mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce(mockData), + }); + + render(await UpdatePage({ params: { id: 1 } })); + + await act(async () => {}); + + expect(screen.getByDisplayValue("テストタイトル")).toBeInTheDocument(); + expect(screen.getByDisplayValue("テスト内容")).toBeInTheDocument(); + expect(screen.getByDisplayValue("講座1")).toBeInTheDocument(); + expect(screen.getByDisplayValue("講座2")).toBeInTheDocument(); + }); +});