utils.spec.tsx 2.1 KB

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