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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* eslint-disable */
  2. import React from 'react';
  3. import Piece from '../logic/Piece';
  4. import ShapeI from '../Shapes/ShapeI';
  5. let colors = {
  6. tetrisI: '#000001',
  7. tetrisBackground: '#000002',
  8. };
  9. jest.mock('../Shapes/ShapeI');
  10. beforeAll(() => {
  11. jest
  12. .spyOn(Piece.prototype, 'getRandomShape')
  13. .mockImplementation((colors: Object) => {
  14. return new ShapeI(colors);
  15. });
  16. });
  17. afterAll(() => {
  18. jest.restoreAllMocks();
  19. });
  20. test('isPositionValid', () => {
  21. let x = 0;
  22. let y = 0;
  23. let spy = jest
  24. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  25. .mockImplementation(() => {
  26. return [{x: x, y: y}];
  27. });
  28. let grid = [
  29. [{isEmpty: true}, {isEmpty: true}],
  30. [{isEmpty: true}, {isEmpty: false}],
  31. ];
  32. let size = 2;
  33. let p = new Piece(colors);
  34. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  35. x = 1;
  36. y = 0;
  37. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  38. x = 0;
  39. y = 1;
  40. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  41. x = 1;
  42. y = 1;
  43. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  44. x = 2;
  45. y = 0;
  46. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  47. x = -1;
  48. y = 0;
  49. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  50. x = 0;
  51. y = 2;
  52. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  53. x = 0;
  54. y = -1;
  55. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  56. spy.mockRestore();
  57. });
  58. test('tryMove', () => {
  59. let p = new Piece(colors);
  60. const callbackMock = jest.fn();
  61. let isValid = true;
  62. let spy1 = jest
  63. .spyOn(Piece.prototype, 'isPositionValid')
  64. .mockImplementation(() => {
  65. return isValid;
  66. });
  67. let spy2 = jest
  68. .spyOn(Piece.prototype, 'removeFromGrid')
  69. .mockImplementation(() => {});
  70. let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
  71. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
  72. isValid = false;
  73. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
  74. isValid = true;
  75. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
  76. expect(callbackMock).toBeCalledTimes(0);
  77. isValid = false;
  78. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
  79. expect(callbackMock).toBeCalledTimes(1);
  80. expect(spy2).toBeCalledTimes(4);
  81. expect(spy3).toBeCalledTimes(4);
  82. spy1.mockRestore();
  83. spy2.mockRestore();
  84. spy3.mockRestore();
  85. });
  86. test('tryRotate', () => {
  87. let p = new Piece(colors);
  88. let isValid = true;
  89. let spy1 = jest
  90. .spyOn(Piece.prototype, 'isPositionValid')
  91. .mockImplementation(() => {
  92. return isValid;
  93. });
  94. let spy2 = jest
  95. .spyOn(Piece.prototype, 'removeFromGrid')
  96. .mockImplementation(() => {});
  97. let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
  98. expect(p.tryRotate(null, null, null)).toBeTrue();
  99. isValid = false;
  100. expect(p.tryRotate(null, null, null)).toBeFalse();
  101. expect(spy2).toBeCalledTimes(2);
  102. expect(spy3).toBeCalledTimes(2);
  103. spy1.mockRestore();
  104. spy2.mockRestore();
  105. spy3.mockRestore();
  106. });
  107. test('toGrid', () => {
  108. let x = 0;
  109. let y = 0;
  110. let spy1 = jest
  111. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  112. .mockImplementation(() => {
  113. return [{x: x, y: y}];
  114. });
  115. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
  116. return colors.tetrisI;
  117. });
  118. let grid = [
  119. [{isEmpty: true}, {isEmpty: true}],
  120. [{isEmpty: true}, {isEmpty: true}],
  121. ];
  122. let expectedGrid = [
  123. [{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}],
  124. [{isEmpty: true}, {isEmpty: true}],
  125. ];
  126. let p = new Piece(colors);
  127. p.toGrid(grid, true);
  128. expect(grid).toStrictEqual(expectedGrid);
  129. spy1.mockRestore();
  130. spy2.mockRestore();
  131. });
  132. test('removeFromGrid', () => {
  133. let gridOld = [
  134. [
  135. {color: colors.tetrisI, isEmpty: false},
  136. {color: colors.tetrisI, isEmpty: false},
  137. {color: colors.tetrisBackground, isEmpty: true},
  138. ],
  139. ];
  140. let gridNew = [
  141. [
  142. {color: colors.tetrisBackground, isEmpty: true},
  143. {color: colors.tetrisBackground, isEmpty: true},
  144. {color: colors.tetrisBackground, isEmpty: true},
  145. ],
  146. ];
  147. let oldCoord = [
  148. {x: 0, y: 0},
  149. {x: 1, y: 0},
  150. ];
  151. let spy1 = jest
  152. .spyOn(ShapeI.prototype, 'getCellsCoordinates')
  153. .mockImplementation(() => {
  154. return oldCoord;
  155. });
  156. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
  157. return colors.tetrisI;
  158. });
  159. let p = new Piece(colors);
  160. p.removeFromGrid(gridOld);
  161. expect(gridOld).toStrictEqual(gridNew);
  162. spy1.mockRestore();
  163. spy2.mockRestore();
  164. });