utils.spec.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {getFormat} from 'sentry/utils/dates';
  2. import {
  3. getConfigFromTimeRange,
  4. getStartFromTimeWindow,
  5. } from 'sentry/views/monitors/components/overviewTimeline/utils';
  6. describe('Crons Timeline Utils', function () {
  7. describe('getStartFromTimeWindow', function () {
  8. const end = new Date('2023-06-15T12:00:00Z');
  9. it('correctly computes for 1h', function () {
  10. const expectedStart = new Date('2023-06-15T11:00:00Z');
  11. const start = getStartFromTimeWindow(end, '1h');
  12. expect(start).toEqual(expectedStart);
  13. });
  14. it('correctly computes for 24h', function () {
  15. const expectedStart = new Date('2023-06-14T12:00:00Z');
  16. const start = getStartFromTimeWindow(end, '24h');
  17. expect(start).toEqual(expectedStart);
  18. });
  19. it('correctly computes for 7d', function () {
  20. const expectedStart = new Date('2023-06-08T12:00:00Z');
  21. const start = getStartFromTimeWindow(end, '7d');
  22. expect(start).toEqual(expectedStart);
  23. });
  24. it('correctly computes for 30d', function () {
  25. const expectedStart = new Date('2023-05-16T12:00:00Z');
  26. const start = getStartFromTimeWindow(end, '30d');
  27. expect(start).toEqual(expectedStart);
  28. });
  29. });
  30. describe('getConfigFromTimeRange', function () {
  31. const timelineWidth = 800;
  32. it('divides into minutes for small intervals', function () {
  33. const start = new Date('2023-06-15T11:00:00Z');
  34. const end = new Date('2023-06-15T11:05:00Z');
  35. const config = getConfigFromTimeRange(start, end, timelineWidth);
  36. expect(config).toEqual({
  37. dateLabelFormat: getFormat({timeOnly: true, seconds: true}),
  38. elapsedMinutes: 5,
  39. timeMarkerInterval: 1,
  40. dateTimeProps: {timeOnly: true},
  41. });
  42. });
  43. it('divides into minutes without showing seconds for medium intervals', function () {
  44. const start = new Date('2023-06-15T08:00:00Z');
  45. const end = new Date('2023-06-15T23:00:00Z');
  46. const config = getConfigFromTimeRange(start, end, timelineWidth);
  47. expect(config).toEqual({
  48. dateLabelFormat: getFormat({timeOnly: true}),
  49. elapsedMinutes: 900,
  50. timeMarkerInterval: 240,
  51. dateTimeProps: {timeOnly: true},
  52. });
  53. });
  54. it('divides into days for larger intervals', function () {
  55. const start = new Date('2023-05-15T11:00:00Z');
  56. const end = new Date('2023-06-15T11:00:00Z');
  57. const config = getConfigFromTimeRange(start, end, timelineWidth);
  58. expect(config).toEqual({
  59. dateLabelFormat: getFormat(),
  60. // 31 elapsed days
  61. elapsedMinutes: 31 * 24 * 60,
  62. // 4 days in between each time label
  63. timeMarkerInterval: 4 * 24 * 60,
  64. dateTimeProps: {dateOnly: true},
  65. });
  66. });
  67. });
  68. });