Skip to content

Commit

Permalink
Created tests for Game class
Browse files Browse the repository at this point in the history
  • Loading branch information
lucfercas committed Oct 16, 2024
1 parent f2e6e31 commit 099ee59
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions api/tests/lib/game.test.js
Original file line number Diff line number Diff line change
@@ -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]));
});
});
});

0 comments on commit 099ee59

Please sign in to comment.