Files
OpenFrontIO/tests/client/GameStatsModal.test.ts
T
RyanandGitHub f070d4843d make it a dedicated stats modal, not under account (#4627)
## Description:

seperates the stats modal into a dedicated path, with gameID support
e.g. :

#modal=stats&gameID=VvyXpL9x

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory

## Please put your Discord username so you can be contacted if a bug or
regression is found:

w.o.n
2026-07-17 09:18:32 -07:00

97 lines
2.7 KiB
TypeScript

import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from "vitest";
vi.mock("../../src/client/Utils", () => ({
translateText: vi.fn((key: string) => key),
}));
vi.mock("../../src/client/components/baseComponents/stats/GameInfoView", () => {
class FakeGameInfoView extends HTMLElement {}
if (!customElements.get("game-info-view")) {
customElements.define("game-info-view", FakeGameInfoView);
}
return { GameInfoView: FakeGameInfoView };
});
import { GameStatsModal } from "../../src/client/GameStatsModal";
import { modalRouter } from "../../src/client/ModalRouter";
import { initNavigation } from "../../src/client/Navigation";
type ModalShell = HTMLElement & { updateComplete: Promise<boolean> };
describe("public game stats route", () => {
let modal: GameStatsModal;
let playPage: HTMLElement;
beforeAll(() => {
playPage = document.createElement("div");
playPage.id = "page-play";
document.body.appendChild(playPage);
initNavigation();
});
afterAll(() => {
playPage.remove();
});
beforeEach(async () => {
history.replaceState(null, "", "/");
modalRouter.register("stats", {
tag: "game-stats-modal",
pageId: "page-stats",
});
if (!customElements.get("game-stats-modal")) {
customElements.define("game-stats-modal", GameStatsModal);
}
modal = document.createElement("game-stats-modal") as GameStatsModal;
modal.id = "page-stats";
modal.setAttribute("inline", "");
modal.className = "hidden page-content";
document.body.appendChild(modal);
await modal.updateComplete;
});
afterEach(() => {
window.showPage?.("page-play");
modal.remove();
history.replaceState(null, "", "/");
});
it("opens a shared gameID without mounting the authenticated account", async () => {
history.replaceState(null, "", "/#modal=stats&gameID=public-game");
expect(modalRouter.routeFromHash()).toBe(true);
await vi.waitFor(async () => {
await modal.updateComplete;
const shell = modal.querySelector("o-modal") as ModalShell | null;
await shell?.updateComplete;
const statsView = modal.querySelector("game-info-view") as
| (HTMLElement & { gameId: string | null })
| null;
expect(modal.isOpen()).toBe(true);
expect(statsView?.gameId).toBe("public-game");
});
expect(document.querySelector("account-modal")).toBeNull();
expect(window.location.hash).toBe("#modal=stats&gameID=public-game");
const backButton = modal.querySelector(
'[slot="header"] button',
) as HTMLButtonElement;
backButton.click();
expect(modal.isOpen()).toBe(false);
expect(window.location.hash).toBe("");
});
});