utils.spec.tsx 2.3 KB

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