Forráskód Böngészése

Remaining gameStore tests

Kirk Trombley 4 éve
szülő
commit
70246ffa5d

+ 176 - 0
client/src/tests/__snapshots__/gameStore.test.js.snap

@@ -58,6 +58,166 @@ exports[`gameStore dispatch joinGame joins game then updates current round 3`] =
 }
 `;
 
+exports[`gameStore dispatch rejoinGame rejoins game then updates current round 1`] = `[MockFunction]`;
+
+exports[`gameStore dispatch rejoinGame rejoins game then updates current round 2`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      "test-player-id",
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "coord": "coord",
+        "currentRound": "round",
+        "timer": "timer",
+      },
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch rejoinGame rejoins game then updates current round even with missing information 1`] = `[MockFunction]`;
+
+exports[`gameStore dispatch rejoinGame rejoins game then updates current round even with missing information 2`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      "test-player-id",
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "coord": "coord",
+        "currentRound": "round",
+        "timer": "timer",
+      },
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits a timeout and then updates the current round 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      null,
+      "prev-round",
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "score": 0,
+        "totalScore": 2000,
+      },
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits a timeout and then updates the current round 2`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits a timeout and then updates the current round 3`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      null,
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "coord": "coord",
+        "currentRound": "round",
+        "timer": "timer",
+      },
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits guess and then updates the current round 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      null,
+      "prev-round",
+      "selected",
+      200,
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "score": 1000,
+        "totalScore": 2000,
+      },
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits guess and then updates the current round 2`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;
+
+exports[`gameStore dispatch submitGuess submits guess and then updates the current round 3`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      "test-game-id",
+      null,
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": Object {
+        "coord": "coord",
+        "currentRound": "round",
+        "timer": "timer",
+      },
+    },
+  ],
+}
+`;
+
 exports[`gameStore dispatch updateCurrentRound updates current round 1`] = `
 [MockFunction] {
   "calls": Array [
@@ -78,3 +238,19 @@ exports[`gameStore dispatch updateCurrentRound updates current round 1`] = `
   ],
 }
 `;
+
+exports[`gameStore dispatch updateRoundSeconds applies an update to round seconds 1`] = `
+[MockFunction] {
+  "calls": Array [
+    Array [
+      9,
+    ],
+  ],
+  "results": Array [
+    Object {
+      "type": "return",
+      "value": undefined,
+    },
+  ],
+}
+`;

+ 142 - 1
client/src/tests/gameStore.test.js

@@ -4,7 +4,12 @@ jest.mock("../store");
 jest.mock("../domain/apiMethods");
 jest.mock("../domain/localStorageMethods");
 
-import { getCurrentRound, joinGame } from "../domain/apiMethods";
+import {
+  getCurrentRound,
+  joinGame,
+  sendGuess,
+  sendTimeout,
+} from "../domain/apiMethods";
 import { IN_ROUND, POST_GAME, PRE_ROUND } from "../domain/gameStates";
 import {
   dispatch,
@@ -16,11 +21,14 @@ import {
   usePanoStartPosition,
   usePanoStartPov,
   useRoundSeconds,
+  useLastRound,
 } from "../domain/gameStore";
 import {
   clearGameInfoFromLocalStorage,
   clearRoundInfoFromLocalStorage,
+  getInfoFromLocalStorage,
   saveGameInfoToLocalStorage,
+  saveTimerToLocalStorage,
 } from "../domain/localStorageMethods";
 
 describe("gameStore", () => {
@@ -117,5 +125,138 @@ describe("gameStore", () => {
         expect(getCurrentRound).toMatchSnapshot();
       });
     });
+    describe("rejoinGame", () => {
+      it("rejoins game then updates current round", async () => {
+        getInfoFromLocalStorage.mockReturnValue({
+          gameId: "test-game-id",
+          playerName: "test-player-name",
+          playerId: "test-player-id",
+          timer: "test-timer",
+          position: "test-position",
+          pov: "test-pov",
+        });
+        getCurrentRound.mockReturnValue({
+          currentRound: "round",
+          coord: "coord",
+          timer: "timer",
+        });
+        await dispatch.rejoinGame();
+        expect(saveGameInfoToLocalStorage).toMatchSnapshot();
+        expect(useGameId()).toBe("test-game-id");
+        expect(usePlayerName()).toBe("test-player-name");
+        expect(dispatch.GET_STORED_VALUE("playerId")).toBe("test-player-id");
+        expect(useCurrentRound()).toBe("round");
+        expect(useTargetPoint()).toBe("coord");
+        expect(usePanoStartPosition()).toBe("test-position");
+        expect(usePanoStartPov()).toBe("test-pov");
+        expect(useRoundSeconds()).toBe("test-timer");
+        expect(useGameState()).toBe(IN_ROUND);
+        expect(getCurrentRound).toMatchSnapshot();
+      });
+      it("rejoins game then updates current round even with missing information", async () => {
+        getInfoFromLocalStorage.mockReturnValue({
+          gameId: "test-game-id",
+          playerName: "test-player-name",
+          playerId: "test-player-id",
+        });
+        getCurrentRound.mockReturnValue({
+          currentRound: "round",
+          coord: "coord",
+          timer: "timer",
+        });
+        await dispatch.rejoinGame();
+        expect(saveGameInfoToLocalStorage).toMatchSnapshot();
+        expect(useGameId()).toBe("test-game-id");
+        expect(usePlayerName()).toBe("test-player-name");
+        expect(dispatch.GET_STORED_VALUE("playerId")).toBe("test-player-id");
+        expect(useCurrentRound()).toBe("round");
+        expect(useTargetPoint()).toBe("coord");
+        expect(usePanoStartPosition()).toBe("coord");
+        expect(usePanoStartPov()).toMatchObject({
+          heading: 0,
+          pitch: 0,
+        });
+        expect(useRoundSeconds()).toBe("timer");
+        expect(useGameState()).toBe(IN_ROUND);
+        expect(getCurrentRound).toMatchSnapshot();
+      });
+    });
+    describe("submitGuess", () => {
+      it("submits guess and then updates the current round", async () => {
+        dispatch.SET_STORED_VALUE("gameId", "test-game-id");
+        dispatch.SET_STORED_VALUE("playerName", "test-player-name");
+        dispatch.SET_STORED_VALUE("currentRound", "prev-round");
+        dispatch.SET_STORED_VALUE("roundSeconds", 200);
+        dispatch.SET_STORED_VALUE("targetPoint", "prev-target");
+        sendGuess.mockReturnValue({
+          score: 1000,
+          totalScore: 2000,
+        });
+        getCurrentRound.mockReturnValue({
+          currentRound: "round",
+          coord: "coord",
+          timer: "timer",
+        });
+        await dispatch.submitGuess("selected");
+        expect(sendGuess).toMatchSnapshot();
+        expect(clearRoundInfoFromLocalStorage).toMatchSnapshot();
+        expect(useLastRound()).toMatchObject({
+          roundNum: "prev-round",
+          targetPoint: "prev-target",
+          score: 1000,
+          totalScore: 2000,
+        });
+        expect(useCurrentRound()).toBe("round");
+        expect(useTargetPoint()).toBe("coord");
+        expect(usePanoStartPosition()).toBe("coord");
+        expect(usePanoStartPov()).toMatchObject({
+          heading: 0,
+          pitch: 0,
+        });
+        expect(useRoundSeconds()).toBe("timer");
+        expect(getCurrentRound).toMatchSnapshot();
+      });
+      it("submits a timeout and then updates the current round", async () => {
+        dispatch.SET_STORED_VALUE("gameId", "test-game-id");
+        dispatch.SET_STORED_VALUE("playerName", "test-player-name");
+        dispatch.SET_STORED_VALUE("currentRound", "prev-round");
+        dispatch.SET_STORED_VALUE("targetPoint", "prev-target");
+        sendTimeout.mockReturnValue({
+          score: 0,
+          totalScore: 2000,
+        });
+        getCurrentRound.mockReturnValue({
+          currentRound: "round",
+          coord: "coord",
+          timer: "timer",
+        });
+        await dispatch.submitGuess(null);
+        expect(sendTimeout).toMatchSnapshot();
+        expect(clearRoundInfoFromLocalStorage).toMatchSnapshot();
+        expect(useLastRound()).toMatchObject({
+          roundNum: "prev-round",
+          targetPoint: "prev-target",
+          score: 0,
+          totalScore: 2000,
+        });
+        expect(useCurrentRound()).toBe("round");
+        expect(useTargetPoint()).toBe("coord");
+        expect(usePanoStartPosition()).toBe("coord");
+        expect(usePanoStartPov()).toMatchObject({
+          heading: 0,
+          pitch: 0,
+        });
+        expect(useRoundSeconds()).toBe("timer");
+        expect(getCurrentRound).toMatchSnapshot();
+      });
+    });
+    describe("updateRoundSeconds", () => {
+      it("applies an update to round seconds", () => {
+        dispatch.SET_STORED_VALUE("roundSeconds", 10);
+        dispatch.updateRoundSeconds(c => c - 1);
+        expect(useRoundSeconds()).toBe(9);
+        expect(saveTimerToLocalStorage).toMatchSnapshot();
+      });
+    });
   });
 });