import type { PlayerState } from "@deadhead/schema"; import { screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it } from "vitest"; import { fakeApi, renderApp, samplePlayerState, sampleTutorialBoard } from "./render"; const NAME = "Deadhead Freight"; /** A company whose welcome is `seen`; "Start a new company" makes another with its welcome unseen. */ function company(seen: boolean) { let state: PlayerState = samplePlayerState({ welcomeSeen: seen }); let companies = 1; const api = fakeApi({ getPlayerState: () => state, getTutorialBoard: () => sampleTutorialBoard(), markWelcomeSeen: () => { state = { ...state, welcomeSeen: true }; return state; }, newCompany: () => { companies++; state = samplePlayerState({ welcomeSeen: false, company: { id: `co${companies}`, name: NAME, credits: 0, loanBalance: 0 }, }); return state; }, }); return api; } async function startNewCompany() { await userEvent.click(await screen.findByRole("button", { name: "Start a new company" })); const confirm = screen.getByRole("dialog", { name: "Start a new company?" }); await userEvent.type(within(confirm).getByRole("textbox"), NAME); await userEvent.click(within(confirm).getByRole("button", { name: "Start over" })); } describe("welcome for a new company and How to play (Step 22.3, decisions 0.5.131, 0.5.132)", () => { it("shows the welcome for a company started from Settings, even after closing one earlier", async () => { const api = company(false); renderApp(api, "/settings"); // This company's welcome first. const first = await screen.findByRole("dialog", { name: "Welcome to Deadhead" }); await userEvent.click(within(first).getByRole("button", { name: "Skip" })); await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); await userEvent.click(await screen.findByRole("link", { name: "Settings" })); await startNewCompany(); const again = await screen.findByRole("dialog", { name: "Welcome to Deadhead" }); expect(again).toHaveTextContent("1 of 3"); }); it("opens How to play from Settings any time, ending in Close, without touching the save", async () => { const api = company(true); renderApp(api, "/settings"); await userEvent.click(await screen.findByRole("button", { name: "How to play" })); const dialog = screen.getByRole("dialog", { name: "Welcome to Deadhead" }); await userEvent.click(within(dialog).getByRole("button", { name: "Next" })); await userEvent.click(within(dialog).getByRole("button", { name: "Next" })); expect(within(dialog).queryByRole("button", { name: "Let's fly" })).not.toBeInTheDocument(); await userEvent.click(within(dialog).getByRole("button", { name: "Close" })); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); expect(api.call).not.toHaveBeenCalledWith("markWelcomeSeen", expect.anything()); // Still on Settings. expect(screen.getByRole("heading", { level: 1, name: "Settings" })).toBeInTheDocument(); }); });