dateUtils.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, test, it, expect } from 'vitest'
  2. import DateUtils 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 sort', () => {
  39. const input = [
  40. new Date(2023, 0, 12),
  41. new Date(2023, 0, 10),
  42. new Date(2023, 0, 11)
  43. ]
  44. const expected = [
  45. new Date(2023, 0, 10),
  46. new Date(2023, 0, 11),
  47. new Date(2023, 0, 12)
  48. ]
  49. expect(DateUtils.sort(input)).toEqual(expected)
  50. })
  51. test('reverse sort', () => {
  52. const input = [
  53. new Date(2023, 0, 12),
  54. new Date(2023, 0, 10),
  55. new Date(2023, 0, 11)
  56. ]
  57. const expected = [
  58. new Date(2023, 0, 12),
  59. new Date(2023, 0, 11),
  60. new Date(2023, 0, 10)
  61. ]
  62. expect(DateUtils.sort(input, true)).toEqual(expected)
  63. })
  64. })