dateUtils.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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('sort', () => {
  39. test('simple array', () => {
  40. const date1 = new Date(2023, 0, 10)
  41. const date2 = new Date(2023, 0, 14)
  42. const date3 = new Date(2023, 0, 12)
  43. const input = [date1, date2, date3]
  44. const result = DateUtils.sort(input)
  45. expect(result).toEqual([date1, date3, date2])
  46. })
  47. test('simple array reverse', () => {
  48. const date1 = new Date(2023, 0, 10)
  49. const date2 = new Date(2023, 0, 14)
  50. const date3 = new Date(2023, 0, 12)
  51. const input = [date1, date2, date3]
  52. const result = DateUtils.sort(input)
  53. expect(result).toEqual([date2, date3, date1])
  54. })
  55. })
  56. })
  57. describe('getFnsLocale', () => {
  58. test('standard call', () => {
  59. expect(DateUtils.getFnsLocale(supportedLocales.FR).code).toEqual('fr')
  60. expect(DateUtils.getFnsLocale(supportedLocales.EN).code).toEqual('en-US')
  61. })
  62. test('unsupported locale', () => {
  63. // @ts-ignore
  64. expect(DateUtils.getFnsLocale('xx').code).toEqual('fr')
  65. })
  66. })
  67. describe('getShortFormatPattern', () => {
  68. test('standard call', () => {
  69. expect(DateUtils.getShortFormatPattern(supportedLocales.FR)).toEqual('dd/MM/yyyy')
  70. expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy')
  71. })
  72. test('unsupported locale', () => {
  73. // @ts-ignore
  74. expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
  75. })
  76. })
  77. describe('formatIsoShortDate', () => {
  78. test('standard call', () => {
  79. expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual('2023-01-10')
  80. expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual('2023-12-20')
  81. })
  82. })