Skip to content

Commit

Permalink
Add tests for player context on playerLeaves
Browse files Browse the repository at this point in the history
Test that playerLeaves action removes players from the context in each state
  • Loading branch information
lookupdaily committed Dec 20, 2023
1 parent 4d76fbc commit 332d7b7
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions server/machines/lobby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ describe("lobbyMachine states", () => {
"Empty",
);
});

it("removes a player from the player list when it receives playerLeaves event", () => {
const actor = interpret(lobbyMachine);
actor.start();

actor.send({
type: "playerJoins",
player: { socketId: "id", name: "a name" },
});

actor.send({
type: "playerLeaves",
socketId: "id",
});

expect(actor.getSnapshot().context.players.length).toEqual(0);
});
});

describe("MultiplePlayers", () => {
Expand Down Expand Up @@ -94,6 +111,31 @@ describe("lobbyMachine states", () => {
lobbyMachine.transition("MultiplePlayers", "playerLeaves").value,
).toBe("OnePlayer");
});

it("removes a player from the player list when it receives playerLeaves event", () => {
const actor = interpret(lobbyMachine);
const players = [
{ socketId: "id", name: "a name" },
{ socketId: "id-2", name: "a name" },
];
actor.start();

players.forEach((player) => {
actor.send({
type: "playerJoins",
player,
});
});

actor.send({
type: "playerLeaves",
socketId: "id",
});

expect(actor.getSnapshot().context.players).toEqual([
{ socketId: "id-2", name: "a name" },
]);
});
});

describe("GameStart", () => {
Expand Down Expand Up @@ -143,6 +185,32 @@ describe("lobbyMachine states", () => {
expect(actor.getSnapshot().value).toBe("GameStart");
});

it("removes a player from the player list when it receives playerLeaves event", () => {
const actor = interpret(lobbyMachine);
const players = [
{ socketId: "id", name: "a name" },
{ socketId: "id-2", name: "a name" },
];
actor.start();

players.forEach((player) => {
actor.send({
type: "playerJoins",
player,
});
});

actor.send({ type: "playerClicksStart" });

actor.send({
type: "playerLeaves",
socketId: "id",
});

expect(actor.getSnapshot().context.players).toEqual([
{ socketId: "id-2", name: "a name" },
]);
});
});

describe("On playerLeaves", () => {
Expand Down

0 comments on commit 332d7b7

Please sign in to comment.