|
@@ -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
|
+});
|