i18nUtils.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, test, it, expect } from 'vitest'
  2. import {createI18n, VueI18n} from 'vue-i18n'
  3. import I18nUtils from "~/services/utils/i18nUtils";
  4. import {EnumChoices} from "~/types/interfaces";
  5. describe('translateEnum', () => {
  6. let i18nUtils: I18nUtils
  7. beforeEach(() => {
  8. // @ts-ignore
  9. const i18n = vi.fn() as VueI18n;
  10. i18nUtils = new I18nUtils(i18n)
  11. i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
  12. })
  13. test('with simple enum', () => {
  14. const input: EnumChoices = [
  15. { value: 'Alpha', label: 'This is the letter A' },
  16. { value: 'Beta', label: 'This is the letter B' },
  17. { value: 'Epsilon', label: 'This is the letter E' },
  18. ]
  19. const expected: EnumChoices = [
  20. { value: 'Alpha', label: 'C\'est la lettre A' },
  21. { value: 'Beta', label: 'C\'est la lettre B' },
  22. { value: 'Epsilon', label: 'C\'est la lettre E' },
  23. ]
  24. expect(i18nUtils.translateEnum(input)).toEqual(expected)
  25. })
  26. test('with simple enum and sorting enabled', () => {
  27. const input: EnumChoices = [
  28. { value: 'Epsilon', label: 'This is the letter E' },
  29. { value: 'Alpha', label: 'This is the letter A' },
  30. { value: 'Beta', label: 'This is the letter B' },
  31. ]
  32. const expected: EnumChoices = [
  33. { value: 'Alpha', label: 'C\'est la lettre A' },
  34. { value: 'Beta', label: 'C\'est la lettre B' },
  35. { value: 'Epsilon', label: 'C\'est la lettre E' },
  36. ]
  37. expect(i18nUtils.translateEnum(input, true)).toEqual(expected)
  38. })
  39. })
  40. describe('formatPhoneNumber', () => {
  41. let i18nUtils: I18nUtils
  42. beforeEach(() => {
  43. // @ts-ignore
  44. const i18n = vi.fn() as VueI18n;
  45. i18nUtils = new I18nUtils(i18n)
  46. })
  47. test('with valid international phone number', () => {
  48. expect(i18nUtils.formatPhoneNumber('+33611223344')).toEqual('06 11 22 33 44')
  49. })
  50. test('with valid non-formatted phone number', () => {
  51. expect(i18nUtils.formatPhoneNumber('0611223344', 'FR')).toEqual('06 11 22 33 44')
  52. })
  53. test('with empty string', () => {
  54. expect(() => i18nUtils.formatPhoneNumber('', 'FR')).toThrowError('NOT_A_NUMBER')
  55. })
  56. test('with invalid string', () => {
  57. expect(() => i18nUtils.formatPhoneNumber('abcd', 'FR')).toThrowError('NOT_A_NUMBER')
  58. })
  59. })