Ver código fonte

complete tests and add default locale to tests

Olivier Massot 2 anos atrás
pai
commit
6f71f2ab8b

+ 8 - 4
services/utils/dateUtils.ts

@@ -7,6 +7,8 @@ export const enum supportedLocales {
   EN = 'en'
 }
 
+const defaultLocale = 'fr'
+
 
 export default class DateUtils {
 
@@ -37,17 +39,19 @@ export default class DateUtils {
   }
 
   public static getFnsLocale(code: supportedLocales): Locale {
-    return {
+    const mapping = {
       'en': enUS,
       'fr' : fr
-    }[code]
+    }
+    return mapping[code] ?? mapping[defaultLocale]
   }
 
   public static getShortFormatPattern(code: supportedLocales): string {
-    return {
+    const mapping = {
       'en': 'MM/dd/yyyy',
       'fr': 'dd/MM/yyyy'
-    }[code]
+    }
+    return mapping[code] ?? mapping[defaultLocale]
   }
 
   public static formatIsoShortDate(date: Date): string {

+ 57 - 2
tests/units/services/utils/dateUtils.test.ts

@@ -1,5 +1,5 @@
 import { describe, test, it, expect } from 'vitest'
-import DateUtils from "~/services/utils/dateUtils";
+import DateUtils, {supportedLocales} from "~/services/utils/dateUtils";
 
 describe('format', () => {
     test('simple formatting', () => {
@@ -49,6 +49,61 @@ describe('formatDatesAndConcat', () => {
 
 describe('sortDate', () => {
     test('sort', () => {
-        // TODO: should assert that ArrayUtils.sort is called
+        test('simple array', () => {
+            const date1 = new Date(2023, 0, 10)
+            const date2 = new Date(2023, 0, 14)
+            const date3 = new Date(2023, 0, 12)
+
+            const input = [date1, date2, date3]
+
+            const result = DateUtils.sort(input)
+
+            expect(result).toEqual([date1, date3, date2])
+        })
+
+        test('simple array reverse', () => {
+            const date1 = new Date(2023, 0, 10)
+            const date2 = new Date(2023, 0, 14)
+            const date3 = new Date(2023, 0, 12)
+
+            const input = [date1, date2, date3]
+
+            const result = DateUtils.sort(input)
+
+            expect(result).toEqual([date2, date3, date1])
+        })
+    })
+})
+
+describe('getFnsLocale', () => {
+    test('standard call', () => {
+        expect(DateUtils.getFnsLocale(supportedLocales.FR).code).toEqual('fr')
+        expect(DateUtils.getFnsLocale(supportedLocales.EN).code).toEqual('en-US')
+    })
+
+    test('unsupported locale', () => {
+        // @ts-ignore
+        expect(DateUtils.getFnsLocale('xx').code).toEqual('fr')
+    })
+})
+
+describe('getShortFormatPattern', () => {
+    test('standard call', () => {
+        expect(DateUtils.getShortFormatPattern(supportedLocales.FR)).toEqual('dd/MM/yyyy')
+        expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy')
+    })
+
+    test('unsupported locale', () => {
+        // @ts-ignore
+        expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
     })
 })
+
+describe('formatIsoShortDate', () => {
+    test('standard call', () => {
+        expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual('2023-01-10')
+        expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual('2023-12-20')
+    })
+})
+
+