layoutUtils.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import type {Layout} from 'react-grid-layout';
  2. import {
  3. calculateColumnDepths,
  4. getNextAvailablePosition,
  5. } from 'sentry/views/dashboards/layoutUtils';
  6. describe('Dashboards > Utils', () => {
  7. describe('calculateColumnDepths', () => {
  8. it('returns 0s when layouts is empty', () => {
  9. const expectedColumnDepths = [0, 0, 0, 0, 0, 0];
  10. const columnDepths = calculateColumnDepths([]);
  11. expect(columnDepths).toEqual(expectedColumnDepths);
  12. });
  13. it('returns the depth of layouts at the same level', () => {
  14. const layout = [
  15. {x: 0, y: 0, w: 1, h: 3},
  16. {x: 1, y: 0, w: 2, h: 2},
  17. {x: 4, y: 0, w: 2, h: 4},
  18. ] as Layout[];
  19. const expectedColumnDepths = [3, 2, 2, 0, 4, 4];
  20. const columnDepths = calculateColumnDepths(layout);
  21. expect(columnDepths).toEqual(expectedColumnDepths);
  22. });
  23. it('ignores any "space" above and returns the depth of a lower widget', () => {
  24. const layout = [{x: 0, y: 6, w: 2, h: 3}] as Layout[];
  25. const expectedColumnDepths = [9, 9, 0, 0, 0, 0];
  26. const columnDepths = calculateColumnDepths(layout);
  27. expect(columnDepths).toEqual(expectedColumnDepths);
  28. });
  29. });
  30. describe('getNextAvailablePosition', () => {
  31. it('returns 0, 0 when there is space for a widget in the top left', () => {
  32. const columnDepths = [0, 0, 1, 1, 0, 0];
  33. const expectedPosition = {x: 0, y: 0};
  34. const expectedNextColumnDepths = [2, 2, 1, 1, 0, 0];
  35. const [position, nextColumnDepths] = getNextAvailablePosition(columnDepths, 2);
  36. expect(position).toEqual(expectedPosition);
  37. expect(nextColumnDepths).toEqual(expectedNextColumnDepths);
  38. });
  39. it('returns the middle position if there is space', () => {
  40. const columnDepths = [1, 1, 0, 0, 1, 1];
  41. const expectedPosition = {x: 2, y: 0};
  42. const expectedNextColumnDepths = [1, 1, 2, 2, 1, 1];
  43. const [position, nextColumnDepths] = getNextAvailablePosition(columnDepths, 2);
  44. expect(position).toEqual(expectedPosition);
  45. expect(nextColumnDepths).toEqual(expectedNextColumnDepths);
  46. });
  47. it('returns top right if there is space', () => {
  48. const columnDepths = [1, 1, 1, 1, 0, 0];
  49. const expectedPosition = {x: 4, y: 0};
  50. const expectedNextColumnDepths = [1, 1, 1, 1, 2, 2];
  51. const [position, nextColumnDepths] = getNextAvailablePosition(columnDepths, 2);
  52. expect(position).toEqual(expectedPosition);
  53. expect(nextColumnDepths).toEqual(expectedNextColumnDepths);
  54. });
  55. it('returns next row if there is no space', () => {
  56. const columnDepths = [1, 1, 1, 1, 1, 1];
  57. const expectedPosition = {x: 0, y: 1};
  58. const expectedNextColumnDepths = [3, 3, 1, 1, 1, 1];
  59. const [position, nextColumnDepths] = getNextAvailablePosition(columnDepths, 2);
  60. expect(position).toEqual(expectedPosition);
  61. expect(nextColumnDepths).toEqual(expectedNextColumnDepths);
  62. });
  63. it('returns the next column depth for multiple sequential calculations', () => {
  64. const initialColumnDepths = [1, 1, 1, 1, 0, 0];
  65. const expectedPosition = {x: 0, y: 1};
  66. const expectedNextColumnDepths = [3, 3, 1, 1, 2, 2];
  67. // Call it twice and pass the output of the first into the second
  68. const [_, intermediateDepths] = getNextAvailablePosition(initialColumnDepths, 2);
  69. const [position, finalColumnDepths] = getNextAvailablePosition(
  70. intermediateDepths,
  71. 2
  72. );
  73. expect(position).toEqual(expectedPosition);
  74. expect(finalColumnDepths).toEqual(expectedNextColumnDepths);
  75. });
  76. it('returns proper next column depth with different height', () => {
  77. const columnDepths = [1, 1, 1, 1, 1, 1];
  78. const expectedPosition = {x: 0, y: 1};
  79. const expectedNextColumnDepths = [5, 5, 1, 1, 1, 1];
  80. const [position, nextColumnDepths] = getNextAvailablePosition(columnDepths, 4);
  81. expect(position).toEqual(expectedPosition);
  82. expect(nextColumnDepths).toEqual(expectedNextColumnDepths);
  83. });
  84. it('does not mutate its input array', () => {
  85. const columnDepths = [1, 1, 1, 1, 1, 1];
  86. getNextAvailablePosition(columnDepths, 4);
  87. expect(columnDepths).toEqual([1, 1, 1, 1, 1, 1]);
  88. });
  89. });
  90. });