|
| 1 | +/** |
| 2 | + * Ports of Storybook interaction tests from `src/browser/stories/App.errors.stories.tsx`. |
| 3 | + */ |
| 4 | + |
| 5 | +import "@testing-library/jest-dom"; |
| 6 | +import { waitFor } from "@testing-library/react"; |
| 7 | +import userEvent from "@testing-library/user-event"; |
| 8 | +import { shouldRunIntegrationTests } from "../../testUtils"; |
| 9 | +import { |
| 10 | + renderWithBackend, |
| 11 | + waitForAppLoad, |
| 12 | + addProjectViaUI, |
| 13 | + createTempGitRepo, |
| 14 | + cleanupTempGitRepo, |
| 15 | + getProjectName, |
| 16 | +} from "../harness"; |
| 17 | + |
| 18 | +const describeIntegration = shouldRunIntegrationTests() ? describe : describe.skip; |
| 19 | + |
| 20 | +describeIntegration("Storybook interactions: Errors", () => { |
| 21 | + test("ProjectRemovalError: removing a project with active workspaces shows an error alert", async () => { |
| 22 | + const user = userEvent.setup(); |
| 23 | + const gitRepo = await createTempGitRepo(); |
| 24 | + |
| 25 | + const { cleanup, env, ...queries } = await renderWithBackend(); |
| 26 | + try { |
| 27 | + await waitForAppLoad(queries); |
| 28 | + |
| 29 | + await addProjectViaUI(user, queries, gitRepo); |
| 30 | + |
| 31 | + // Create 2 workspaces, so `projects.remove()` returns the same error shown in Storybook. |
| 32 | + const ws1 = await env.orpc.workspace.create({ |
| 33 | + projectPath: gitRepo, |
| 34 | + branchName: "feature-auth", |
| 35 | + trunkBranch: "main", |
| 36 | + }); |
| 37 | + const ws2 = await env.orpc.workspace.create({ |
| 38 | + projectPath: gitRepo, |
| 39 | + branchName: "feature-ui", |
| 40 | + trunkBranch: "main", |
| 41 | + }); |
| 42 | + expect(ws1.success && ws2.success).toBe(true); |
| 43 | + if (!ws1.success || !ws2.success) throw new Error("Workspace creation failed"); |
| 44 | + |
| 45 | + const projectName = getProjectName(gitRepo); |
| 46 | + |
| 47 | + // The remove button is typically shown on hover; select via aria-label (same as Storybook). |
| 48 | + await waitFor(() => { |
| 49 | + const btn = document.querySelector(`button[aria-label="Remove project ${projectName}"]`); |
| 50 | + expect(btn).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + const removeButton = document.querySelector( |
| 54 | + `button[aria-label="Remove project ${projectName}"]` |
| 55 | + ) as HTMLElement; |
| 56 | + const projectRow = removeButton.closest("[data-project-path]") as HTMLElement | null; |
| 57 | + expect(projectRow).toBeTruthy(); |
| 58 | + |
| 59 | + await user.hover(projectRow!); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + expect(removeButton).toBeVisible(); |
| 63 | + }); |
| 64 | + |
| 65 | + // This flow intentionally triggers an error path. Silence the expected |
| 66 | + // console.error line while preserving unexpected errors. |
| 67 | + const consoleErrorMock = console.error as unknown as jest.Mock; |
| 68 | + const previousImpl = consoleErrorMock.getMockImplementation(); |
| 69 | + consoleErrorMock.mockImplementation((...args: unknown[]) => { |
| 70 | + const text = args.map(String).join(" "); |
| 71 | + if ( |
| 72 | + text.includes( |
| 73 | + "Failed to remove project: Cannot remove project with active workspaces. Please remove all 2 workspace(s) first." |
| 74 | + ) |
| 75 | + ) { |
| 76 | + return; |
| 77 | + } |
| 78 | + return previousImpl?.(...args); |
| 79 | + }); |
| 80 | + try { |
| 81 | + await user.click(removeButton); |
| 82 | + |
| 83 | + const alert = await queries.findByRole("alert"); |
| 84 | + expect(alert).toHaveTextContent( |
| 85 | + /cannot remove project with active workspaces\. please remove all 2 workspace\(s\) first\./i |
| 86 | + ); |
| 87 | + } finally { |
| 88 | + consoleErrorMock.mockImplementation(previousImpl ?? (() => {})); |
| 89 | + } |
| 90 | + } finally { |
| 91 | + await cleanupTempGitRepo(gitRepo); |
| 92 | + await cleanup(); |
| 93 | + } |
| 94 | + }); |
| 95 | +}); |
0 commit comments