dates.spec.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import timezoneMock from 'timezone-mock'
  3. import { absoluteDateTime as absDT, relativeDateTime as relDT } from '../dates'
  4. import { Translator } from '../translator'
  5. describe('Dates', () => {
  6. const dateUS = 'mm/dd/yyyy'
  7. const dateTimeUS = 'mm/dd/yyyy l:MM P'
  8. it('translates absolute dates correctly', () => {
  9. // UTC is default
  10. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  11. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  12. '04/09/2021 10:11 am',
  13. )
  14. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  15. '04/09/2021 10:11 pm',
  16. )
  17. // Switch to US/Eastern
  18. timezoneMock.register('US/Eastern', global)
  19. expect(absDT('2021-04-09T10:11:12Z', dateUS)).toBe('04/09/2021')
  20. expect(absDT('2021-04-09T10:11:12Z', dateTimeUS)).toBe(
  21. '04/09/2021 6:11 am',
  22. )
  23. expect(absDT('2021-04-09T22:11:12Z', dateTimeUS)).toBe(
  24. '04/09/2021 6:11 pm',
  25. )
  26. timezoneMock.unregister(global)
  27. })
  28. it('handles UTC date strings', () => {
  29. expect(absDT('2021-04-09 10:11:12 UTC', dateUS)).toBe('04/09/2021')
  30. })
  31. it('shows relative dates correctly', () => {
  32. const b = new Date('2021-04-09T10:11:12Z')
  33. const t = new Translator()
  34. expect(relDT('2021-04-09T10:11:12Z', b, t)).toBe('just now')
  35. expect(relDT('2021-04-09T10:12:11Z', b, t)).toBe('just now')
  36. expect(relDT('2021-04-09T10:12:12Z', b, t)).toBe('in 1 minute')
  37. expect(relDT('2021-04-09T10:13:12Z', b, t)).toBe('in 2 minutes')
  38. expect(relDT('2021-04-09T11:10:12Z', b, t)).toBe('in 59 minutes')
  39. expect(relDT('2021-04-09T10:10:13Z', b, t)).toBe('just now')
  40. expect(relDT('2021-04-09T10:10:12Z', b, t)).toBe('1 minute ago')
  41. expect(relDT('2021-04-09T10:09:12Z', b, t)).toBe('2 minutes ago')
  42. expect(relDT('2021-04-09T09:12:12Z', b, t)).toBe('59 minutes ago')
  43. expect(relDT('2021-04-09T09:11:12Z', b, t)).toBe('1 hour ago')
  44. expect(relDT('2021-04-08T11:11:12Z', b, t)).toBe('23 hours ago')
  45. expect(relDT('2021-04-09T11:11:12Z', b, t)).toBe('in 1 hour')
  46. expect(relDT('2021-04-10T09:11:12Z', b, t)).toBe('in 23 hours')
  47. expect(relDT('2021-04-08T10:11:12Z', b, t)).toBe('1 day ago')
  48. expect(relDT('2021-04-03T10:11:12Z', b, t)).toBe('6 days ago')
  49. expect(relDT('2021-04-10T10:11:12Z', b, t)).toBe('in 1 day')
  50. expect(relDT('2021-04-15T10:11:12Z', b, t)).toBe('in 6 days')
  51. expect(relDT('2021-04-02T10:11:12Z', b, t)).toBe('1 week ago')
  52. expect(relDT('2021-03-12T10:11:12Z', b, t)).toBe('4 weeks ago')
  53. expect(relDT('2021-04-16T10:11:12Z', b, t)).toBe('in 1 week')
  54. expect(relDT('2021-05-07T10:11:12Z', b, t)).toBe('in 4 weeks')
  55. expect(relDT('2021-03-09T10:11:12Z', b, t)).toBe('1 month ago')
  56. expect(relDT('2020-04-19T10:11:12Z', b, t)).toBe('11 months ago')
  57. expect(relDT('2021-05-09T10:11:12Z', b, t)).toBe('in 1 month')
  58. expect(relDT('2022-03-09T10:11:12Z', b, t)).toBe('in 11 months')
  59. expect(relDT('2020-04-09T10:11:12Z', b, t)).toBe('1 year ago')
  60. expect(relDT('2000-04-09T10:11:12Z', b, t)).toBe('21 years ago')
  61. expect(relDT('2022-04-09T10:11:12Z', b, t)).toBe('in 1 year')
  62. expect(relDT('2042-04-09T10:11:12Z', b, t)).toBe('in 21 years')
  63. })
  64. })