Skip to content

Commit

Permalink
Update test files
Browse files Browse the repository at this point in the history
  • Loading branch information
slavingia committed Sep 25, 2024
1 parent 0833ab1 commit 53a9ccc
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/(dashboard)/dashboard/page.test.tsx
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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders loading state initially

TestingLibraryElementError: Unable to find an element with the text: Loading.... This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="flex justify-center items-center h-full" > <svg class="lucide lucide-loader-circle h-8 w-8 animate-spin" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="M21 12a9 9 0 1 1-6.219-8.56" /> </svg> </div> </div> </body> ❯ Object.getElementError node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ app/(dashboard)/dashboard/page.test.tsx:30:19
});

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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders pull requests when loaded successfully

TestingLibraryElementError: Unable to find an element with the text: Your Pull Requests. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="space-y-6" > <div class="p-6" > <ul class="space-y-8" > <li> <div class="flex items-center justify-between mb-2" > <h3 class="font-semibold text-lg" > undefined/undefined </h3> </div> <div data-testid="pr-1" > PR 1 </div> </li> <li> <div class="flex items-center justify-between mb-2" > <h3 class="font-semibold text-lg" > undefined/undefined </h3> </div> <div data-testid="pr-2" > PR 2 </div> </li> </ul> </div> </div> </div> </body> Ignored nodes: comments, script, style <html> <head /> <body> <div> <div class="space-y-6" > <div class="p-6" > <ul class="space-y-8" > <li> <div class="flex items-center justify-between mb-2" > <h3 class="font-semibold text-lg" > undefined/undefined </h3> </div> <div data-testid="pr-1" > PR 1 </div> </li> <li> <div class="flex items-center justify-between mb-2" > <h3 class="font-semibold text-lg" > undefined/undefined </h3> </div> <div data-testid="pr-2" > PR 2 </div> </li> </ul> </div> </div> </div> </body> </html> ❯ Proxy.waitForWrapper node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/wait-for.js:163:27 ❯ app/(dashboard)/dashboard/page.test.tsx:42:11
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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders error message when loading fails

TestingLibraryElementError: Unable to find an element with the text: Error loading pull requests. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="flex flex-col items-center justify-center h-full" > <svg class="lucide lucide-circle-alert h-12 w-12 text-red-500 mb-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" /> <line x1="12" x2="12" y1="8" y2="12" /> <line x1="12" x2="12.01" y1="16" y2="16" /> </svg> <p class="text-lg mb-4" > Failed to fetch pull requests. Please reconnect your GitHub account. </p> <button> Reconnect GitHub account </button> </div> </div> </body> Ignored nodes: comments, script, style <html> <head /> <body> <div> <div class="flex flex-col items-center justify-center h-full" > <svg class="lucide lucide-circle-alert h-12 w-12 text-red-500 mb-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" /> <line x1="12" x2="12" y1="8" y2="12" /> <line x1="12" x2="12.01" y1="16" y2="16" /> </svg> <p class="text-lg mb-4" > Failed to fetch pull requests. Please reconnect your GitHub account. </p> <button> Reconnect GitHub account </button> </div> </div> </body> </html> ❯ Proxy.waitForWrapper node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/wait-for.js:163:27 ❯ app/(dashboard)/dashboard/page.test.tsx:55:11
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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/page.test.tsx > DashboardPage > renders reconnect button when GitHub token is invalid

