Skip to content

Commit

Permalink
Merge pull request #97 from Pecamo/more-game-unit-tests
Browse files Browse the repository at this point in the history
Adding some unit tests
  • Loading branch information
BinaryBrain authored Aug 21, 2024
2 parents d50355a + 349c475 commit 55451b4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions server/display.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
})
8 changes: 1 addition & 7 deletions server/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
58 changes: 51 additions & 7 deletions server/game.test.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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,
});
});
})
2 changes: 1 addition & 1 deletion server/htmlColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 55451b4

Please # to comment.