From 931d7b0fe6d311cbbf0bf9d363c28331ee6d7f93 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Tue, 24 Mar 2020 14:23:19 +0100 Subject: [PATCH] Added grid tests --- screens/Tetris/__tests__/GridManager.test.js | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/screens/Tetris/__tests__/GridManager.test.js b/screens/Tetris/__tests__/GridManager.test.js index 43d1f0e..8808754 100644 --- a/screens/Tetris/__tests__/GridManager.test.js +++ b/screens/Tetris/__tests__/GridManager.test.js @@ -1,10 +1,19 @@ import React from 'react'; import GridManager from "../GridManager"; +import ScoreManager from "../ScoreManager"; +import Piece from "../Piece"; let colors = { tetrisBackground: "#000002" }; +jest.mock("../ScoreManager"); + +afterAll(() => { + jest.restoreAllMocks(); +}); + + test('getEmptyLine', () => { let g = new GridManager(2, 2, colors); expect(g.getEmptyLine(2)).toStrictEqual([ @@ -32,3 +41,63 @@ test('getEmptyGrid', () => { expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]); }); +test('getLinesToClear', () => { + let g = new GridManager(2, 2, colors); + g.getCurrentGrid()[0][0].isEmpty = false; + g.getCurrentGrid()[0][1].isEmpty = false; + let coord = [{x: 1, y: 0}]; + expect(g.getLinesToClear(coord)).toStrictEqual([0]); + + g.getCurrentGrid()[0][0].isEmpty = true; + g.getCurrentGrid()[0][1].isEmpty = true; + g.getCurrentGrid()[1][0].isEmpty = false; + g.getCurrentGrid()[1][1].isEmpty = false; + expect(g.getLinesToClear(coord)).toStrictEqual([]); + coord = [{x: 1, y: 1}]; + expect(g.getLinesToClear(coord)).toStrictEqual([1]); +}); + +test('clearLines', () => { + let g = new GridManager(2, 2, colors); + let grid = [ + [ + {color: colors.tetrisBackground, isEmpty: true}, + {color: colors.tetrisBackground, isEmpty: true}, + ], + [ + {color: '0', isEmpty: true}, + {color: '0', isEmpty: true}, + ], + ]; + g.getCurrentGrid()[1][0].color = '0'; + g.getCurrentGrid()[1][1].color = '0'; + expect(g.getCurrentGrid()).toStrictEqual(grid); + let scoreManager = new ScoreManager(); + g.clearLines([1], scoreManager); + grid = [ + [ + {color: colors.tetrisBackground, isEmpty: true}, + {color: colors.tetrisBackground, isEmpty: true}, + ], + [ + {color: colors.tetrisBackground, isEmpty: true}, + {color: colors.tetrisBackground, isEmpty: true}, + ], + ]; + expect(g.getCurrentGrid()).toStrictEqual(grid); +}); + +test('freezeTetromino', () => { + let g = new GridManager(2, 2, colors); + let spy1 = jest.spyOn(GridManager.prototype, 'getLinesToClear') + .mockImplementation(() => {}); + let spy2 = jest.spyOn(GridManager.prototype, 'clearLines') + .mockImplementation(() => {}); + g.freezeTetromino(new Piece({}), null); + + expect(spy1).toHaveBeenCalled(); + expect(spy2).toHaveBeenCalled(); + + spy1.mockRestore(); + spy2.mockRestore(); +});