From 3befb0fd3fefa9835faf1a02a86463ae92cece18 Mon Sep 17 00:00:00 2001 From: Sahil Lavingia Date: Wed, 25 Sep 2024 11:43:01 -0400 Subject: [PATCH] cp --- app/(dashboard)/dashboard/page.test.tsx | 69 ---------- .../dashboard/pull-request.test.tsx | 123 ------------------ 2 files changed, 192 deletions(-) delete mode 100644 app/(dashboard)/dashboard/page.test.tsx delete mode 100644 app/(dashboard)/dashboard/pull-request.test.tsx diff --git a/app/(dashboard)/dashboard/page.test.tsx b/app/(dashboard)/dashboard/page.test.tsx deleted file mode 100644 index 49b34f0b..00000000 --- a/app/(dashboard)/dashboard/page.test.tsx +++ /dev/null @@ -1,69 +0,0 @@ -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 }) => ( - - ), -})); - -vi.mock("./pull-request", () => ({ - PullRequestItem: ({ pullRequest }: { pullRequest: any }) => ( -
{pullRequest.title}
- ), -})); - -describe("DashboardPage", () => { - beforeEach(() => { - vi.resetAllMocks(); - }); - - it("renders loading state initially", () => { - render(); - expect(screen.getByText("Loading...")).toBeInTheDocument(); - }); - - 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(); - - await waitFor(() => { - 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(); - - await waitFor(() => { - 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(); - - await waitFor(() => { - expect(screen.getByRole("button", { name: /reconnect to github/i })).toBeInTheDocument(); - }); - }); -}); diff --git a/app/(dashboard)/dashboard/pull-request.test.tsx b/app/(dashboard)/dashboard/pull-request.test.tsx deleted file mode 100644 index fbc8b642..00000000 --- a/app/(dashboard)/dashboard/pull-request.test.tsx +++ /dev/null @@ -1,123 +0,0 @@ -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(); - 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(); - - 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(); - - fireEvent.click(screen.getByText("Update tests to fix")); - - 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(); - - 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(); - - fireEvent.click(screen.getByText("Write new tests")); - - await waitFor(() => { - expect(screen.getByText("Failed to generate test files.")).toBeInTheDocument(); - }); - }); -});