formatDate.test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { expect } from 'chai'
  2. import { formatLocalDate } from '@/shared/utils/formatDate'
  3. import moment from 'moment'
  4. describe('formatLocalDate', function () {
  5. it('should format a date string with local timezone offset', function () {
  6. const date = '2022-12-21T10:29:21.881Z'
  7. const formatted = formatLocalDate(date)
  8. // Should match format: D MMM YYYY, HH:mm:ss Z
  9. // Tests run with TZ=GMT so timezone offset should be +00:00
  10. expect(formatted).to.equal('21 Dec 2022, 10:29:21 +00:00')
  11. })
  12. it('should format a Date object with local timezone offset', function () {
  13. const date = new Date('2023-01-03T14:20:13.000Z')
  14. const formatted = formatLocalDate(date)
  15. // Tests run with TZ=GMT so timezone offset should be +00:00
  16. expect(formatted).to.equal('3 Jan 2023, 14:20:13 +00:00')
  17. })
  18. it('should format a moment object with local timezone offset', function () {
  19. const date = moment('2022-03-22T11:36:09.000Z')
  20. const formatted = formatLocalDate(date)
  21. // Tests run with TZ=GMT so timezone offset should be +00:00
  22. expect(formatted).to.equal('22 Mar 2022, 11:36:09 +00:00')
  23. })
  24. it('should return "N/A" for null', function () {
  25. const formatted = formatLocalDate(null)
  26. expect(formatted).to.equal('N/A')
  27. })
  28. it('should return "N/A" for undefined', function () {
  29. const formatted = formatLocalDate(undefined)
  30. expect(formatted).to.equal('N/A')
  31. })
  32. it('should return "N/A" for empty string', function () {
  33. const formatted = formatLocalDate('')
  34. expect(formatted).to.equal('N/A')
  35. })
  36. it('should format consistent output for the same date', function () {
  37. const date = '2022-08-05T09:29:17.000Z'
  38. const formatted1 = formatLocalDate(date)
  39. const formatted2 = formatLocalDate(date)
  40. expect(formatted1).to.equal(formatted2)
  41. })
  42. it('should include timezone offset in output', function () {
  43. const date = '2022-12-21T10:29:21.881Z'
  44. const formatted = formatLocalDate(date)
  45. // Tests run with TZ=GMT so should always be +00:00
  46. expect(formatted).to.match(/\+00:00$/)
  47. })
  48. it('should format date with correct month abbreviation', function () {
  49. const date = '2022-12-21T10:29:21.881Z'
  50. const formatted = formatLocalDate(date)
  51. // December should be abbreviated as Dec
  52. expect(formatted).to.include('Dec')
  53. })
  54. it('should format time with seconds', function () {
  55. const date = '2022-12-21T10:29:21.881Z'
  56. const formatted = formatLocalDate(date)
  57. // Should include seconds in HH:mm:ss format
  58. expect(formatted).to.match(/\d{2}:\d{2}:\d{2}/)
  59. })
  60. it('should handle dates from different years', function () {
  61. const date1 = '2021-02-18T13:24:54.000Z'
  62. const date2 = '2023-04-04T15:31:26.000Z'
  63. const formatted1 = formatLocalDate(date1)
  64. const formatted2 = formatLocalDate(date2)
  65. expect(formatted1).to.include('2021')
  66. expect(formatted2).to.include('2023')
  67. })
  68. })