|
| 1 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { expect, it, vi } from "vite-plus/test"; |
| 3 | +import { page, userEvent } from "vite-plus/test/browser"; |
| 4 | + |
| 5 | +import { render } from "../../../test/render"; |
| 6 | +import { ProposedPlanCard } from "./ProposedPlanCard"; |
| 7 | + |
| 8 | +const writeFile = vi.hoisted(() => |
| 9 | + vi.fn<(request: unknown) => Promise<{ _tag: "Success"; value: { relativePath: string } }>>(), |
| 10 | +); |
| 11 | + |
| 12 | +vi.mock(import("~/state/session"), async (original) => ({ |
| 13 | + ...(await original()), |
| 14 | + readEnvironmentScope: (() => true) as never, |
| 15 | + useEnvironmentScope: (() => true) as never, |
| 16 | +})); |
| 17 | +vi.mock(import("~/state/use-atom-command"), () => ({ |
| 18 | + useAtomCommand: (() => writeFile) as never, |
| 19 | +})); |
| 20 | + |
| 21 | +it("saves the plan once when Enter is pressed in the workspace path field", async () => { |
| 22 | + const pending = Promise.withResolvers<{ |
| 23 | + _tag: "Success"; |
| 24 | + value: { relativePath: string }; |
| 25 | + }>(); |
| 26 | + writeFile.mockReturnValue(pending.promise); |
| 27 | + const environmentId = EnvironmentId.make("environment-local"); |
| 28 | + |
| 29 | + await using screen = await render( |
| 30 | + <ProposedPlanCard |
| 31 | + planMarkdown={"# Migration Plan\n\n- Step 1\n"} |
| 32 | + environmentId={environmentId} |
| 33 | + cwd="/workspace" |
| 34 | + workspaceRoot="/workspace" |
| 35 | + />, |
| 36 | + ); |
| 37 | + |
| 38 | + await screen.getByRole("button", { name: "Plan actions" }).click(); |
| 39 | + await page.getByRole("menuitem", { name: "Save to workspace" }).click(); |
| 40 | + await page.getByRole("textbox", { name: "Workspace path" }).fill("plans/migration.md"); |
| 41 | + await userEvent.keyboard("{Enter}"); |
| 42 | + await userEvent.keyboard("{Enter}"); |
| 43 | + |
| 44 | + expect(writeFile).toHaveBeenCalledExactlyOnceWith({ |
| 45 | + environmentId, |
| 46 | + input: { |
| 47 | + cwd: "/workspace", |
| 48 | + relativePath: "plans/migration.md", |
| 49 | + contents: "# Migration Plan\n\n- Step 1\n", |
| 50 | + }, |
| 51 | + }); |
| 52 | + pending.resolve({ _tag: "Success", value: { relativePath: "plans/migration.md" } }); |
| 53 | + await expect |
| 54 | + .element(page.getByRole("dialog", { name: "Save plan to workspace" })) |
| 55 | + .not.toBeInTheDocument(); |
| 56 | +}); |
0 commit comments