-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from "react"; | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import DashboardPage from "./page"; | ||
import { getAssignedPullRequests } from "@/lib/github"; | ||
|
||
vi.mock("@/lib/github", () => ({ | ||
getAssignedPullRequests: vi.fn(), | ||
})); | ||
|
||
vi.mock("@/components/ui/button", () => ({ | ||
Button: ({ children }: { children: React.ReactNode }) => ( | ||
<button>{children}</button> | ||
), | ||
})); | ||
|
||
vi.mock("./pull-request", () => ({ | ||
PullRequestItem: ({ pullRequest }: { pullRequest: any }) => ( | ||
<div data-testid={`pr-${pullRequest.id}`}>{pullRequest.title}</div> | ||
), | ||
})); | ||
|
||
describe("DashboardPage", () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("renders loading state initially", () => { | ||
render(<DashboardPage />); | ||
expect(screen.getByText("Loading...")).toBeInTheDocument(); | ||
Check failure on line 30 in app/(dashboard)/dashboard/page.test.tsx GitHub Actions / testapp/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders loading state initially
|
||
}); | ||
|
||
it("renders pull requests when loaded successfully", async () => { | ||
const mockPullRequests = [ | ||
{ id: 1, title: "PR 1", repository: { full_name: "repo1" } }, | ||
{ id: 2, title: "PR 2", repository: { full_name: "repo2" } }, | ||
]; | ||
(getAssignedPullRequests as jest.Mock).mockResolvedValue(mockPullRequests); | ||
|
||
render(<DashboardPage />); | ||
|
||
await waitFor(() => { | ||
Check failure on line 42 in app/(dashboard)/dashboard/page.test.tsx GitHub Actions / testapp/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders pull requests when loaded successfully
|
||
expect(screen.getByText("Your Pull Requests")).toBeInTheDocument(); | ||
}); | ||
|
||
expect(screen.getByTestId("pr-1")).toBeInTheDocument(); | ||
expect(screen.getByTestId("pr-2")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders error message when loading fails", async () => { | ||
(getAssignedPullRequests as jest.Mock).mockRejectedValue(new Error("Failed to fetch")); | ||
|
||
render(<DashboardPage />); | ||
|
||
await waitFor(() => { | ||
Check failure on line 55 in app/(dashboard)/dashboard/page.test.tsx GitHub Actions / testapp/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders error message when loading fails
|
||
expect(screen.getByText("Error loading pull requests")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("renders reconnect button when GitHub token is invalid", async () => { | ||
(getAssignedPullRequests as jest.Mock).mockResolvedValue({ error: "GitHub token invalid" }); | ||
|
||
render(<DashboardPage />); | ||
|
||
await waitFor(() => { | ||
Check failure on line 65 in app/(dashboard)/dashboard/page.test.tsx GitHub Actions / testapp/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders reconnect button when GitHub token is invalid
|
||
expect(screen.getByRole("button", { name: /reconnect to github/i })).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import React from "react"; | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import { PullRequestItem } from "./pull-request"; | ||
import { generateTestsResponseSchema } from "@/app/api/generate-tests/schema"; | ||
import { commitChangesToPullRequest, getPullRequestInfo } from "@/lib/github"; | ||
|
||
vi.mock("@/lib/github", () => ({ | ||
commitChangesToPullRequest: vi.fn(), | ||
getPullRequestInfo: vi.fn(), | ||
})); | ||
|
||
vi.mock("@/hooks/use-toast", () => ({ | ||
useToast: () => ({ | ||
toast: vi.fn(), | ||
}), | ||
})); | ||
|
||
const mockPullRequest = { | ||
id: 1, | ||
title: "Test PR", | ||
number: 123, | ||
buildStatus: "success", | ||
repository: { | ||
owner: { login: "testuser" }, | ||
name: "testrepo", | ||
}, | ||
}; | ||
|
||
describe("PullRequestItem", () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("renders pull request information", () => { | ||
render(<PullRequestItem pullRequest={mockPullRequest} />); | ||
expect(screen.getByText("Test PR")).toBeInTheDocument(); | ||
expect(screen.getByText("#123")).toBeInTheDocument(); | ||
expect(screen.getByText("Build: success")).toBeInTheDocument(); | ||
}); | ||
|
||
it("handles writing new tests", async () => { | ||
(getPullRequestInfo as jest.Mock).mockResolvedValue({ | ||
diff: "mock diff", | ||
testFiles: [], | ||
}); | ||
|
||
global.fetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve(generateTestsResponseSchema.parse([{ name: "test.js", content: "test content" }])), | ||
}); | ||
|
||
render(<PullRequestItem pullRequest={mockPullRequest} />); | ||
|
||
fireEvent.click(screen.getByText("Write new tests")); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Test files")).toBeInTheDocument(); | ||
expect(screen.getByText("test.js")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("handles updating tests", async () => { | ||
(getPullRequestInfo as jest.Mock).mockResolvedValue({ | ||
diff: "mock diff", | ||
testFiles: [{ name: "existing.test.js", content: "existing content" }], | ||
}); | ||
|
||
global.fetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve(generateTestsResponseSchema.parse([{ name: "existing.test.js", content: "updated content" }])), | ||
}); | ||
|
||
render(<PullRequestItem pullRequest={mockPullRequest} />); | ||
|
||
fireEvent.click(screen.getByText("Update tests to fix")); | ||
Check failure on line 76 in app/(dashboard)/dashboard/pull-request.test.tsx GitHub Actions / testapp/(dashboard)/dashboard/pull-request.test.tsx > PullRequestItem > handles updating tests
|
||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Test files")).toBeInTheDocument(); | ||
expect(screen.getByText("existing.test.js")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("handles committing changes", async () => { | ||
(getPullRequestInfo as jest.Mock).mockResolvedValue({ | ||
diff: "mock diff", | ||
testFiles: [], | ||
}); | ||
|
||
global.fetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve(generateTestsResponseSchema.parse([{ name: "test.js", content: "test content" }])), | ||
}); | ||
|
||
(commitChangesToPullRequest as jest.Mock).mockResolvedValue("https://github.com/testuser/testrepo/commit/abc123"); | ||
|
||
render(<PullRequestItem pullRequest={mockPullRequest} />); | ||
|
||
fireEvent.click(screen.getByText("Write new tests")); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Commit changes")).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByText("Commit changes")); | ||
|
||
await waitFor(() => { | ||
expect(commitChangesToPullRequest).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it("handles errors when generating tests", async () => { | ||
(getPullRequestInfo as jest.Mock).mockRejectedValue(new Error("Failed to fetch PR info")); | ||
|
||
render(<PullRequestItem pullRequest={mockPullRequest} />); | ||
|
||
fireEvent.click(screen.getByText("Write new tests")); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Failed to generate test files.")).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,24 @@ describe("Homepage", () => { | |
"Write Your Tests" | ||
); | ||
}); | ||
|
||
it("renders the sign-in button for signed-out users", () => { | ||
render( | ||
<ClerkProvider> | ||
<Homepage /> | ||
</ClerkProvider> | ||
); | ||
|
||
expect(screen.getByRole("button", { name: /sign in/i })).toBeInTheDocument(); | ||
Check failure on line 45 in app/(dashboard)/page.test.tsx GitHub Actions / testapp/(dashboard)/page.test.tsx > Homepage > renders the sign-in button for signed-out users
|
||
}); | ||
|
||
it("renders the dashboard link for signed-in users", () => { | ||
render( | ||
<ClerkProvider> | ||
<Homepage /> | ||
</ClerkProvider> | ||
); | ||
|
||
expect(screen.getByRole("link", { name: /go to dashboard/i })).toBeInTheDocument(); | ||
Check failure on line 55 in app/(dashboard)/page.test.tsx GitHub Actions / testapp/(dashboard)/page.test.tsx > Homepage > renders the dashboard link for signed-in users
|
||
}); | ||
}); |