utils.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {DataCategoryExact} from 'sentry/types/core';
  2. import type {SpikeDetails} from 'getsentry/views/spikeProtection/types';
  3. import {getSpikeDetailsFromSeries} from 'getsentry/views/spikeProtection/utils';
  4. describe('getSpikeDetailsFromSeries', function () {
  5. function validateResults(
  6. actual: any,
  7. expectedStoredSpikes: Array<Partial<SpikeDetails>> = []
  8. ) {
  9. // required for the case where the actual is longer than the expected
  10. expect(actual).toHaveLength(expectedStoredSpikes.length);
  11. expectedStoredSpikes.forEach(res =>
  12. expect(actual).toContainEqual(expect.objectContaining(res))
  13. );
  14. }
  15. it('returns empty on error or no result', function () {
  16. expect(
  17. getSpikeDetailsFromSeries({
  18. dataCategory: DataCategoryExact.ERROR,
  19. storedSpikes: [],
  20. })
  21. ).toHaveLength(0);
  22. });
  23. it('returns valid results from stored spikes', function () {
  24. const storedSpikes = [
  25. {
  26. billingMetric: 1,
  27. endDate: new Date(2022, 0, 7, 0, 0, 0, 0).toISOString(),
  28. eventsDropped: 5,
  29. id: '1',
  30. initialThreshold: 1,
  31. organizationId: 1,
  32. projectId: 1,
  33. startDate: new Date(2022, 0, 6, 0, 0, 0, 0).toISOString(),
  34. },
  35. {
  36. billingMetric: 1,
  37. endDate: new Date(2022, 0, 7, 0, 45, 0, 0).toISOString(),
  38. eventsDropped: 4,
  39. id: '1',
  40. initialThreshold: 2,
  41. organizationId: 1,
  42. projectId: 1,
  43. startDate: new Date(2022, 0, 7, 0, 0, 0, 0).toISOString(),
  44. },
  45. ];
  46. const result = getSpikeDetailsFromSeries({
  47. dataCategory: DataCategoryExact.ERROR,
  48. storedSpikes,
  49. });
  50. const expectedStoredSpikes = [
  51. {
  52. start: storedSpikes[0]!.startDate,
  53. end: storedSpikes[0]!.endDate,
  54. dropped: 5,
  55. threshold: 1,
  56. },
  57. {
  58. start: storedSpikes[1]!.startDate,
  59. end: storedSpikes[1]!.endDate,
  60. dropped: 4,
  61. threshold: 2,
  62. },
  63. ];
  64. validateResults(result, expectedStoredSpikes);
  65. });
  66. it('returns stored spikes and overwrites calculated ones', function () {
  67. const storedSpikes = [
  68. {
  69. billingMetric: 1,
  70. endDate: new Date(2022, 0, 6, 0, 0, 0, 0).toISOString(),
  71. eventsDropped: 4,
  72. id: '1',
  73. initialThreshold: 2,
  74. organizationId: 1,
  75. projectId: 1,
  76. startDate: new Date(2022, 0, 3, 0, 45, 0, 0).toISOString(),
  77. },
  78. {
  79. billingMetric: 1,
  80. endDate: new Date(2022, 0, 8, 0, 15, 0, 0).toISOString(),
  81. eventsDropped: 4,
  82. id: '1',
  83. initialThreshold: 2,
  84. organizationId: 1,
  85. projectId: 1,
  86. startDate: new Date(2022, 0, 8, 0, 0, 0, 0).toISOString(),
  87. },
  88. ];
  89. const result = getSpikeDetailsFromSeries({
  90. dataCategory: DataCategoryExact.ERROR,
  91. storedSpikes,
  92. });
  93. const expectedStoredSpikes = [
  94. {
  95. start: storedSpikes[0]!.startDate,
  96. end: storedSpikes[0]!.endDate,
  97. dropped: 4,
  98. threshold: 2,
  99. },
  100. {
  101. start: storedSpikes[1]!.startDate,
  102. end: storedSpikes[1]!.endDate,
  103. dropped: 4,
  104. threshold: 2,
  105. },
  106. ];
  107. validateResults(result, expectedStoredSpikes);
  108. });
  109. });