deadhead-space-freight-tycoon/apps/web/test/map-locked.test.tsx

78 lines
3.1 KiB
TypeScript

import { content } from "@deadhead/content";
import { checkRoute } from "@deadhead/sim";
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useClock } from "../src/clock/clock";
import { fakeApi, renderApp, samplePlayerState, sampleTutorialBoard } from "./render";
const NOW = Date.parse("2100-01-01T00:00:00Z");
describe("whole map from the start", () => {
beforeEach(() => {
vi.useFakeTimers({ toFake: ["Date", "setInterval", "clearInterval"] });
vi.setSystemTime(NOW);
useClock.setState({ offsetMs: 0, synced: true, now: NOW });
});
afterEach(() => vi.useRealTimers());
const openMap = async () => {
// A brand-new tutorial player.
renderApp(
fakeApi({
getPlayerState: () => samplePlayerState(),
getTutorialBoard: () => sampleTutorialBoard(),
}),
"/map",
);
return screen.findByTestId("map-canvas");
};
it("is available during the tutorial and shows every location", async () => {
const canvas = await openMap();
expect(within(canvas).getAllByRole("button", { name: /^Tap / })).toHaveLength(
content.locations.length + 1,
);
});
it("renders undiscovered locations as locked and discovered ones normally", async () => {
const canvas = await openMap();
for (const id of ["mars", "ceres-station", "jupiter-cloud-platform"]) {
const name = content.location(id)!.name;
expect(
within(canvas).getByRole("button", { name: `Tap ${name} (locked)` }),
).toBeInTheDocument();
}
expect(within(canvas).getByRole("button", { name: "Tap Earth" })).toBeInTheDocument();
expect(within(canvas).getByRole("button", { name: "Tap Moon" })).toBeInTheDocument();
});
it("lists the purchasable ships that can reach Mars from Earth now (no bases yet), per the sim's range check", async () => {
const canvas = await openMap();
await userEvent.click(within(canvas).getByRole("button", { name: "Tap Mars (locked)" }));
const panel = screen.getByRole("dialog", { name: "Mars" });
expect(panel).toHaveTextContent("Undiscovered — deliver here to unlock.");
const expected = content.purchasableShipModels
.filter(
(m) =>
checkRoute(m, content.location("earth")!, content.location("mars")!, content.config).ok,
)
.map((m) => m.name);
expect(expected.length).toBeGreaterThan(0);
expect(expected).not.toContain("Courier");
// A tutorial player has no bases yet, so reach is measured from Earth (CR-012).
expect(screen.getByLabelText("Reachable with")).toHaveTextContent(
`From Earth: ${expected.join(", ")}`,
);
});
it("shows details, not reachability, for a discovered location", async () => {
const canvas = await openMap();
await userEvent.click(within(canvas).getByRole("button", { name: "Tap Moon" }));
const panel = screen.getByRole("dialog", { name: "Moon" });
expect(panel).toHaveTextContent("Airless body");
expect(screen.queryByLabelText("Reachable with")).not.toBeInTheDocument();
});
});