Skip to content

Commit

Permalink
test: PopulationSelector コンポーネントのテスト追加
Browse files Browse the repository at this point in the history
  • Loading branch information
crab85193 committed Dec 17, 2024
1 parent 6f3dadc commit 7825a2e
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/tests/PopulationSelector.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import PopulationSelector from "./../components/PopulationSelector";
import { fetchPopulation } from "../api/population";
import { Prefecture } from "../types/prefecture";
import { PopulationCategory } from "../types/population";

jest.mock("../api/population");

describe("PopulationSelector", () => {
const mockPrefectures: Prefecture[] = [
{ prefCode: 1, prefName: "北海道" },
{ prefCode: 2, prefName: "青森県" },
];

it("都道府県を選択し、人口データが表示される", async () => {
const mockPopulationData: PopulationCategory[] = [
{
label: "年少人口",
data: [{ year: 1960, value: 1681479, rate: 33.37 }],
},
];

(fetchPopulation as jest.Mock).mockResolvedValue(mockPopulationData);

render(<PopulationSelector prefectures={mockPrefectures} />);

fireEvent.change(screen.getByRole("combobox"), { target: { value: "1" } });

await waitFor(() => screen.getByText("年少人口"));

expect(screen.getByText("年少人口")).toBeInTheDocument();
expect(
screen.getByText("1960: 1681479人 (比率: 33.37%)")
).toBeInTheDocument();
});

it("人口データの取得中にエラーメッセージが表示される", async () => {
(fetchPopulation as jest.Mock).mockRejectedValue(new Error("API Error"));

render(<PopulationSelector prefectures={mockPrefectures} />);

fireEvent.change(screen.getByRole("combobox"), { target: { value: "1" } });

await waitFor(() => screen.getByText(/人口データの取得に失敗しました/i));

expect(
screen.getByText(/人口データの取得に失敗しました/i)
).toBeInTheDocument();
});
});

0 comments on commit 7825a2e

Please sign in to comment.