From 099ee59e4b3fa1e28ec1d1e65ed859c9c4470d01 Mon Sep 17 00:00:00 2001 From: Lucia_F Date: Wed, 16 Oct 2024 11:27:05 +0100 Subject: [PATCH] Created tests for Game class --- api/tests/lib/game.test.js | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 api/tests/lib/game.test.js diff --git a/api/tests/lib/game.test.js b/api/tests/lib/game.test.js new file mode 100644 index 0000000..26d007b --- /dev/null +++ b/api/tests/lib/game.test.js @@ -0,0 +1,54 @@ +const Game = require("../../classes/Game"); + + +describe("Game", () => { + let game; + + beforeEach(() => { + game = new Game(); + }); + + describe("Initialization", () => { + test("should initialise with an empty players array", () => { + expect(game.players).toEqual([]); + }); + + test("should initialise with a target number between 1 and 100", () => { + expect(game.targetNumber).toBeGreaterThanOrEqual(1); + expect(game.targetNumber).toBeLessThanOrEqual(100); + }); + }); + + describe("generateRandomNumber", () => { + test("should return a number between 1 and 100", () => { + const randomNumber = game.generateRandomNumber(); + expect(randomNumber).toBeGreaterThanOrEqual(1); + expect(randomNumber).toBeLessThanOrEqual(100); + }); + + test("should generate different numbers on subsequent calls", () => { + const randomNumber1 = game.generateRandomNumber(); + const randomNumber2 = game.generateRandomNumber(); + expect(randomNumber1).not.toEqual(randomNumber2); // Not always true, but generally should be. + }); + }); + + describe("addPlayer", () => { + test("should add a player to the players array", () => { + const user = { id: 1, name: "John" }; + game.addPlayer(user); + expect(game.players).toContain(user); + }); + + test("should correctly add multiple players", () => { + const user1 = { id: 1, name: "John" }; + const user2 = { id: 2, name: "Jane" }; + + game.addPlayer(user1); + game.addPlayer(user2); + + expect(game.players).toHaveLength(2); + expect(game.players).toEqual(expect.arrayContaining([user1, user2])); + }); + }); +});