utils.spec.tsx 4.9 KB

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