Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions src/components/__tests__/DashboardStatsClient.test.ts

This file was deleted.

152 changes: 152 additions & 0 deletions src/components/__tests__/DashboardStatsClient.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import DashboardStatsClient, { buildEventSeries } from "../DashboardStatsClient";
import { useDashboardData, useDashboardStats } from "@/hooks/useDashboardData";
import "@testing-library/jest-dom";

// Mock recharts to avoid rendering errors in jsdom
vi.mock("recharts", () => ({
ResponsiveContainer: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
BarChart: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
Bar: () => <div />,
XAxis: () => <div />,
YAxis: () => <div />,
CartesianGrid: () => <div />,
Tooltip: () => <div />,
}));

vi.mock("@/components/ActivityHeatmapGrid", () => ({
default: () => <div data-testid="heatmap-grid" />,
}));

vi.mock("@/hooks/useDashboardData", () => ({
useDashboardData: vi.fn(),
useDashboardStats: vi.fn(),
}));

describe("buildEventSeries", () => {
it("should return an empty array when given an empty array", () => {
expect(buildEventSeries([])).toEqual([]);
});

it("should replace 'Event' with an empty string in the name property", () => {
const input = [
{ type: "PushEvent", count: 10 },
{ type: "PullRequestEvent", count: 5 },
{ type: "IssuesEvent", count: 2 },
];
const expected = [
{ name: "Push", count: 10 },
{ name: "PullRequest", count: 5 },
{ name: "Issues", count: 2 },
];
expect(buildEventSeries(input)).toEqual(expected);
});

it("should return at most 6 elements", () => {
const input = Array.from({ length: 10 }, (_, i) => ({
type: `Type${i}Event`,
count: i,
}));
const result = buildEventSeries(input);
expect(result.length).toBe(6);
expect(result).toEqual([
{ name: "Type0", count: 0 },
{ name: "Type1", count: 1 },
{ name: "Type2", count: 2 },
{ name: "Type3", count: 3 },
{ name: "Type4", count: 4 },
{ name: "Type5", count: 5 },
]);
});

it("should correctly map the count property", () => {
const input = [
{ type: "PushEvent", count: 42 },
{ type: "CreateEvent", count: 1 },
];
const expected = [
{ name: "Push", count: 42 },
{ name: "Create", count: 1 },
];
expect(buildEventSeries(input)).toEqual(expected);
});

it("should not modify type if it does not contain 'Event'", () => {
const input = [{ type: "Push", count: 10 }];
const expected = [{ name: "Push", count: 10 }];
expect(buildEventSeries(input)).toEqual(expected);
});
});

describe("DashboardStatsClient", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should render loading state when data is loading", () => {
vi.mocked(useDashboardData).mockReturnValue({
summary: null,
isLoading: true,
error: null,
} as unknown as ReturnType<typeof useDashboardData>);
vi.mocked(useDashboardStats).mockReturnValue({
heatmap: null,
isLoading: false,
error: null,
} as unknown as ReturnType<typeof useDashboardStats>);

render(<DashboardStatsClient />);
expect(screen.getByText("Loading stats...")).toBeInTheDocument();
});

it("should render error state when there is an error", () => {
vi.mocked(useDashboardData).mockReturnValue({
summary: null,
isLoading: false,
error: new Error("Failed to load"),
} as unknown as ReturnType<typeof useDashboardData>);
vi.mocked(useDashboardStats).mockReturnValue({
heatmap: null,
isLoading: false,
error: null,
} as unknown as ReturnType<typeof useDashboardStats>);

render(<DashboardStatsClient />);
expect(screen.getByText("Failed to load stats.")).toBeInTheDocument();
});

it("should render charts and heatmap when data is loaded successfully", () => {
const mockSummary = {
activity: {
eventBreakdown: [
{ type: "PushEvent", count: 10 },
],
},
contributions: {
calendar: [
{ date: "2023-01-01", count: 5 },
],
},
};

vi.mocked(useDashboardData).mockReturnValue({
summary: mockSummary,
isLoading: false,
error: null,
} as unknown as ReturnType<typeof useDashboardData>);
vi.mocked(useDashboardStats).mockReturnValue({
heatmap: [[1, 2, 3]],
isLoading: false,
error: null,
} as unknown as ReturnType<typeof useDashboardStats>);

render(<DashboardStatsClient />);

expect(screen.getByText("Activity Stats")).toBeInTheDocument();
expect(screen.getByText("Recent Event Breakdown")).toBeInTheDocument();
expect(screen.getByText("Monthly Contributions")).toBeInTheDocument();
expect(screen.getByTestId("heatmap-grid")).toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
coverage: {
provider: "v8",
reporter: ["text", "lcov"],
include: ["src/lib/**/*.ts", "src/hooks/**/*.ts"],
include: ["src/lib/**/*.ts", "src/hooks/**/*.ts", "src/components/DashboardStatsClient.tsx"],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of adding individual component files to the coverage include list, it is more robust to use a glob pattern. This ensures that all current and future components in the src/components directory are automatically tracked for coverage.

Suggested change
include: ["src/lib/**/*.ts", "src/hooks/**/*.ts", "src/components/DashboardStatsClient.tsx"],
include: ["src/lib/**/*.ts", "src/hooks/**/*.ts", "src/components/**/*.{ts,tsx}"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 カバレッジ閾値違反のリスク

DashboardStatsClient.tsx にはテスト対象の buildEventSeries のほかに、StatBarChartEventBreakdownChartMonthlyContributionsChartDashboardStatsClient(デフォルトエクスポート)の計4つの関数が定義されています。現行テストはこれらをまったく呼び出しておらず、ファイル単体の関数カバレッジは約 20%(1/5)になります。集計カバレッジが既存ファイル群で余裕をもって 80% を超えていれば問題ありませんが、ボーダーライン付近の場合はこの追加で functions: 80 閾値を下回り CI が失敗するリスクがあります。事前にローカルで vitest run --coverage を実行して閾値違反が起きないことを確認することを推奨します。

Prompt To Fix With AI
This is a comment left during a code review.
Path: vitest.config.ts
Line: 14

Comment:
**カバレッジ閾値違反のリスク**

`DashboardStatsClient.tsx` にはテスト対象の `buildEventSeries` のほかに、`StatBarChart``EventBreakdownChart``MonthlyContributionsChart``DashboardStatsClient`(デフォルトエクスポート)の計4つの関数が定義されています。現行テストはこれらをまったく呼び出しておらず、ファイル単体の関数カバレッジは約 20%(1/5)になります。集計カバレッジが既存ファイル群で余裕をもって 80% を超えていれば問題ありませんが、ボーダーライン付近の場合はこの追加で `functions: 80` 閾値を下回り CI が失敗するリスクがあります。事前にローカルで `vitest run --coverage` を実行して閾値違反が起きないことを確認することを推奨します。

How can I resolve this? If you propose a fix, please make it concise.

thresholds: {
lines: 80,
functions: 80,
Expand Down
Loading