Created
February 10, 2023 00:43
-
-
Save marcoacierno/2246fc9d66c5aa5449fee91ee772ff11 to your computer and use it in GitHub Desktop.
XState tests example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { waitFor } from "xstate/lib/waitFor"; | |
| import { interpret } from "xstate"; | |
| describe("Example", () => { | |
| it("answering with the wrong shells loses the turn", async () => { | |
| const mockGameMachine = gameMachine.withConfig({ | |
| actions: { | |
| redirectToGame: jest.fn(), | |
| }, | |
| services: { | |
| loadConfig: () => | |
| Promise.resolve({ | |
| shells: 3, | |
| difficulty: { | |
| name: "test", | |
| id: "test", | |
| numberOfShuffles: 1, | |
| shuffleSpeedInMs: 1, | |
| }, | |
| }), | |
| }, | |
| }); | |
| const service = interpret(mockGameMachine); | |
| service.start(); | |
| await waitFor(service, (state) => state.matches("idle")); | |
| service.send("START_TURN"); | |
| await waitFor(service, (state) => state.matches("showingBall")); | |
| service.send("START_SHUFFLE"); | |
| await waitFor(service, (state) => state.matches("shuffle")); | |
| service.send("PLAYER_GUESS"); | |
| await waitFor(service, (state) => state.matches("guessing")); | |
| await service.send({ | |
| type: "GUESS_SHELL", | |
| shell: 99, | |
| }); | |
| await waitFor(service, (state) => state.matches("lose")); | |
| expect(service.getSnapshot().context.stats.losses).toBe(1); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment