dateUtils.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { describe, test, it, expect } from 'vitest'
  2. import DateUtils, { supportedLocales } from '~/services/utils/dateUtils'
  3. describe('format', () => {
  4. test('simple formatting', () => {
  5. const input = new Date(2020, 4, 12)
  6. expect(DateUtils.format(input, 'y-MM-dd')).toEqual('2020-05-12')
  7. })
  8. })
  9. describe('formatDatesAndConcat', () => {
  10. test('simple array and default sep', () => {
  11. const input = [
  12. new Date(2023, 0, 10),
  13. new Date(2023, 0, 11),
  14. new Date(2023, 0, 12),
  15. ]
  16. const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
  17. expect(result).toEqual('10/01/2023 - 11/01/2023 - 12/01/2023')
  18. })
  19. test('single date and default sep', () => {
  20. const input = new Date(2023, 0, 10)
  21. const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
  22. expect(result).toEqual('10/01/2023')
  23. })
  24. test('simple array with other format and custom sep', () => {
  25. const input = [
  26. new Date(2023, 0, 10),
  27. new Date(2023, 0, 11),
  28. new Date(2023, 0, 12),
  29. ]
  30. const result = DateUtils.formatAndConcat(input, 'yMMdd', '|')
  31. expect(result).toEqual('20230110|20230111|20230112')
  32. })
  33. test('empty array', () => {
  34. expect(DateUtils.formatAndConcat([], 'dd-MM-y')).toEqual('')
  35. })
  36. })
  37. describe('sortDate', () => {
  38. test('simple array', () => {
  39. const date1 = new Date(2023, 0, 10)
  40. const date2 = new Date(2023, 0, 14)
  41. const date3 = new Date(2023, 0, 12)
  42. const input = [date1, date2, date3]
  43. const result = DateUtils.sort(input)
  44. expect(result).toEqual([date1, date3, date2])
  45. })
  46. test('simple array reverse', () => {
  47. const date1 = new Date(2023, 0, 10)
  48. const date2 = new Date(2023, 0, 14)
  49. const date3 = new Date(2023, 0, 12)
  50. const input = [date1, date2, date3]
  51. const result = DateUtils.sort(input, true)
  52. expect(result).toEqual([date2, date3, date1])
  53. })
  54. })
  55. describe('getFnsLocale', () => {
  56. test('standard call', () => {
  57. expect(DateUtils.getFnsLocale(supportedLocales.FR).code).toEqual('fr')
  58. expect(DateUtils.getFnsLocale(supportedLocales.EN).code).toEqual('en-US')
  59. })
  60. test('unsupported locale', () => {
  61. // @ts-ignore
  62. expect(DateUtils.getFnsLocale('xx').code).toEqual('fr')
  63. })
  64. })
  65. describe('getShortFormatPattern', () => {
  66. test('standard call', () => {
  67. expect(DateUtils.getShortFormatPattern(supportedLocales.FR)).toEqual(
  68. 'dd/MM/yyyy',
  69. )
  70. expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual(
  71. 'MM/dd/yyyy',
  72. )
  73. })
  74. test('unsupported locale', () => {
  75. // @ts-ignore
  76. expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
  77. })
  78. })
  79. describe('getFormatPattern', () => {
  80. test('standard call', () => {
  81. expect(DateUtils.getFormatPattern(supportedLocales.FR)).toEqual(
  82. 'dd/MM/yyyy HH:mm',
  83. )
  84. expect(DateUtils.getFormatPattern(supportedLocales.EN)).toEqual(
  85. 'MM/dd/yyyy HH:mm',
  86. )
  87. })
  88. test('unsupported locale', () => {
  89. // @ts-ignore
  90. expect(DateUtils.getFormatPattern('xx')).toEqual('dd/MM/yyyy HH:mm')
  91. })
  92. })
  93. describe('formatIsoShortDate', () => {
  94. test('standard call', () => {
  95. expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual(
  96. '2023-01-10',
  97. )
  98. expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual(
  99. '2023-12-20',
  100. )
  101. })
  102. })