Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding some unit tests #97

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const as const?

Loading