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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 spy = jest.spyOn(Piece.prototype, 'isPositionValid')
  49. .mockImplementation(() => {return isValid;});
  50. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
  51. isValid = false;
  52. expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
  53. isValid = true;
  54. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
  55. expect(callbackMock).toBeCalledTimes(0);
  56. isValid = false;
  57. expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
  58. expect(callbackMock).toBeCalledTimes(1);
  59. spy.mockRestore();
  60. });
  61. test('tryRotate', () => {
  62. let p = new Piece(colors);
  63. let isValid = true;
  64. let spy = jest.spyOn(Piece.prototype, 'isPositionValid')
  65. .mockImplementation(() => {return isValid;});
  66. expect(p.tryRotate( null, null, null)).toBeTrue();
  67. isValid = false;
  68. expect(p.tryRotate( null, null, null)).toBeFalse();
  69. spy.mockRestore();
  70. });
  71. test('toGrid', () => {
  72. let x = 0;
  73. let y = 0;
  74. let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates')
  75. .mockImplementation(() => {return [{x: x, y: y}];});
  76. let spy2 = jest.spyOn(ShapeI.prototype, 'getColor')
  77. .mockImplementation(() => {return colors.tetrisI;});
  78. let grid = [
  79. [{isEmpty: true}, {isEmpty: true}],
  80. [{isEmpty: true}, {isEmpty: true}],
  81. ];
  82. let expectedGrid = [
  83. [{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}],
  84. [{isEmpty: true}, {isEmpty: true}],
  85. ];
  86. let p = new Piece(colors);
  87. p.toGrid(grid, true);
  88. expect(grid).toStrictEqual(expectedGrid);
  89. spy1.mockRestore();
  90. spy2.mockRestore();
  91. });