Browse Source

Added tests for Piece class

Arnaud Vergnet 4 years ago
parent
commit
fcec2a3c8a

+ 0
- 12
.idea/runConfigurations/All_Tests__coverage_.xml View File

@@ -1,12 +0,0 @@
1
-<component name="ProjectRunConfigurationManager">
2
-  <configuration default="false" name="All Tests (coverage)" type="JavaScriptTestRunnerJest">
3
-    <node-interpreter value="project" />
4
-    <node-options value="" />
5
-    <jest-package value="$PROJECT_DIR$/node_modules/jest" />
6
-    <working-dir value="$PROJECT_DIR$" />
7
-    <jest-options value="--coverage" />
8
-    <envs />
9
-    <scope-kind value="ALL" />
10
-    <method v="2" />
11
-  </configuration>
12
-</component>

+ 5
- 1
screens/Tetris/Piece.js View File

@@ -21,7 +21,11 @@ export default class Piece {
21 21
     #currentShape: Object;
22 22
 
23 23
     constructor(colors: Object) {
24
-        this.#currentShape = new this.#shapes[Math.floor(Math.random() * 7)](colors);
24
+        this.#currentShape = this.getRandomShape(colors);
25
+    }
26
+
27
+    getRandomShape(colors: Object) {
28
+        return new this.#shapes[Math.floor(Math.random() * 7)](colors);
25 29
     }
26 30
 
27 31
     toGrid(grid: Array<Array<Object>>, isPreview: boolean) {

+ 109
- 0
screens/Tetris/__tests__/Piece.test.js View File

@@ -0,0 +1,109 @@
1
+import React from 'react';
2
+import Piece from "../Piece";
3
+import ShapeI from "../Shapes/ShapeI";
4
+
5
+let colors = {
6
+    tetrisI: "#000001",
7
+    tetrisBackground: "#000002"
8
+};
9
+
10
+jest.mock("../Shapes/ShapeI");
11
+
12
+beforeAll(() => {
13
+    jest.spyOn(Piece.prototype, 'getRandomShape')
14
+        .mockImplementation((colors: Object) => {return new ShapeI(colors);});
15
+});
16
+
17
+afterAll(() => {
18
+    jest.restoreAllMocks();
19
+});
20
+
21
+test('isPositionValid', () => {
22
+    let x = 0;
23
+    let y = 0;
24
+    let spy = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates')
25
+        .mockImplementation(() => {return [{x: x, y: y}];});
26
+    let grid = [
27
+        [{isEmpty: true}, {isEmpty: true}],
28
+        [{isEmpty: true}, {isEmpty: false}],
29
+    ];
30
+    let size = 2;
31
+
32
+    let p = new Piece(colors);
33
+    expect(p.isPositionValid(grid, size, size)).toBeTrue();
34
+    x = 1; y = 0;
35
+    expect(p.isPositionValid(grid, size, size)).toBeTrue();
36
+    x = 0; y = 1;
37
+    expect(p.isPositionValid(grid, size, size)).toBeTrue();
38
+    x = 1; y = 1;
39
+    expect(p.isPositionValid(grid, size, size)).toBeFalse();
40
+    x = 2; y = 0;
41
+    expect(p.isPositionValid(grid, size, size)).toBeFalse();
42
+    x = -1; y = 0;
43
+    expect(p.isPositionValid(grid, size, size)).toBeFalse();
44
+    x = 0; y = 2;
45
+    expect(p.isPositionValid(grid, size, size)).toBeFalse();
46
+    x = 0; y = -1;
47
+    expect(p.isPositionValid(grid, size, size)).toBeFalse();
48
+
49
+    spy.mockRestore();
50
+});
51
+
52
+test('tryMove', () => {
53
+    let p = new Piece(colors);
54
+    const callbackMock = jest.fn();
55
+    let isValid = true;
56
+    let spy = jest.spyOn(Piece.prototype, 'isPositionValid')
57
+        .mockImplementation(() => {return isValid;});
58
+
59
+    expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
60
+    isValid = false;
61
+    expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
62
+    isValid = true;
63
+    expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
64
+    expect(callbackMock).toBeCalledTimes(0);
65
+
66
+    isValid = false;
67
+    expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
68
+    expect(callbackMock).toBeCalledTimes(1);
69
+
70
+    spy.mockRestore();
71
+});
72
+
73
+test('tryRotate', () => {
74
+    let p = new Piece(colors);
75
+    let isValid = true;
76
+    let spy = jest.spyOn(Piece.prototype, 'isPositionValid')
77
+        .mockImplementation(() => {return isValid;});
78
+
79
+    expect(p.tryRotate( null, null, null)).toBeTrue();
80
+    isValid = false;
81
+    expect(p.tryRotate( null, null, null)).toBeFalse();
82
+
83
+    spy.mockRestore();
84
+});
85
+
86
+
87
+test('toGrid', () => {
88
+    let x = 0;
89
+    let y = 0;
90
+    let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates')
91
+        .mockImplementation(() => {return [{x: x, y: y}];});
92
+    let spy2 = jest.spyOn(ShapeI.prototype, 'getColor')
93
+        .mockImplementation(() => {return colors.tetrisI;});
94
+    let grid = [
95
+        [{isEmpty: true}, {isEmpty: true}],
96
+        [{isEmpty: true}, {isEmpty: true}],
97
+    ];
98
+    let expectedGrid = [
99
+        [{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}],
100
+        [{isEmpty: true}, {isEmpty: true}],
101
+    ];
102
+
103
+    let p = new Piece(colors);
104
+    p.toGrid(grid, true);
105
+    expect(grid).toStrictEqual(expectedGrid);
106
+
107
+    spy1.mockRestore();
108
+    spy2.mockRestore();
109
+});

screens/Tetris/__tests__/Tetromino.test.js → screens/Tetris/__tests__/Shape.test.js View File


Loading…
Cancel
Save