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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React from 'react';
  2. import Piece from "../Piece";
  3. import ShapeI from "../Shapes/ShapeI";
  4. let colors = {
  5. tetrisI: "#000001",
  6. tetrisBackground: "#000002"
  7. };
  8. jest.mock("../Shapes/ShapeI");
  9. beforeAll(() => {
  10. jest.spyOn(Piece.prototype, 'getRandomShape')
  11. .mockImplementation((colors: Object) => {return new ShapeI(colors);});
  12. });
  13. afterAll(() => {
  14. jest.restoreAllMocks();
  15. });
  16. test('isPositionValid', () => {
  17. let x = 0;
  18. let y = 0;
  19. let spy = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates')
  20. .mockImplementation(() => {return [{x: x, y: y}];});
  21. let grid = [
  22. [{isEmpty: true}, {isEmpty: true}],
  23. [{isEmpty: true}, {isEmpty: false}],
  24. ];
  25. let size = 2;
  26. let p = new Piece(colors);
  27. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  28. x = 1; y = 0;
  29. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  30. x = 0; y = 1;
  31. expect(p.isPositionValid(grid, size, size)).toBeTrue();
  32. x = 1; y = 1;
  33. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  34. x = 2; y = 0;
  35. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  36. x = -1; y = 0;
  37. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  38. x = 0; y = 2;
  39. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  40. x = 0; y = -1;
  41. expect(p.isPositionValid(grid, size, size)).toBeFalse();
  42. spy.mockRestore();
  43. });
  44. test('tryMove', () => {
  45. let p = new Piece(colors);
  46. const callbackMock = jest.fn();
  47. let isValid = true;
  48. let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid')
  49. .mockImplementation(() => {return isValid;});
  50. let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid')
  51. .mockImplementation(() => {});
  52. let spy3 = jest.spyOn(Piece.prototype, 'toGrid')
  53. .mockImplementation(() => {});
  54. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
  55. isValid = false;
  56. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
  57. isValid = true;
  58. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
  59. expect(callbackMock).toBeCalledTimes(0);
  60. isValid = false;
  61. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
  62. expect(callbackMock).toBeCalledTimes(1);
  63. expect(spy2).toBeCalledTimes(4);
  64. expect(spy3).toBeCalledTimes(4);
  65. spy1.mockRestore();
  66. spy2.mockRestore();
  67. spy3.mockRestore();
  68. });
  69. test('tryRotate', () => {
  70. let p = new Piece(colors);
  71. let isValid = true;
  72. let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid')
  73. .mockImplementation(() => {return isValid;});
  74. let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid')
  75. .mockImplementation(() => {});
  76. let spy3 = jest.spyOn(Piece.prototype, 'toGrid')
  77. .mockImplementation(() => {});
  78. expect(p.tryRotate( null, null, null)).toBeTrue();
  79. isValid = false;
  80. expect(p.tryRotate( null, null, null)).toBeFalse();
  81. expect(spy2).toBeCalledTimes(2);
  82. expect(spy3).toBeCalledTimes(2);
  83. spy1.mockRestore();
  84. spy2.mockRestore();
  85. spy3.mockRestore();
  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. let p = new Piece(colors);
  103. p.toGrid(grid, true);
  104. expect(grid).toStrictEqual(expectedGrid);
  105. spy1.mockRestore();
  106. spy2.mockRestore();
  107. });
  108. test('removeFromGrid', () => {
  109. let gridOld = [
  110. [
  111. {color: colors.tetrisI, isEmpty: false},
  112. {color: colors.tetrisI, isEmpty: false},
  113. {color: colors.tetrisBackground, isEmpty: true},
  114. ],
  115. ];
  116. let gridNew = [
  117. [
  118. {color: colors.tetrisBackground, isEmpty: true},
  119. {color: colors.tetrisBackground, isEmpty: true},
  120. {color: colors.tetrisBackground, isEmpty: true},
  121. ],
  122. ];
  123. let oldCoord = [{x: 0, y: 0}, {x: 1, y: 0}];
  124. let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates')
  125. .mockImplementation(() => {return oldCoord;});
  126. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor')
  127. .mockImplementation(() => {return colors.tetrisI;});
  128. let p = new Piece(colors);
  129. p.removeFromGrid(gridOld);
  130. expect(gridOld).toStrictEqual(gridNew);
  131. spy1.mockRestore();
  132. spy2.mockRestore();
  133. });