Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

MoveDial.test.tsx

100 lines3.6 KB

1import { render, screen } from "@testing-library/react"
2import { beforeEach, describe, expect, it, vi } from "vitest"
3import { REFILL_INTERVAL_MS, useQuota } from "../../../../src/contexts/quota-context"
4import { TeamProvider } from "../../../../src/contexts/team-context"
5import { MoveDial } from "../../../../src/components/ui/MoveDial"
6​
7vi.mock("../../../../src/contexts/quota-context", async (importOriginal) => ({
8 ...(await importOriginal<typeof import("../../../../src/contexts/quota-context")>()),
9 useQuota: vi.fn(),
10}))
11const mockUseQuota = vi.mocked(useQuota)
12​
13function showDial(remaining = 8, nextRefillAt: number | null = null) {
14 mockUseQuota.mockReturnValue({
15 remaining,
16 max: 8,
17 refillIn: 0,
18 nextRefillAt,
19 emptyMoveAttempts: 0,
20 notifyNoMoves: vi.fn(),
21 consume: vi.fn(),
22 syncFromServer: vi.fn(),
23 refund: vi.fn(),
24 bonusCount: 0,
25 bonusPosition: null,
26 setBonusPosition: vi.fn(),
27 clearBonus: vi.fn(),
28 localBoxesCompleted: 0,
29 })
30 return render(
31 <TeamProvider team="BLUE" teamId={1}>
32 <MoveDial />
33 </TeamProvider>,
34 )
36​
37describe("MoveDial", () => {
38 beforeEach(() => vi.clearAllMocks())
39​
40 it("shows the available count and an accessible description", () => {
41 showDial(5)
42 expect(screen.getByTestId("move-dial-count")).toHaveTextContent("5")
43 expect(screen.getByRole("img")).toHaveAccessibleName("5 of 8 moves available")
44 })
45​
46 it.each([0, 3, 8])("fills exactly %i of eight segments", (remaining) => {
47 showDial(remaining)
48 const segments = screen.getByTestId("move-dial-segments")
49 expect(segments.children).toHaveLength(8)
50 expect(segments.querySelectorAll('[data-filled="true"]')).toHaveLength(remaining)
51 })
52​
53 it("uses the player's team colour", () => {
54 showDial()
55 expect(screen.getByTestId("move-dial").style.getPropertyValue("--ammo-color")).toBe("#1E88E5")
56 })
57​
58 it("shows the refill state when no moves remain", () => {
59 showDial(0)
60 expect(screen.getByTestId("move-dial")).toHaveAttribute("data-empty", "true")
61 expect(screen.getByRole("img")).toHaveAccessibleName("0 of 8 moves available, recharging")
62 expect(screen.getByText("Refill")).toBeInTheDocument()
63 })
64​
65 it("animates the next empty segment until the refill deadline", () => {
66 showDial(2, Date.now() + REFILL_INTERVAL_MS)
67 expect(screen.getByTestId("move-dial-charge")).toBeInTheDocument()
68 })
69​
70 it("has no refill sweep when full", () => {
71 showDial(8, Date.now() + REFILL_INTERVAL_MS)
72 expect(screen.queryByTestId("move-dial-charge")).not.toBeInTheDocument()
73 })
74​
75 it("keeps the refill sweep and number stable across a spend followed by a box bonus", () => {
76 const deadline = Date.now() + REFILL_INTERVAL_MS
77 const { rerender } = showDial(5, deadline)
78 const charge = screen.getByTestId("move-dial-charge")
79 const count = screen.getByTestId("move-dial-count")
80 const timing = charge.getAttribute("style")
81 const quota = mockUseQuota()
82​
83 // The optimistic spend and authoritative refund + bonus arrive separately.
84 for (const remaining of [4, 6]) {
85 mockUseQuota.mockReturnValue({ ...quota, remaining })
86 rerender(
87 <TeamProvider team="BLUE" teamId={1}>
88 <MoveDial />
89 </TeamProvider>,
90 )
91 expect(screen.getByTestId("move-dial-charge")).toBe(charge)
92 expect(charge.getAttribute("style")).toBe(timing)
93 expect(screen.getByTestId("move-dial-count")).toBe(count)
94 expect(count).toHaveTextContent(String(remaining))
95 expect(
96 screen.getByTestId("move-dial-segments").querySelectorAll('[data-filled="true"]'),
97 ).toHaveLength(remaining)
98 }
99 })
100})