dates.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import ConfigStore from 'sentry/stores/configStore';
  2. import {
  3. getTimeFormat,
  4. intervalToMilliseconds,
  5. parsePeriodToHours,
  6. setDateToTime,
  7. shouldUse24Hours,
  8. } from 'sentry/utils/dates';
  9. describe('utils.dates', function () {
  10. describe('setDateToTime', function () {
  11. it('can set new time for current date', function () {
  12. const date = new Date();
  13. const newDate = setDateToTime(date, '11:11');
  14. expect(newDate).toEqual(new Date(1508238680000));
  15. });
  16. it('can set new time (including seconds) for current date', function () {
  17. const date = new Date();
  18. const newDate = setDateToTime(date, '11:11:11');
  19. expect(newDate).toEqual(new Date(1508238671000));
  20. });
  21. it('can set new time in local for current date', function () {
  22. const date = new Date();
  23. const newDate = setDateToTime(date, '11:11:11', {local: true});
  24. expect(newDate).toEqual(new Date(1508166671000));
  25. });
  26. });
  27. describe('intervalToMilliseconds()', function () {
  28. it('can convert standard formats', function () {
  29. expect(intervalToMilliseconds('24h')).toBe(86400000);
  30. expect(intervalToMilliseconds('30m')).toBe(1800000);
  31. expect(intervalToMilliseconds('15m')).toBe(900000);
  32. expect(intervalToMilliseconds('5m')).toBe(300000);
  33. expect(intervalToMilliseconds('1m')).toBe(60000);
  34. });
  35. it('can convert arbitrary formats', function () {
  36. expect(intervalToMilliseconds('30d')).toBe(2592000000);
  37. expect(intervalToMilliseconds('7d')).toBe(604800000);
  38. expect(intervalToMilliseconds('1d')).toBe(86400000);
  39. expect(intervalToMilliseconds('1h')).toBe(3600000);
  40. expect(intervalToMilliseconds('2m')).toBe(120000);
  41. });
  42. });
  43. describe('parsePeriodToHours()', function () {
  44. it('can convert standard formats', function () {
  45. expect(parsePeriodToHours('30s').toFixed(4)).toBe('0.0083');
  46. expect(parsePeriodToHours('1m').toFixed(4)).toBe('0.0167');
  47. expect(parsePeriodToHours('1h')).toBe(1);
  48. expect(parsePeriodToHours('24h')).toBe(24);
  49. expect(parsePeriodToHours('1d')).toBe(24);
  50. expect(parsePeriodToHours('2w')).toBe(336);
  51. });
  52. it('handle invalid statsPeriod', function () {
  53. expect(parsePeriodToHours('24')).toBe(24 / 3600);
  54. expect(parsePeriodToHours('')).toBe(-1);
  55. expect(parsePeriodToHours('24x')).toBe(-1);
  56. });
  57. });
  58. describe('user clock preferences', function () {
  59. afterEach(function () {
  60. ConfigStore.set('user', TestStubs.User({}));
  61. });
  62. describe('shouldUse24Hours()', function () {
  63. it('returns false if user preference is 12 hour clock', function () {
  64. const user = TestStubs.User();
  65. user.options.clock24Hours = false;
  66. ConfigStore.set('user', user);
  67. expect(shouldUse24Hours()).toBe(false);
  68. });
  69. it('returns true if user preference is 24 hour clock', function () {
  70. const user = TestStubs.User();
  71. user.options.clock24Hours = true;
  72. ConfigStore.set('user', user);
  73. expect(shouldUse24Hours()).toBe(true);
  74. });
  75. });
  76. describe('getTimeFormat()', function () {
  77. it('does not use AM/PM if shouldUse24Hours is true', function () {
  78. const user = TestStubs.User();
  79. user.options.clock24Hours = true;
  80. ConfigStore.set('user', user);
  81. expect(getTimeFormat()).toBe('HH:mm');
  82. });
  83. it('uses AM/PM if shouldUse24Hours is false', function () {
  84. const user = TestStubs.User();
  85. user.options.clock24Hours = false;
  86. ConfigStore.set('user', user);
  87. expect(getTimeFormat()).toBe('LT');
  88. });
  89. });
  90. });
  91. });