From 349c47562cd68b0f51c3dac8633d2d0ee179518f Mon Sep 17 00:00:00 2001 From: Kewin Dousse Date: Mon, 19 Aug 2024 01:28:33 +0200 Subject: [PATCH] Adding some unit tests --- package.json | 3 ++- server/display.test.ts | 17 +++++++++++++ server/display.ts | 8 +----- server/game.test.ts | 58 +++++++++++++++++++++++++++++++++++++----- server/htmlColors.ts | 2 +- 5 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 server/display.test.ts diff --git a/package.json b/package.json index 220b6a1..906860e 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "watch:start": "nodemon --exec npm run start", "dev": "concurrently -n build,start \"npm run watch:build\" \"npm run watch:start\"", "dev:full": "concurrently -n client,server -c yellow,green \"npm run dev\" \"cd client && npm run dev\"", - "test": "jest" + "test": "jest", + "test:watch": "jest --watch" }, "contributors": [ "Protectator", diff --git a/server/display.test.ts b/server/display.test.ts new file mode 100644 index 0000000..f884804 --- /dev/null +++ b/server/display.test.ts @@ -0,0 +1,17 @@ +import {Display} from "./display"; +import {HtmlColors} from "./htmlColors"; + +const DISPLAY_SIZE = 100; + +function generateTestDisplay() { + return new Display(DISPLAY_SIZE, "", 13335, false); +} + +const FULL_BLACK = Array(DISPLAY_SIZE).fill(HtmlColors.black); + +describe('Display', () => { + test("is fully black at creation", () => { + const display = generateTestDisplay(); + expect(display.screenshot()).toStrictEqual(FULL_BLACK); + }); +}) diff --git a/server/display.ts b/server/display.ts index 970c42c..8653713 100644 --- a/server/display.ts +++ b/server/display.ts @@ -104,12 +104,6 @@ export class Display { } private newBlackArray(length: number): Color[] { - const arr: Color[] = []; - - for (let i = 0; i < length; i++) { - arr[i] = HtmlColors.black; - } - - return arr; + return Array(length).fill(HtmlColors.black); } } diff --git a/server/game.test.ts b/server/game.test.ts index b06c3f3..60a7af0 100644 --- a/server/game.test.ts +++ b/server/game.test.ts @@ -1,6 +1,7 @@ -import { Game } from "./game"; -import { Display } from "./display"; -import { GameOptions } from "./types/GameOptions"; +import {Game} from "./game"; +import {Display} from "./display"; +import {GameOptions} from "./types/GameOptions"; +import {Color} from "./color"; const gameOptions: GameOptions = { BattleRoyale: { @@ -23,8 +24,51 @@ const gameOptions: GameOptions = { }, }; -test("Game is created correctly", () => { - const display: Display = new Display(100, "", 13335, false); +const INITIAL_STATE = { + characters: [ + { + alive: true, + color: new Color(255, 0, 0, 0), + facesRight: true, + id: 0, + shotCooldown: 0, + shotRange: 18, + x: 0, + }, + { + alive: true, + color: new Color(0, 255, 255, 0), + facesRight: true, + id: 1, + shotCooldown: 0, + shotRange: 18, + x: 50, + } + ], + shots: [], + turnNb: 0, +}; + +function generateTestDisplayAndGame() { + const display = new Display(100, "", 13335, false); const game = new Game(20, 2, display, gameOptions); - expect(game.newInputs.length).toBe(0); -}); + return {display, game}; +} + +const FAKE_DATE = new Date('2020-01-01'); + +describe('Game', () => { + beforeEach(() => { + jest + .useFakeTimers() + .setSystemTime(FAKE_DATE); + }); + + test("is created correctly", () => { + const {game} = generateTestDisplayAndGame(); + expect(game.newInputs.length).toBe(0); + expect(game.gameState).toStrictEqual({ + ...INITIAL_STATE, startDate: FAKE_DATE, + }); + }); +}) diff --git a/server/htmlColors.ts b/server/htmlColors.ts index 84079c3..5c926b2 100644 --- a/server/htmlColors.ts +++ b/server/htmlColors.ts @@ -145,4 +145,4 @@ export const HtmlColors = { gainsboro: new Color(220, 220, 220), whitesmoke: new Color(245, 245, 245), white: new Color(255, 255, 255), -}; +} as const;