Bläddra i källkod

add dateUtils.test.ts and revise DateUtils class

Olivier Massot 2 år sedan
förälder
incheckning
4f09f18db2

+ 2 - 2
components/Ui/Input/DatePicker.vue

@@ -136,13 +136,13 @@ const datesFormatted: ComputedRef<string|null> = computed(() => {
   if (props.range && datesParsed.value && datesParsed.value.length < 2) {
     return null
   }
-  return datesParsed.value ? DateUtils.formatDatesAndConcat(datesParsed.value, props.format) :  null
+  return datesParsed.value ? DateUtils.formatAndConcat(datesParsed.value, props.format) :  null
 })
 
 const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
   if (newValue === oldValue) { return }
   if (props.range && newValue && newValue.length < 2) { return }
-  updateViolationState(Array.isArray(newValue) ? DateUtils.sortDate(newValue) : newValue)
+  updateViolationState(Array.isArray(newValue) ? DateUtils.sort(newValue) : newValue)
 })
 
 onUnmounted(() => {

+ 15 - 10
services/utils/dateUtils.ts

@@ -2,7 +2,7 @@ import { format } from 'date-fns';
 
 export default class DateUtils {
 
-  public static format(date: any, fmt: string): string {
+  public static format(date: Date, fmt: string): string {
     return format(date, fmt)
   }
 
@@ -13,21 +13,26 @@ export default class DateUtils {
    * @param fmt
    * @param sep
    */
-  public static formatDatesAndConcat (dates: any, fmt: string, sep: string = ' - '): string {
+  public static formatAndConcat (dates: Date | Array<Date>, fmt: string, sep: string = ' - '): string {
     dates = Array.isArray(dates) ? dates : [dates]
-
-    const dFormat: Array<string> = Array.isArray(dates) ? dates : [dates]
-    for (const date of dates) {
-      dFormat.push(this.format(date, fmt))
-    }
-    return dFormat.join(sep)
+    return dates.map((d) => this.format(d, fmt)).join(sep)
   }
 
   /**
    * Trie les dates par ordre chronologique
+   *
    * @param dates
+   * @param reverse
    */
-  public static sortDate (dates: Array<string>): Array<string> {
-    return dates.sort()
+  public static sort(dates: Array<Date>, reverse: boolean = false): Array<Date> {
+    return dates.sort((a, b) => {
+      if (a < b) {
+        return reverse ? 1 : -1;
+      }
+      if (a > b) {
+        return reverse ? -1 : 1;
+      }
+      return 0;
+    })
   }
 }

+ 82 - 0
tests/units/services/utils/dateUtils.test.ts

@@ -0,0 +1,82 @@
+import { describe, test, it, expect } from 'vitest'
+import DateUtils from "~/services/utils/dateUtils";
+
+describe('format', () => {
+    test('simple formatting', () => {
+      const input = new Date(2020, 4, 12)
+
+      expect(DateUtils.format(input, 'y-MM-dd')).toEqual('2020-05-12')
+    })
+})
+
+describe('formatDatesAndConcat', () => {
+    test('simple array and default sep', () => {
+      const input = [
+          new Date(2023, 0, 10),
+          new Date(2023, 0, 11),
+          new Date(2023, 0, 12)
+      ]
+
+      const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
+
+      expect(result).toEqual('10/01/2023 - 11/01/2023 - 12/01/2023')
+    })
+
+    test('single date and default sep', () => {
+      const input = new Date(2023, 0, 10)
+
+      const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
+
+      expect(result).toEqual('10/01/2023')
+    })
+
+    test('simple array with other format and custom sep', () => {
+      const input = [
+          new Date(2023, 0, 10),
+          new Date(2023, 0, 11),
+          new Date(2023, 0, 12)
+      ]
+
+      const result = DateUtils.formatAndConcat(input, 'yMMdd', '|')
+
+      expect(result).toEqual('20230110|20230111|20230112')
+    })
+
+    test ('empty array', () => {
+        expect(DateUtils.formatAndConcat([], 'dd-MM-y')).toEqual('')
+    })
+})
+
+describe('sortDate', () => {
+    test('simple sort', () => {
+        const input = [
+            new Date(2023, 0, 12),
+            new Date(2023, 0, 10),
+            new Date(2023, 0, 11)
+        ]
+
+        const expected = [
+            new Date(2023, 0, 10),
+            new Date(2023, 0, 11),
+            new Date(2023, 0, 12)
+        ]
+
+        expect(DateUtils.sort(input)).toEqual(expected)
+    })
+
+    test('reverse sort', () => {
+        const input = [
+            new Date(2023, 0, 12),
+            new Date(2023, 0, 10),
+            new Date(2023, 0, 11)
+        ]
+
+        const expected = [
+            new Date(2023, 0, 12),
+            new Date(2023, 0, 11),
+            new Date(2023, 0, 10)
+        ]
+
+        expect(DateUtils.sort(input, true)).toEqual(expected)
+    })
+})