dateUtils.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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('dd/MM/yyyy')
  68. expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy')
  69. })
  70. test('unsupported locale', () => {
  71. // @ts-ignore
  72. expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
  73. })
  74. })
  75. describe('getFormatPattern', () => {
  76. test('standard call', () => {
  77. expect(DateUtils.getFormatPattern(supportedLocales.FR)).toEqual('dd/MM/yyyy HH:mm')
  78. expect(DateUtils.getFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy HH:mm')
  79. })
  80. test('unsupported locale', () => {
  81. // @ts-ignore
  82. expect(DateUtils.getFormatPattern('xx')).toEqual('dd/MM/yyyy HH:mm')
  83. })
  84. })
  85. describe('formatIsoShortDate', () => {
  86. test('standard call', () => {
  87. expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual('2023-01-10')
  88. expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual('2023-12-20')
  89. })
  90. })