Skip to content

Instantly share code, notes, and snippets.

@marcoacierno
Created February 10, 2023 00:43
Show Gist options
  • Select an option

  • Save marcoacierno/2246fc9d66c5aa5449fee91ee772ff11 to your computer and use it in GitHub Desktop.

Select an option

Save marcoacierno/2246fc9d66c5aa5449fee91ee772ff11 to your computer and use it in GitHub Desktop.
XState tests example
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