| 1 | // Run: tsx src/__tests__/goal-lifecycle-actions.test.tsx |
| 2 | |
| 3 | import { JSDOM } from "jsdom"; |
| 4 | import React from "react"; |
| 5 | import { act } from "react"; |
| 6 | import { createRoot } from "react-dom/client"; |
| 7 | import { GoalLifecycleActions } from "../components/GoalLifecycleActions"; |
| 8 | import { LocaleProvider } from "../lib/i18n"; |
| 9 | |
| 10 | const dom = new JSDOM("<!doctype html><html><body><div id=\"root\"></div></body></html>", { url: "http://localhost/" }); |
| 11 | (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 12 | globalThis.window = dom.window as unknown as Window & typeof globalThis; |
| 13 | globalThis.document = dom.window.document; |
| 14 | globalThis.HTMLElement = dom.window.HTMLElement; |
| 15 | globalThis.Event = dom.window.Event; |
| 16 | |
| 17 | let edit: { objective: string; maxGoalRounds: number | null } | undefined; |
| 18 | let paused = 0; |
| 19 | const prompts = ["finish the migration safely", "12"]; |
| 20 | window.prompt = () => prompts.shift() ?? null; |
| 21 | |
| 22 | await act(async () => { |
| 23 | createRoot(document.getElementById("root")!).render( |
| 24 | <LocaleProvider> |
| 25 | <GoalLifecycleActions |
| 26 | goalView={{ |
| 27 | id: "goal-1", revision: 3, objective: "finish the migration", phase: "active", |
| 28 | maxGoalRounds: null, roundsStarted: 2, createdAt: "2026-01-01T00:00:00Z", |
| 29 | updatedAt: "2026-01-01T00:00:00Z", activation: "armed", |
| 30 | }} |
| 31 | goalStatus="running" |
| 32 | running |
| 33 | onEditGoal={(objective, maxGoalRounds) => { edit = { objective, maxGoalRounds }; }} |
| 34 | onPauseGoal={() => { paused += 1; }} |
| 35 | onResumeGoal={() => {}} |
| 36 | onStopGoal={() => {}} |
| 37 | /> |
| 38 | </LocaleProvider>, |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | const buttons = Array.from(document.querySelectorAll<HTMLButtonElement>("button")); |
| 43 | const editButton = buttons.find((button) => button.textContent === "Edit goal"); |
| 44 | const pauseButton = buttons.find((button) => button.textContent === "Pause goal"); |
| 45 | if (!editButton || !pauseButton) throw new Error("active goal lifecycle actions did not render"); |
| 46 | if (pauseButton.disabled) throw new Error("running automatic Goal round is not pausable"); |
| 47 | |
| 48 | await act(async () => { editButton.click(); }); |
| 49 | if (edit?.objective !== "finish the migration safely" || edit.maxGoalRounds !== 12) { |
| 50 | throw new Error(`edit action returned ${JSON.stringify(edit)}`); |
| 51 | } |
| 52 | await act(async () => { pauseButton.click(); }); |
| 53 | if (paused !== 1) throw new Error("pause action was not delivered"); |
| 54 | console.log("goal lifecycle actions: PASS"); |
| 55 |