Skip to content

Commit

Permalink
履修プラン更新ページのテストを記述
Browse files Browse the repository at this point in the history
  • Loading branch information
kameiryohei committed Oct 12, 2024
1 parent 48898fe commit 03f6c38
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions __test__/UpdatePage.test.tsx
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 03f6c38

Please sign in to comment.