utils.spec.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import moment from 'moment';
  2. import {
  3. getDateFromMoment,
  4. getXAxisDates,
  5. } from 'sentry/views/organizationStats/usageChart/utils';
  6. const TS_START = 1531094400000; // 2018 July 9, 12am UTC
  7. const TS_END = 1531180800000; // 2018 July 10, 12am UTC
  8. describe('getDateFromMoment', () => {
  9. const start = moment.unix(TS_START / 1000);
  10. // Ensure date remains in UTC
  11. it('shows the date if interval is >= 24h', () => {
  12. expect(getDateFromMoment(start)).toBe('Jul 9');
  13. expect(getDateFromMoment(start, '7d')).toBe('Jul 9');
  14. expect(getDateFromMoment(moment('2021-10-31'))).toBe('Oct 31');
  15. });
  16. // Ensure datetime is shifted to localtime
  17. it('shows the date and time if interval is <24h', () => {
  18. expect(getDateFromMoment(start, '6h')).toBe('Jul 8 8:00 PM - 2:00 AM (-04:00)');
  19. expect(getDateFromMoment(start, '1h')).toBe('Jul 8 8:00 PM - 9:00 PM (-04:00)');
  20. expect(getDateFromMoment(start, '5m')).toBe('Jul 8 8:00 PM - 8:05 PM (-04:00)');
  21. });
  22. // Ensure datetime is shifted to localtime
  23. it('coerces date and time into UTC', () => {
  24. expect(getDateFromMoment(start, '6h', true)).toBe(
  25. 'Jul 9 12:00 AM - 6:00 AM (+00:00)'
  26. );
  27. expect(getDateFromMoment(start, '1h', true)).toBe(
  28. 'Jul 9 12:00 AM - 1:00 AM (+00:00)'
  29. );
  30. expect(getDateFromMoment(start, '5m', true)).toBe(
  31. 'Jul 9 12:00 AM - 12:05 AM (+00:00)'
  32. );
  33. });
  34. });
  35. describe('getXAxisDates', () => {
  36. // Ensure date remains in UTC
  37. it('calculates 1d intervals', () => {
  38. let dates = getXAxisDates(TS_START, TS_END);
  39. expect(dates).toEqual(['Jul 9', 'Jul 10']);
  40. dates = getXAxisDates('2021-10-29', '2021-10-31');
  41. expect(dates).toEqual(['Oct 29', 'Oct 30', 'Oct 31']);
  42. });
  43. // Datetime remains in UTC
  44. it('calculates 4h intervals in UTC', () => {
  45. const dates = getXAxisDates(TS_START, TS_END, true, '4h');
  46. expect(dates).toEqual([
  47. 'Jul 9 12:00 AM - 4:00 AM (+00:00)',
  48. 'Jul 9 4:00 AM - 8:00 AM (+00:00)',
  49. 'Jul 9 8:00 AM - 12:00 PM (+00:00)',
  50. 'Jul 9 12:00 PM - 4:00 PM (+00:00)',
  51. 'Jul 9 4:00 PM - 8:00 PM (+00:00)',
  52. 'Jul 9 8:00 PM - 12:00 AM (+00:00)',
  53. 'Jul 10 12:00 AM - 4:00 AM (+00:00)',
  54. ]);
  55. });
  56. // Datetime is shifted to localtime
  57. it('calculates 4h intervals', () => {
  58. const dates = getXAxisDates(TS_START, TS_END, false, '4h');
  59. expect(dates).toEqual([
  60. 'Jul 8 8:00 PM - 12:00 AM (-04:00)',
  61. 'Jul 9 12:00 AM - 4:00 AM (-04:00)',
  62. 'Jul 9 4:00 AM - 8:00 AM (-04:00)',
  63. 'Jul 9 8:00 AM - 12:00 PM (-04:00)',
  64. 'Jul 9 12:00 PM - 4:00 PM (-04:00)',
  65. 'Jul 9 4:00 PM - 8:00 PM (-04:00)',
  66. 'Jul 9 8:00 PM - 12:00 AM (-04:00)',
  67. ]);
  68. });
  69. // Datetime is shifted to localtime
  70. it('calculates 1h intervals', () => {
  71. const dates = getXAxisDates(TS_START, TS_END, false, '1h');
  72. expect(dates).toEqual([
  73. 'Jul 8 8:00 PM - 9:00 PM (-04:00)',
  74. 'Jul 8 9:00 PM - 10:00 PM (-04:00)',
  75. 'Jul 8 10:00 PM - 11:00 PM (-04:00)',
  76. 'Jul 8 11:00 PM - 12:00 AM (-04:00)',
  77. 'Jul 9 12:00 AM - 1:00 AM (-04:00)',
  78. 'Jul 9 1:00 AM - 2:00 AM (-04:00)',
  79. 'Jul 9 2:00 AM - 3:00 AM (-04:00)',
  80. 'Jul 9 3:00 AM - 4:00 AM (-04:00)',
  81. 'Jul 9 4:00 AM - 5:00 AM (-04:00)',
  82. 'Jul 9 5:00 AM - 6:00 AM (-04:00)',
  83. 'Jul 9 6:00 AM - 7:00 AM (-04:00)',
  84. 'Jul 9 7:00 AM - 8:00 AM (-04:00)',
  85. 'Jul 9 8:00 AM - 9:00 AM (-04:00)',
  86. 'Jul 9 9:00 AM - 10:00 AM (-04:00)',
  87. 'Jul 9 10:00 AM - 11:00 AM (-04:00)',
  88. 'Jul 9 11:00 AM - 12:00 PM (-04:00)',
  89. 'Jul 9 12:00 PM - 1:00 PM (-04:00)',
  90. 'Jul 9 1:00 PM - 2:00 PM (-04:00)',
  91. 'Jul 9 2:00 PM - 3:00 PM (-04:00)',
  92. 'Jul 9 3:00 PM - 4:00 PM (-04:00)',
  93. 'Jul 9 4:00 PM - 5:00 PM (-04:00)',
  94. 'Jul 9 5:00 PM - 6:00 PM (-04:00)',
  95. 'Jul 9 6:00 PM - 7:00 PM (-04:00)',
  96. 'Jul 9 7:00 PM - 8:00 PM (-04:00)',
  97. 'Jul 9 8:00 PM - 9:00 PM (-04:00)',
  98. ]);
  99. });
  100. it('handles invalid date strings', () => {
  101. const dates1 = getXAxisDates('', '');
  102. expect(dates1).toEqual([]);
  103. const dates2 = getXAxisDates('sentry', '2021-01-01');
  104. expect(dates2).toEqual([]);
  105. });
  106. });