Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Piece.test.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /* eslint-disable */
  20. import React from 'react';
  21. import Piece from '../logic/Piece';
  22. import ShapeI from '../Shapes/ShapeI';
  23. let theme = {
  24. colors: {
  25. tetrisI: '#000001',
  26. tetrisBackground: '#000002',
  27. },
  28. };
  29. jest.mock('../Shapes/ShapeI');
  30. beforeAll(() => {
  31. jest
  32. .spyOn(Piece.prototype, 'getRandomShape')
  33. .mockImplementation((colors: Object) => {
  34. return new ShapeI(colors);
  35. });
  36. });
  37. afterAll(() => {
  38. jest.restoreAllMocks();
  39. });
  40. test('isPositionValid', () => {
  41. let x = 0;
  42. let y = 0;
  43. let spy = jest
  44. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  45. .mockImplementation(() => {
  46. return [{x: x, y: y}];
  47. });
  48. let grid = [
  49. [{isEmpty: true}, {isEmpty: true}],
  50. [{isEmpty: true}, {isEmpty: false}],
  51. ];
  52. let size = 2;
  53. let p = new Piece(theme);
  54. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  55. x = 1;
  56. y = 0;
  57. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  58. x = 0;
  59. y = 1;
  60. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  61. x = 1;
  62. y = 1;
  63. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  64. x = 2;
  65. y = 0;
  66. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  67. x = -1;
  68. y = 0;
  69. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  70. x = 0;
  71. y = 2;
  72. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  73. x = 0;
  74. y = -1;
  75. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  76. spy.mockRestore();
  77. });
  78. test('tryMove', () => {
  79. let p = new Piece(theme);
  80. const callbackMock = jest.fn();
  81. let isValid = true;
  82. let spy1 = jest
  83. .spyOn(Piece.prototype, 'isPositionValid')
  84. .mockImplementation(() => {
  85. return isValid;
  86. });
  87. let spy2 = jest
  88. .spyOn(Piece.prototype, 'removeFromGrid')
  89. .mockImplementation(() => {});
  90. let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
  91. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
  92. isValid = false;
  93. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
  94. isValid = true;
  95. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
  96. expect(callbackMock).toBeCalledTimes(0);
  97. isValid = false;
  98. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
  99. expect(callbackMock).toBeCalledTimes(1);
  100. expect(spy2).toBeCalledTimes(4);
  101. expect(spy3).toBeCalledTimes(4);
  102. spy1.mockRestore();
  103. spy2.mockRestore();
  104. spy3.mockRestore();
  105. });
  106. test('tryRotate', () => {
  107. let p = new Piece(theme);
  108. let isValid = true;
  109. let spy1 = jest
  110. .spyOn(Piece.prototype, 'isPositionValid')
  111. .mockImplementation(() => {
  112. return isValid;
  113. });
  114. let spy2 = jest
  115. .spyOn(Piece.prototype, 'removeFromGrid')
  116. .mockImplementation(() => {});
  117. let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
  118. expect(p.tryRotate(null, null, null)).toBeTrue();
  119. isValid = false;
  120. expect(p.tryRotate(null, null, null)).toBeFalse();
  121. expect(spy2).toBeCalledTimes(2);
  122. expect(spy3).toBeCalledTimes(2);
  123. spy1.mockRestore();
  124. spy2.mockRestore();
  125. spy3.mockRestore();
  126. });
  127. test('toGrid', () => {
  128. let x = 0;
  129. let y = 0;
  130. let spy1 = jest
  131. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  132. .mockImplementation(() => {
  133. return [{x: x, y: y}];
  134. });
  135. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
  136. return theme.colors.tetrisI;
  137. });
  138. let grid = [
  139. [
  140. {isEmpty: true, key: '0'},
  141. {isEmpty: true, key: '1'},
  142. ],
  143. [
  144. {isEmpty: true, key: '0'},
  145. {isEmpty: true, key: '1'},
  146. ],
  147. ];
  148. let expectedGrid = [
  149. [
  150. {color: theme.colors.tetrisI, isEmpty: false, key: '0'},
  151. {isEmpty: true, key: '1'},
  152. ],
  153. [
  154. {isEmpty: true, key: '0'},
  155. {isEmpty: true, key: '1'},
  156. ],
  157. ];
  158. let p = new Piece(theme);
  159. p.toGrid(grid, true);
  160. expect(grid).toStrictEqual(expectedGrid);
  161. spy1.mockRestore();
  162. spy2.mockRestore();
  163. });
  164. test('removeFromGrid', () => {
  165. let gridOld = [
  166. [
  167. {color: theme.colors.tetrisI, isEmpty: false, key: '0'},
  168. {color: theme.colors.tetrisI, isEmpty: false, key: '1'},
  169. {color: theme.colors.tetrisBackground, isEmpty: true, key: '2'},
  170. ],
  171. ];
  172. let gridNew = [
  173. [
  174. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  175. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  176. {color: theme.colors.tetrisBackground, isEmpty: true, key: '2'},
  177. ],
  178. ];
  179. let oldCoord = [
  180. {x: 0, y: 0},
  181. {x: 1, y: 0},
  182. ];
  183. let spy1 = jest
  184. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  185. .mockImplementation(() => {
  186. return oldCoord;
  187. });
  188. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
  189. return theme.colors.tetrisI;
  190. });
  191. let p = new Piece(theme);
  192. p.removeFromGrid(gridOld);
  193. expect(gridOld).toStrictEqual(gridNew);
  194. spy1.mockRestore();
  195. spy2.mockRestore();
  196. });