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

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