utils.spec.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import type {Point} from 'sentry/views/performance/transactionSummary/transactionVitals/types';
  2. import {
  3. findNearestBucketIndex,
  4. getRefRect,
  5. mapPoint,
  6. } from 'sentry/views/performance/transactionSummary/transactionVitals/utils';
  7. describe('Utils', function () {
  8. describe('findNearestBucketIndex()', function () {
  9. it('returns null for insufficient data', function () {
  10. expect(findNearestBucketIndex([], 1)).toBeNull();
  11. });
  12. it('returns null for x axis that is too big', function () {
  13. const data = [
  14. {bin: 10, count: 0},
  15. {bin: 20, count: 0},
  16. ];
  17. expect(findNearestBucketIndex(data, 30)).toBeNull();
  18. expect(findNearestBucketIndex(data, 35)).toBeNull();
  19. });
  20. it('returns -1 for x axis that is too small', function () {
  21. const data = [
  22. {bin: 10, count: 0},
  23. {bin: 20, count: 0},
  24. ];
  25. expect(findNearestBucketIndex(data, 5)).toBe(-1);
  26. expect(findNearestBucketIndex(data, 9.9999)).toBe(-1);
  27. });
  28. it('returns the correct bin for the x axis', function () {
  29. const data = [
  30. {bin: 10, count: 0},
  31. {bin: 20, count: 0},
  32. {bin: 30, count: 0},
  33. {bin: 40, count: 0},
  34. ];
  35. expect(findNearestBucketIndex(data, 10)).toBe(0);
  36. expect(findNearestBucketIndex(data, 12)).toBe(0);
  37. expect(findNearestBucketIndex(data, 18.111)).toBe(0);
  38. expect(findNearestBucketIndex(data, 19.999)).toBe(0);
  39. expect(findNearestBucketIndex(data, 20)).toBe(1);
  40. expect(findNearestBucketIndex(data, 25)).toBe(1);
  41. expect(findNearestBucketIndex(data, 28.123)).toBe(1);
  42. expect(findNearestBucketIndex(data, 29.321)).toBe(1);
  43. expect(findNearestBucketIndex(data, 30)).toBe(2);
  44. expect(findNearestBucketIndex(data, 30.421)).toBe(2);
  45. expect(findNearestBucketIndex(data, 32.521)).toBe(2);
  46. expect(findNearestBucketIndex(data, 39.921)).toBe(2);
  47. expect(findNearestBucketIndex(data, 40)).toBe(3);
  48. expect(findNearestBucketIndex(data, 40.992)).toBe(3);
  49. expect(findNearestBucketIndex(data, 49.992)).toBe(3);
  50. });
  51. });
  52. describe('getRefRect()', function () {
  53. it('returns null for insufficient data', function () {
  54. expect(getRefRect([])).toBeNull();
  55. expect(getRefRect([{bin: 10, count: 0}])).toBeNull();
  56. });
  57. it('returns default rect if insufficient variation in data', function () {
  58. const defaultRect = {point1: {x: 0, y: 0}, point2: {x: 1, y: 1}};
  59. expect(
  60. getRefRect([
  61. {bin: 0, count: 0},
  62. {bin: 10, count: 0},
  63. ])
  64. ).toEqual(defaultRect);
  65. expect(
  66. getRefRect([
  67. {bin: 0, count: 1},
  68. {bin: 10, count: 1},
  69. ])
  70. ).toEqual(defaultRect);
  71. });
  72. it('returns two unique points in the data', function () {
  73. const data = [
  74. {bin: 10, count: 9},
  75. {bin: 20, count: 9},
  76. {bin: 30, count: 9},
  77. {bin: 40, count: 9},
  78. {bin: 50, count: 3},
  79. ];
  80. expect(getRefRect(data)).toEqual({
  81. point1: {x: 0, y: 3},
  82. point2: {x: 4, y: 9},
  83. });
  84. });
  85. });
  86. describe('mapPoint()', function () {
  87. it('validates src and dest rects', function () {
  88. const bad1 = {point1: {x: 0, y: 0}, point2: {x: 0, y: 0}};
  89. const bad2 = {point1: {x: 0, y: 0}, point2: {x: 0, y: 1}};
  90. const bad3 = {point1: {x: 0, y: 0}, point2: {x: 1, y: 0}};
  91. const good = {point1: {x: 0, y: 0}, point2: {x: 1, y: 1}};
  92. expect(mapPoint({x: 0, y: 0}, bad1, good)).toBeNull();
  93. expect(mapPoint({x: 0, y: 0}, bad2, good)).toBeNull();
  94. expect(mapPoint({x: 0, y: 0}, bad3, good)).toBeNull();
  95. expect(mapPoint({x: 0, y: 0}, good, bad1)).toBeNull();
  96. expect(mapPoint({x: 0, y: 0}, good, bad2)).toBeNull();
  97. expect(mapPoint({x: 0, y: 0}, good, bad3)).toBeNull();
  98. });
  99. it('maps corners correctly', function () {
  100. const src = {point1: {x: 0, y: 0}, point2: {x: 1, y: 1}};
  101. const dest = {point1: {x: 10, y: 10}, point2: {x: 20, y: 20}};
  102. expect(mapPoint({x: 0, y: 0}, src, dest)).toEqual({x: 10, y: 10});
  103. expect(mapPoint({x: 0, y: 1}, src, dest)).toEqual({x: 10, y: 20});
  104. expect(mapPoint({x: 1, y: 0}, src, dest)).toEqual({x: 20, y: 10});
  105. expect(mapPoint({x: 1, y: 1}, src, dest)).toEqual({x: 20, y: 20});
  106. });
  107. it('maps center points correctly', function () {
  108. const expectPointsToBeClose = (point1: Point | null, point2: Point) => {
  109. expect(point1?.x).toBeCloseTo(point2.x);
  110. expect(point1?.y).toBeCloseTo(point2?.y);
  111. };
  112. const src = {point1: {x: 0, y: 0}, point2: {x: 1, y: 1}};
  113. const dest = {point1: {x: 10, y: 10}, point2: {x: 20, y: 20}};
  114. expectPointsToBeClose(mapPoint({x: 0.5, y: 0.5}, src, dest), {x: 15, y: 15});
  115. expectPointsToBeClose(mapPoint({x: 0.1, y: 0.9}, src, dest), {x: 11, y: 19});
  116. expectPointsToBeClose(mapPoint({x: 0.875, y: 0.125}, src, dest), {
  117. x: 18.75,
  118. y: 11.25,
  119. });
  120. expectPointsToBeClose(mapPoint({x: 0.111, y: 0.999}, src, dest), {
  121. x: 11.11,
  122. y: 19.99,
  123. });
  124. });
  125. });
  126. });