Skip to content

Commit ce730bb

Browse files
Merge branch 'main' into restartButton
2 parents e13bf11 + 4050eae commit ce730bb

16 files changed

+488
-569
lines changed

__tests__/game.integration.test.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const Game = require('../src/game.js');
66
const Render = require('../src/render.js');
77
const fs = require('fs');
88

9+
let game, render;
10+
911
describe('Game', () => {
1012
beforeEach(() => {
11-
document.body.innerHTML = fs.readFileSync('./index.html');
12-
render = new Render()
13+
document.body.innerHTML = fs.readFileSync('./index.html');
14+
render = new Render(true)
1315
game = new Game(render)
1416
});
1517

@@ -291,8 +293,7 @@ describe('Game', () => {
291293
game.activePlayer = game.players[0];
292294
game.generateTetromino(6)
293295
game.rotateTetromino()
294-
// console.log(game.grid)
295-
expect(game.grid).toEqual(
296+
expect(game.grid).toEqual(
296297
[
297298
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
298299
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -317,36 +318,6 @@ describe('Game', () => {
317318
])
318319
})
319320

320-
xtest('rotation test 6 (L-Piece) - testing new position for activeTetromino', () => {
321-
game.activePlayer = game.players[0];
322-
game.generateTetromino(2)
323-
game.rotateTetromino()
324-
expect(game.grid).toEqual(
325-
[
326-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
327-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
328-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
329-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
330-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
331-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
332-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
333-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
334-
[0, 0, 0, 0, 0, 3, 0, 0, 0, 0],
335-
[0, 0, 0, 0, 0, 3, 0, 0, 0, 0],
336-
[0, 0, 0, 0, 0, 3, 3, 0, 0, 0],
337-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
338-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
339-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
340-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
341-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
342-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
343-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
344-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
345-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
346-
]
347-
)
348-
})
349-
350321
test('rotation test 6 (I-Piece P1) - testing new position for activeTetromino', () => {
351322
game.activePlayer = game.players[0];
352323
game.generateTetromino(0)
@@ -536,5 +507,37 @@ describe('Game', () => {
536507
expect(generateTetrominoSpy).toHaveBeenCalled();
537508
});
538509

510+
test("Clearing lines increments the linesCleared var inside player1", () => {
511+
game.activePlayer = game.players[0];
512+
game.grid[19] = [1,1,1,1,1,1,1,1,1,1];
513+
expect(game.players[0].linesCleared).toBe(0);
514+
game.removeCompleteLines();
515+
expect(game.players[0].linesCleared).toBe(1);
516+
});
539517

518+
test("Clearing lines increments the linesCleared var inside player2", () => {
519+
game.activePlayer = game.players[1];
520+
game.grid[0] = [1,1,1,1,1,1,1,1,1,1];
521+
expect(game.players[1].linesCleared).toBe(0);
522+
game.removeCompleteLines();
523+
expect(game.players[1].linesCleared).toBe(1);
524+
});
525+
526+
test("Clearing lines increments the linesCleared multiple times inside player1", () => {
527+
game.activePlayer = game.players[0];
528+
game.grid[19] = [1,1,1,1,1,1,1,1,1,1];
529+
game.grid[18] = [1,1,1,1,1,1,1,1,1,1];
530+
expect(game.players[0].linesCleared).toBe(0);
531+
game.removeCompleteLines();
532+
expect(game.players[0].linesCleared).toBe(2);
533+
});
534+
535+
test("Clearing lines increments the linesCleared multiple times inside player2", () => {
536+
game.activePlayer = game.players[1];
537+
game.grid[0] = [1,1,1,1,1,1,1,1,1,1];
538+
game.grid[1] = [1,1,1,1,1,1,1,1,1,1];
539+
expect(game.players[1].linesCleared).toBe(0);
540+
game.removeCompleteLines();
541+
expect(game.players[1].linesCleared).toBe(2);
542+
});
540543
});

__tests__/game.test.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,130 @@ it('spawns the Z-Block if player 1 is the active player', () => {
471471
)
472472
})
473473

474+
it('BIZARRE - spawns the plus-Block if player 1 is the active player', () => {
475+
const random = 7
476+
game.activePlayer = game.players[0];
477+
game.generateTetromino(random)
478+
479+
expect(game.grid).toEqual(
480+
[
481+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
482+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
483+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
484+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
485+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
486+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
487+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
488+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
489+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
490+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
491+
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
492+
[0, 0, 0, 8, 8, 8, 0, 0, 0, 0],
493+
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
494+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
495+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
496+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
497+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
498+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
499+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
500+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
501+
]
502+
)
503+
})
504+
505+
it('BIZARRE - spawns the plus-Block if player 2 is the active player', () => {
506+
const random = 7
507+
game.activePlayer = game.players[1];
508+
game.generateTetromino(random)
509+
510+
expect(game.grid).toEqual(
511+
[
512+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
513+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
514+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
515+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
516+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
517+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
518+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
519+
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
520+
[0, 0, 0, 8, 8, 8, 0, 0, 0, 0],
521+
[0, 0, 0, 0, 8, 0, 0, 0, 0, 0],
522+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
523+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
524+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
525+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
526+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
527+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
528+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
529+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
530+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
531+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
532+
]
533+
)
534+
})
535+
536+
it('BIZARRE - spawns the u-Block if player 1 is the active player', () => {
537+
const random = 8
538+
game.activePlayer = game.players[0];
539+
game.generateTetromino(random)
540+
541+
expect(game.grid).toEqual(
542+
[
543+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
544+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
545+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
546+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
547+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
548+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
549+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
550+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
551+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
552+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
553+
[0, 0, 0, 9, 0, 9, 0, 0, 0, 0],
554+
[0, 0, 0, 9, 9, 9, 0, 0, 0, 0],
555+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
556+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
557+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
558+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
559+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
560+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
561+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
562+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
563+
]
564+
)
565+
})
566+
567+
it('BIZARRE - spawns the u-Block if player 2 is the active player', () => {
568+
const random = 8
569+
game.activePlayer = game.players[1];
570+
game.generateTetromino(random)
571+
572+
expect(game.grid).toEqual(
573+
[
574+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
575+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
576+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
577+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
578+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
579+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
580+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
581+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
582+
[0, 0, 0, 9, 9, 9, 0, 0, 0, 0],
583+
[0, 0, 0, 9, 0, 9, 0, 0, 0, 0],
584+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
585+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
586+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
587+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
588+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
589+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
590+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
591+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
592+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
593+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
594+
]
595+
)
596+
})
597+
474598
it('checks if activeTetromino equals the spawnPoint coordinates', () => {
475599
const random = 6
476600
game.activePlayer = game.players[0];
@@ -523,6 +647,7 @@ it('spawns the Z-Block if player 1 is the active player', () => {
523647
game.grid = [[1,1], [0,1], [1,1], [1,0], [0,1], [1,0]];
524648
game.removeCompleteLines();
525649
expect(game.grid).toEqual([[0,1], [0,0], [0,0], [1,0], [0,1], [1,0]]);
650+
game = new Game();
526651
game.grid = [[1,0], [0,1], [0,1], [1,1], [0,1], [1,1]];
527652
game.removeCompleteLines();
528653
expect(game.grid).toEqual([[1,0], [0,1], [0,1], [0,0], [0,0], [0,1]]);

__tests__/player.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
const Player = require('../src/player.js');
6+
const fs = require('fs');
7+
8+
describe('Player', () => {
9+
beforeEach(() => {
10+
document.body.innerHTML = fs.readFileSync('./index.html');
11+
})
12+
test('incrementLineCounter() correctly increments linesCleared', () => {
13+
const mockGame = {
14+
activePlayer: 'player 1',
15+
players : ['player 1', 'player 2']
16+
}
17+
const player = new Player(1, mockGame);
18+
expect(player.linesCleared).toBe(0);
19+
player.incrementLineCounter();
20+
expect(player.linesCleared).toBe(1);
21+
})
22+
})

__tests__/powerUps.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const powerUps = require('../src/powerUps.js')
2+
3+
describe('PowerUps.speedUp', () => {
4+
it('speeds up game for opponent', () => {
5+
const mockGame = {
6+
players : [{timer : 100}, {timer : 100}]
7+
}
8+
mockGame.activePlayer = mockGame.players[0]
9+
10+
powerUps.speedUp(mockGame)
11+
expect(mockGame.players[1].timer).toBe(80)
12+
})
13+
})
14+
15+
describe('PowerUps.bizarre', () => {
16+
it('generates bizarre block for opponent', () => {
17+
const mockGame = {
18+
bizarre : false
19+
}
20+
powerUps.bizarre(mockGame)
21+
expect(mockGame.bizarre).toBe(true)
22+
})
23+
})
24+
25+
describe('PowerUps.clearRandomBlock', () => {
26+
it('clears random block from opponents side (p1 perspective)', () => {
27+
const mockGame = {
28+
grid : [[2,1],[2,0]],
29+
players : [1,2],
30+
midRow : 0
31+
}
32+
mockGame.activePlayer = mockGame.players[0]
33+
34+
powerUps.removeRandomBlock(mockGame)
35+
36+
expect(mockGame.grid[0]).not.toEqual([2,1])
37+
expect(mockGame.grid[1]).toEqual([2,0])
38+
})
39+
})

__tests__/render.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ describe('Render class', () => {
4141
render.findSpawnLine([[0], [0], [0], [0]]);
4242
expect(document.querySelector('.spawnRow').id).toBe("row1");
4343
})
44+
45+
it('shows the correct active player', () => {
46+
render.displayActivePlayer('player1');
47+
expect(document.querySelector('#activePlayer').textContent).toBe('player1');
48+
})
4449
});

audio/gameOver.wav

606 KB
Binary file not shown.

0 commit comments

Comments
 (0)