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.

GridManager.test.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable */
  2. import React from 'react';
  3. import GridManager from '../logic/GridManager';
  4. import ScoreManager from '../logic/ScoreManager';
  5. import Piece from '../logic/Piece';
  6. let theme = {
  7. colors: {
  8. tetrisBackground: '#000002',
  9. },
  10. };
  11. jest.mock('../logic/ScoreManager');
  12. afterAll(() => {
  13. jest.restoreAllMocks();
  14. });
  15. test('getEmptyLine', () => {
  16. let g = new GridManager(2, 2, theme);
  17. expect(g.getEmptyLine(2)).toStrictEqual([
  18. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  19. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  20. ]);
  21. expect(g.getEmptyLine(-1)).toStrictEqual([]);
  22. });
  23. test('getEmptyGrid', () => {
  24. let g = new GridManager(2, 2, theme);
  25. expect(g.getEmptyGrid(2, 2)).toStrictEqual([
  26. [
  27. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  28. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  29. ],
  30. [
  31. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  32. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  33. ],
  34. ]);
  35. expect(g.getEmptyGrid(-1, 2)).toStrictEqual([]);
  36. expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]);
  37. });
  38. test('getLinesToClear', () => {
  39. let g = new GridManager(2, 2, theme);
  40. g.getCurrentGrid()[0][0].isEmpty = false;
  41. g.getCurrentGrid()[0][1].isEmpty = false;
  42. let coord = [{x: 1, y: 0}];
  43. expect(g.getLinesToClear(coord)).toStrictEqual([0]);
  44. g.getCurrentGrid()[0][0].isEmpty = true;
  45. g.getCurrentGrid()[0][1].isEmpty = true;
  46. g.getCurrentGrid()[1][0].isEmpty = false;
  47. g.getCurrentGrid()[1][1].isEmpty = false;
  48. expect(g.getLinesToClear(coord)).toStrictEqual([]);
  49. coord = [{x: 1, y: 1}];
  50. expect(g.getLinesToClear(coord)).toStrictEqual([1]);
  51. });
  52. test('clearLines', () => {
  53. let g = new GridManager(2, 2, theme);
  54. let grid = [
  55. [
  56. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  57. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  58. ],
  59. [
  60. {color: '0', isEmpty: true, key: '0'},
  61. {color: '0', isEmpty: true, key: '1'},
  62. ],
  63. ];
  64. g.getCurrentGrid()[1][0].color = '0';
  65. g.getCurrentGrid()[1][1].color = '0';
  66. expect(g.getCurrentGrid()).toStrictEqual(grid);
  67. let scoreManager = new ScoreManager();
  68. g.clearLines([1], scoreManager);
  69. grid = [
  70. [
  71. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  72. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  73. ],
  74. [
  75. {color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
  76. {color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
  77. ],
  78. ];
  79. expect(g.getCurrentGrid()).toStrictEqual(grid);
  80. });
  81. test('freezeTetromino', () => {
  82. let g = new GridManager(2, 2, theme);
  83. let spy1 = jest
  84. .spyOn(GridManager.prototype, 'getLinesToClear')
  85. .mockImplementation(() => {});
  86. let spy2 = jest
  87. .spyOn(GridManager.prototype, 'clearLines')
  88. .mockImplementation(() => {});
  89. g.freezeTetromino(new Piece({}), null);
  90. expect(spy1).toHaveBeenCalled();
  91. expect(spy2).toHaveBeenCalled();
  92. spy1.mockRestore();
  93. spy2.mockRestore();
  94. });