TestingLibraryElementError: Unable to find role="button" and name `/reconnect to github/i` Ignored nodes: comments, script, style <body> <div> <div class="flex flex-col items-center justify-center h-full" > <svg class="lucide lucide-circle-alert h-12 w-12 text-red-500 mb-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" /> <line x1="12" x2="12" y1="8" y2="12" /> <line x1="12" x2="12.01" y1="16" y2="16" /> </svg> <p class="text-lg mb-4" > GitHub token invalid </p> <button> Reconnect GitHub account </button> </div> </div> </body> Ignored nodes: comments, script, style <html> <head /> <body> <div> <div class="flex flex-col items-center justify-center h-full" > <svg class="lucide lucide-circle-alert h-12 w-12 text-red-500 mb-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" /> <line x1="12" x2="12" y1="8" y2="12" /> <line x1="12" x2="12.01" y1="16" y2="16" /> </svg> <p class="text-lg mb-4" > GitHub token invalid </p> <button> Reconnect GitHub account </button> </div> </div> </body> </html> ❯ Proxy.waitForWrapper node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/wait-for.js:163:27 ❯ app/(dashboard)/dashboard/page.test.tsx:65:11
expect(screen.getByRole("button", { name: /reconnect to github/i })).toBeInTheDocument();
});
});
});
123 changes: 123 additions & 0 deletions app/(dashboard)/dashboard/pull-request.test.tsx
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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/dashboard/pull-request.test.tsx > PullRequestItem > handles updating tests

TestingLibraryElementError: Unable to find an element with the text: Update tests to fix. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible. Ignored nodes: comments, script, style <body> <div> <div class="bg-white p-4 rounded-lg shadow-md" > <div class="flex items-center justify-between mb-2" > <span class="flex items-center" > <svg class="lucide lucide-git-pull-request mr-2 h-4 w-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="18" cy="18" r="3" /> <circle cx="6" cy="6" r="3" /> <path d="M13 6h3a2 2 0 0 1 2 2v7" /> <line x1="6" x2="6" y1="9" y2="21" /> </svg> <span class="font-medium" > Test PR </span> </span> <a class="text-sm text-gray-600 underline" href="https://github.com/undefined/pull/123" > # 123 </a> </div> <div class="flex items-center justify-between" > <span class="flex items-center" > <svg class="lucide lucide-circle-check-big mr-2 h-4 w-4 text-green-500" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="M21.801 10A10 10 0 1 1 17 3.335" /> <path d="m9 11 3 3L22 4" /> </svg> <a class="text-sm underline text-gray-600" href="https://github.com/undefined/pull/123/checks" > Build: success </a> </span> <button class="inline-flex items-center justify-center whitespace-nowrap font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 shadow h-8 rounded-md px-3 text-xs bg-green-500 hover:bg-green-600 text-white" > <svg class="lucide lucide-circle-plus mr-2 h-4 w-4" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <circle cx="12" cy="12" r="10" /> <path d="M8 12h8" /> <path d="M12 8v8" /> </svg> Write new tests </button> </div> </div> </div> </body> ❯ Object.getElementError node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ app/(dashboard)/dashboard/pull-request.test.tsx:76:28

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();
});
});
});
20 changes: 20 additions & 0 deletions app/(dashboard)/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/page.test.tsx > Homepage > renders the sign-in button for signed-out users

TestingLibraryElementError: Unable to find an accessible element with the role "button" and name `/sign in/i` Here are the accessible roles: heading: Name "Write Your Tests Faster Than Ever": <h1 class="text-4xl font-bold text-gray-900 tracking-tight sm:text-5xl md:text-6xl" /> Name "Writes New Tests": <h2 class="text-lg font-medium text-gray-900" /> Name "Fixes Broken Tests": <h2 class="text-lg font-medium text-gray-900" /> Name "Seamless Integration": <h2 class="text-lg font-medium text-gray-900" /> Name "Ready to streamline your pull requests?": <h2 class="text-3xl font-bold text-gray-900 sm:text-4xl" /> -------------------------------------------------- paragraph: Name "": <p class="mt-3 text-base text-gray-500 sm:mt-5 sm:text-xl lg:text-lg xl:text-xl max-w-2xl mx-auto" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-3 max-w-3xl text-lg text-gray-500" /> -------------------------------------------------- link: Name "View on GitHub": <a href="https://github.com/gumroad/shortest" target="_blank" /> Name "View Dashboard": <a href="/dashboard" /> Name "Get started": <a href="https://github.com/gumroad/shortest" rel="noopener noreferrer" target="_blank" /> Name "View Dashboard": <a href="/dashboard" /> -------------------------------------------------- button: Name "View on GitHub": <button /> Name "View Dashboard": <button /> Name "Get started": <button /> Name "View Dashboard": <button /> -------------------------------------------------- Ignored nodes: comments, script, style <body> <div> <div class="main" > <section class="py-20" > <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8" > <div class="text-center" > <h1 class="text-4xl font-bold text-gray-900 tracking-tight sm:text-5xl md:text-6xl" > Write Your Tests <span class="block text-orange-500" > Faster Than Ever </span> </h1> <p class="mt-3 text-base text-gray-500 sm:mt-5 sm:text-xl lg:text-lg xl:text-xl max-w-2xl mx-auto" > Shortest observes your pull requests, helps you write tests for new features, and fixes broken tests that block you from shipping. </p> <div class="mt-8 flex justify-center" > <a href="https://github.com/gumroad/shortest" target="_blank" > <button> View on GitHub <svg class="lucide lucide-arrow-right ml-2 h-5 w-5" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="M5 12h14" /> <path d="m12 5 7 7-7 7" /> </svg> </button> </a> <a href="/dashboard" > <button> View Dashboard <svg class="lucide lucide-arrow-right ml-2 h-5 w-5" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-wid
});

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

