Procházet zdrojové kódy

add i18nUtils.test.ts, complete yamlEncoder.test.ts

Olivier Massot před 3 roky
rodič
revize
64d0696854

+ 4 - 4
services/utils/i18nUtils.ts

@@ -1,6 +1,6 @@
 import {VueI18n} from "vue-i18n";
 import {EnumChoice, EnumChoices} from "~/types/interfaces";
-import {parsePhoneNumber} from "libphonenumber-js";
+import {CountryCode, parsePhoneNumber} from "libphonenumber-js";
 import ArrayUtils from "~/services/utils/arrayUtils";
 
 export default class I18nUtils {
@@ -21,7 +21,7 @@ export default class I18nUtils {
     public translateEnum(enum_: EnumChoices, sort: boolean = true): EnumChoices {
         enum_ = enum_.map(
             (item: EnumChoice) => {
-                return {value: item.value, label: this.i18n.t(item.value) as string}
+                return {value: item.value, label: this.i18n.t(item.label) as string}
             }
         )
 
@@ -32,8 +32,8 @@ export default class I18nUtils {
         return enum_
     }
 
-    public formatPhoneNumber (number: string): string {
-        const parsed = parsePhoneNumber(number)
+    public formatPhoneNumber (number: string, defaultCountry?: CountryCode): string {
+        const parsed = parsePhoneNumber(number, defaultCountry)
         return parsed ? parsed.formatNational() : ''
     }
 }

+ 7 - 0
tests/units/services/encoder/yamlDenormalizer.test.ts

@@ -3,6 +3,13 @@ import YamlEncoder from "~/services/encoder/yamlEncoder";
 import {load} from "js-yaml";
 
 
+describe('encode', () => {
+    test('simple conversion', () => {
+        const result = YamlEncoder.encode({title: {a: 1, b: 2, c: ['foo', 'bar']}})
+        expect(result).toEqual(`title:\n  a: 1\n  b: 2\n  c:\n    - foo\n    - bar\n`)
+    })
+})
+
 describe('decode', () => {
     test('with empty data', () => {
         const result = YamlEncoder.decode("")

+ 87 - 0
tests/units/services/utils/i18nUtils.test.ts

@@ -0,0 +1,87 @@
+import { describe, test, it, expect } from 'vitest'
+import {createI18n, VueI18n} from 'vue-i18n'
+import I18nUtils from "~/services/utils/i18nUtils";
+import { config } from "@vue/test-utils"
+import {EnumChoices} from "~/types/interfaces";
+
+// config.global.mocks = {
+//     $t: (tKey: string) => tKey  // just return translation key
+// };
+
+// const i18n = createI18n({})
+// config.global.plugins = [i18n]
+//
+// config.global.mocks["t"] = (msg: string) => 'test'
+
+describe('translateEnum', () => {
+    test('with simple enum', () => {
+        // @ts-ignore
+        const i18n = vi.fn() as VueI18n;
+
+        i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
+
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+
+        const input: EnumChoices = [
+            { value: 'Alpha', label: 'This is the letter A' },
+            { value: 'Beta', label: 'This is the letter B' },
+            { value: 'Epsilon', label: 'This is the letter E' },
+        ]
+
+        const expected: EnumChoices = [
+            { value: 'Alpha', label: 'C\'est la lettre A' },
+            { value: 'Beta', label: 'C\'est la lettre B' },
+            { value: 'Epsilon', label: 'C\'est la lettre E' },
+        ]
+
+        expect(i18nUtils.translateEnum(input)).toEqual(expected)
+    })
+
+    test('with simple enum and sorting enabled', () => {
+        // @ts-ignore
+        const i18n = vi.fn() as VueI18n;
+
+        i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
+
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+
+        const input: EnumChoices = [
+            { value: 'Epsilon', label: 'This is the letter E' },
+            { value: 'Alpha', label: 'This is the letter A' },
+            { value: 'Beta', label: 'This is the letter B' },
+        ]
+
+        const expected: EnumChoices = [
+            { value: 'Alpha', label: 'C\'est la lettre A' },
+            { value: 'Beta', label: 'C\'est la lettre B' },
+            { value: 'Epsilon', label: 'C\'est la lettre E' },
+        ]
+
+        expect(i18nUtils.translateEnum(input, true)).toEqual(expected)
+    })
+})
+
+describe('formatPhoneNumber', () => {
+    test('with valid international phone number', () => {
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+        expect(i18nUtils.formatPhoneNumber('+33611223344')).toEqual('06 11 22 33 44')
+    })
+    test('with valid non-formatted phone number', () => {
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+        expect(i18nUtils.formatPhoneNumber('0611223344', 'FR')).toEqual('06 11 22 33 44')
+    })
+    test('with empty string', () => {
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+        expect(() => i18nUtils.formatPhoneNumber('', 'FR')).toThrowError('NOT_A_NUMBER')
+    })
+    test('with invalid string', () => {
+        // @ts-ignore
+        const i18nUtils = new I18nUtils(i18n)
+        expect(() => i18nUtils.formatPhoneNumber('abcd', 'FR')).toThrowError('NOT_A_NUMBER')
+    })
+})