i18nUtils.test.ts 2.3 KB

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