|
@@ -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')
|
|
|
|
|
+ })
|
|
|
|
|
+})
|