utils.spec.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import moment from 'moment-timezone';
  2. import {IncidentFixture} from 'sentry-fixture/incident';
  3. import {MetricRuleFixture} from 'sentry-fixture/metricRule';
  4. import {resetMockDate, setMockDate} from 'sentry-test/utils';
  5. import {buildMetricGraphDateRange} from 'sentry/views/alerts/rules/metric/details/utils';
  6. describe('buildMetricGraphDateRange', () => {
  7. const now = '2022-05-16T20:00:00';
  8. beforeEach(() => {
  9. setMockDate(new Date(`${now}Z`));
  10. });
  11. afterEach(() => {
  12. resetMockDate();
  13. });
  14. it('should use current date for an active alert', () => {
  15. const incident = IncidentFixture({
  16. dateStarted: '2022-05-16T18:55:00Z',
  17. dateClosed: null,
  18. alertRule: MetricRuleFixture({timeWindow: 1}),
  19. });
  20. const result = buildMetricGraphDateRange(incident);
  21. expect(result).toEqual({start: '2022-05-16T17:40:00', end: now});
  22. expect(moment(result.end).diff(moment(result.start), 'minutes')).toBe(140);
  23. });
  24. it('should use current date for a recently closed alert', () => {
  25. const incident = IncidentFixture({
  26. dateStarted: '2022-05-16T18:55:00Z',
  27. dateClosed: '2022-05-16T18:57:00Z',
  28. alertRule: MetricRuleFixture({timeWindow: 1}),
  29. });
  30. const result = buildMetricGraphDateRange(incident);
  31. expect(result).toEqual({start: '2022-05-16T17:40:00', end: now});
  32. expect(moment(result.end).diff(moment(result.start), 'minutes')).toBe(140);
  33. });
  34. it('should use a past date for an older alert', () => {
  35. // Incident is from over a week ago
  36. const incident = IncidentFixture({
  37. dateStarted: '2022-05-04T18:55:00Z',
  38. dateClosed: '2022-05-04T18:57:00Z',
  39. alertRule: MetricRuleFixture({timeWindow: 1}),
  40. });
  41. const result = buildMetricGraphDateRange(incident);
  42. expect(result).toEqual({end: '2022-05-04T20:12:00', start: '2022-05-04T17:40:00'});
  43. expect(moment(result.end).diff(moment(result.start), 'minutes')).toBe(152);
  44. });
  45. it('should handle large time windows', () => {
  46. const incident = IncidentFixture({
  47. dateStarted: '2022-04-20T20:28:00Z',
  48. dateClosed: null,
  49. // 1 day time window
  50. alertRule: MetricRuleFixture({timeWindow: 1440}),
  51. });
  52. const result = buildMetricGraphDateRange(incident);
  53. expect(result).toEqual({start: '2022-02-04T20:28:00', end: now});
  54. expect(moment(result.end).diff(moment(result.start), 'days')).toBe(100);
  55. });
  56. });