anomalyChart.spec.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {getAnomalyMarkerSeries} from 'sentry/views/alerts/rules/metric/utils/anomalyChart';
  2. import {type Anomaly, AnomalyType} from 'sentry/views/alerts/types';
  3. const anomaly: Anomaly['anomaly'] = {anomaly_type: AnomalyType.NONE, anomaly_score: 0};
  4. const anomaly_high: Anomaly['anomaly'] = {
  5. anomaly_type: AnomalyType.HIGH_CONFIDENCE,
  6. anomaly_score: 2,
  7. };
  8. const anomaly_low: Anomaly['anomaly'] = {
  9. anomaly_type: AnomalyType.LOW_CONFIDENCE,
  10. anomaly_score: 1,
  11. };
  12. describe('anomalyChart', () => {
  13. it('should return an empty array for empty anomalies', () => {
  14. expect(getAnomalyMarkerSeries([])).toEqual([]);
  15. });
  16. it('should not create anomaly values', () => {
  17. const input: Anomaly[] = [
  18. {
  19. anomaly,
  20. timestamp: d(-3),
  21. value: 1,
  22. },
  23. {
  24. anomaly,
  25. timestamp: d(-2),
  26. value: 1,
  27. },
  28. ];
  29. expect(getAnomalyMarkerSeries(input)).toHaveLength(1);
  30. });
  31. it('should create two anomaly areas', () => {
  32. const input: Anomaly[] = [
  33. {
  34. anomaly: anomaly_high,
  35. timestamp: d(-3),
  36. value: 1,
  37. },
  38. {
  39. anomaly: anomaly_high,
  40. timestamp: d(-2),
  41. value: 1,
  42. },
  43. {
  44. anomaly,
  45. timestamp: d(-1),
  46. value: 0,
  47. },
  48. {
  49. anomaly,
  50. timestamp: d(-1),
  51. value: 0,
  52. },
  53. ];
  54. expect(getAnomalyMarkerSeries(input)).toHaveLength(2);
  55. });
  56. it('should create three anomaly areas', () => {
  57. const input: Anomaly[] = [
  58. {
  59. anomaly: anomaly_high,
  60. timestamp: d(-3),
  61. value: 1,
  62. },
  63. {
  64. anomaly: anomaly_high,
  65. timestamp: d(-2),
  66. value: 1,
  67. },
  68. {
  69. anomaly,
  70. timestamp: d(-1),
  71. value: 0,
  72. },
  73. {
  74. anomaly,
  75. timestamp: d(-1),
  76. value: 0,
  77. },
  78. {
  79. anomaly: anomaly_low,
  80. timestamp: d(1),
  81. value: 2,
  82. },
  83. {
  84. anomaly: anomaly_low,
  85. timestamp: d(2),
  86. value: 2,
  87. },
  88. ];
  89. expect(getAnomalyMarkerSeries(input)).toHaveLength(3);
  90. });
  91. });
  92. function d(offset: number) {
  93. const value = new Date();
  94. value.setHours(12);
  95. value.setDate(value.getDate() + offset);
  96. return value.valueOf();
  97. }