date_utils.spec.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as DateUtils from 'src/services/date_utils/date_utils.js'
  2. describe('DateUtils', () => {
  3. describe('relativeTime', () => {
  4. it('returns now with low enough amount of seconds', () => {
  5. const futureTime = Date.now() + 20 * DateUtils.SECOND
  6. const pastTime = Date.now() - 20 * DateUtils.SECOND
  7. expect(DateUtils.relativeTime(futureTime, 30)).to.eql({ num: 0, key: 'time.now' })
  8. expect(DateUtils.relativeTime(pastTime, 30)).to.eql({ num: 0, key: 'time.now' })
  9. })
  10. it('rounds down for past', () => {
  11. const time = Date.now() - 1.8 * DateUtils.HOUR
  12. expect(DateUtils.relativeTime(time)).to.eql({ num: 1, key: 'time.hour' })
  13. })
  14. it('rounds up for future', () => {
  15. const time = Date.now() + 1.8 * DateUtils.HOUR
  16. expect(DateUtils.relativeTime(time)).to.eql({ num: 2, key: 'time.hours' })
  17. })
  18. it('uses plural when necessary', () => {
  19. const time = Date.now() - 3.8 * DateUtils.WEEK
  20. expect(DateUtils.relativeTime(time)).to.eql({ num: 3, key: 'time.weeks' })
  21. })
  22. it('works with date string', () => {
  23. const time = Date.now() - 4 * DateUtils.MONTH
  24. const dateString = new Date(time).toISOString()
  25. expect(DateUtils.relativeTime(dateString)).to.eql({ num: 4, key: 'time.months' })
  26. })
  27. })
  28. describe('relativeTimeShort', () => {
  29. it('returns the short version of the same relative time', () => {
  30. const time = Date.now() + 2 * DateUtils.YEAR
  31. expect(DateUtils.relativeTimeShort(time)).to.eql({ num: 2, key: 'time.years_short' })
  32. })
  33. })
  34. })