View workflow job for this annotation

GitHub Actions / test

app/(dashboard)/page.test.tsx > Homepage > renders the dashboard link for signed-in users

TestingLibraryElementError: Unable to find an accessible element with the role "link" and name `/go to dashboard/i` Here are the accessible roles: heading: Name "Write Your Tests Faster Than Ever": <h1 class="text-4xl font-bold text-gray-900 tracking-tight sm:text-5xl md:text-6xl" /> Name "Writes New Tests": <h2 class="text-lg font-medium text-gray-900" /> Name "Fixes Broken Tests": <h2 class="text-lg font-medium text-gray-900" /> Name "Seamless Integration": <h2 class="text-lg font-medium text-gray-900" /> Name "Ready to streamline your pull requests?": <h2 class="text-3xl font-bold text-gray-900 sm:text-4xl" /> -------------------------------------------------- paragraph: Name "": <p class="mt-3 text-base text-gray-500 sm:mt-5 sm:text-xl lg:text-lg xl:text-xl max-w-2xl mx-auto" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-2 text-base text-gray-500" /> Name "": <p class="mt-3 max-w-3xl text-lg text-gray-500" /> -------------------------------------------------- link: Name "View on GitHub": <a href="https://github.com/gumroad/shortest" target="_blank" /> Name "View Dashboard": <a href="/dashboard" /> Name "Get started": <a href="https://github.com/gumroad/shortest" rel="noopener noreferrer" target="_blank" /> Name "View Dashboard": <a href="/dashboard" /> -------------------------------------------------- button: Name "View on GitHub": <button /> Name "View Dashboard": <button /> Name "Get started": <button /> Name "View Dashboard": <button /> -------------------------------------------------- Ignored nodes: comments, script, style <body> <div> <div class="main" > <section class="py-20" > <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8" > <div class="text-center" > <h1 class="text-4xl font-bold text-gray-900 tracking-tight sm:text-5xl md:text-6xl" > Write Your Tests <span class="block text-orange-500" > Faster Than Ever </span> </h1> <p class="mt-3 text-base text-gray-500 sm:mt-5 sm:text-xl lg:text-lg xl:text-xl max-w-2xl mx-auto" > Shortest observes your pull requests, helps you write tests for new features, and fixes broken tests that block you from shipping. </p> <div class="mt-8 flex justify-center" > <a href="https://github.com/gumroad/shortest" target="_blank" > <button> View on GitHub <svg class="lucide lucide-arrow-right ml-2 h-5 w-5" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" > <path d="M5 12h14" /> <path d="m12 5 7 7-7 7" /> </svg> </button> </a> <a href="/dashboard" > <button> View Dashboard <svg class="lucide lucide-arrow-right ml-2 h-5 w-5" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stro
});
});

0 comments on commit 53a9ccc

Please sign in to comment.