|
| 1 | +import { render, screen, waitFor, within } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import type { Project } from "@/lib/projects"; |
| 6 | + |
| 7 | +const setActive = vi.fn(); |
| 8 | +const create = vi.fn(); |
| 9 | +const update = vi.fn(); |
| 10 | +const remove = vi.fn(); |
| 11 | +const refresh = vi.fn(); |
| 12 | + |
| 13 | +const PROJECTS: Project[] = [ |
| 14 | + { |
| 15 | + id: "rna-pilot", |
| 16 | + name: "RNA pilot", |
| 17 | + description: "Compare treatment and control cohorts.", |
| 18 | + tags: ["genomics"], |
| 19 | + createdAt: "2026-07-01T12:00:00.000Z", |
| 20 | + updatedAt: "2026-07-08T12:00:00.000Z", |
| 21 | + archived: false, |
| 22 | + spendLimitUsd: 25, |
| 23 | + }, |
| 24 | + { |
| 25 | + id: "old-study", |
| 26 | + name: "Old study", |
| 27 | + description: "", |
| 28 | + tags: [], |
| 29 | + createdAt: "2026-06-01T12:00:00.000Z", |
| 30 | + updatedAt: "2026-06-02T12:00:00.000Z", |
| 31 | + archived: true, |
| 32 | + spendLimitUsd: null, |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +vi.mock("@/lib/use-projects", () => ({ |
| 37 | + useProjects: () => ({ |
| 38 | + projects: PROJECTS, |
| 39 | + activeProjectId: "rna-pilot", |
| 40 | + activeProject: PROJECTS[0], |
| 41 | + loading: false, |
| 42 | + error: null, |
| 43 | + setActive, |
| 44 | + refresh, |
| 45 | + create, |
| 46 | + update, |
| 47 | + remove, |
| 48 | + }), |
| 49 | +})); |
| 50 | + |
| 51 | +vi.mock("next-themes", () => ({ |
| 52 | + useTheme: () => ({ |
| 53 | + resolvedTheme: "light", |
| 54 | + setTheme: vi.fn(), |
| 55 | + }), |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("@/components/settings-dialog", () => ({ |
| 59 | + SettingsDialog: () => null, |
| 60 | +})); |
| 61 | + |
| 62 | +import { ProjectView } from "@/components/project-view"; |
| 63 | + |
| 64 | +describe("ProjectView", () => { |
| 65 | + beforeEach(() => { |
| 66 | + create.mockResolvedValue({ |
| 67 | + ...PROJECTS[0], |
| 68 | + id: "new-project", |
| 69 | + name: "New project", |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("shows active and archived projects and opens a selected project", async () => { |
| 74 | + const user = userEvent.setup(); |
| 75 | + const onOpenProject = vi.fn(); |
| 76 | + render(<ProjectView onOpenProject={onOpenProject} />); |
| 77 | + |
| 78 | + expect(screen.getByRole("heading", { name: "Choose a project" })).toBeInTheDocument(); |
| 79 | + expect(screen.getByText("RNA pilot")).toBeInTheDocument(); |
| 80 | + expect(screen.getByText("Old study")).toBeInTheDocument(); |
| 81 | + expect(screen.getByText("Current")).toBeInTheDocument(); |
| 82 | + |
| 83 | + const card = screen.getByText("RNA pilot").closest('[data-slot="card"]'); |
| 84 | + expect(card).not.toBeNull(); |
| 85 | + await user.click(within(card as HTMLElement).getByRole("button", { name: "Open" })); |
| 86 | + |
| 87 | + expect(setActive).toHaveBeenCalledWith("rna-pilot"); |
| 88 | + expect(onOpenProject).toHaveBeenCalledWith("rna-pilot"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("creates a project and opens it immediately", async () => { |
| 92 | + const user = userEvent.setup(); |
| 93 | + const onOpenProject = vi.fn(); |
| 94 | + render(<ProjectView onOpenProject={onOpenProject} />); |
| 95 | + |
| 96 | + await user.click(screen.getByRole("button", { name: "New project" })); |
| 97 | + await user.type(screen.getByLabelText("Name"), "Protein screen"); |
| 98 | + await user.type(screen.getByLabelText(/Tags/), "proteomics, pilot"); |
| 99 | + await user.click(screen.getByRole("button", { name: "Create project" })); |
| 100 | + |
| 101 | + await waitFor(() => |
| 102 | + expect(create).toHaveBeenCalledWith({ |
| 103 | + name: "Protein screen", |
| 104 | + description: "", |
| 105 | + tags: ["proteomics", "pilot"], |
| 106 | + spendLimitUsd: null, |
| 107 | + }), |
| 108 | + ); |
| 109 | + expect(setActive).toHaveBeenCalledWith("new-project"); |
| 110 | + expect(onOpenProject).toHaveBeenCalledWith("new-project"); |
| 111 | + }); |
| 112 | +}); |
0 commit comments