i18n.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { nextTick } from 'vue'
  3. import { renderComponent } from '@tests/support/components'
  4. import { i18n } from '@shared/i18n'
  5. import CommonSectionMenu from '@mobile/components/CommonSectionMenu/CommonSectionMenu.vue' // TODO: switch to shared component example
  6. describe('i18n', () => {
  7. it('starts with empty state', () => {
  8. expect(i18n.t('unknown string')).toBe('unknown string')
  9. expect(i18n.t('yes')).toBe('yes')
  10. })
  11. it('translates known strings', () => {
  12. const map = new Map([
  13. ['yes', 'ja'],
  14. ['Hello world!', 'Hallo Welt!'],
  15. ['The second component.', 'Die zweite Komponente.'],
  16. [
  17. 'String with 3 placeholders: %s %s %s',
  18. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  19. ],
  20. ['FORMAT_DATE', 'dd/mm/yyyy'],
  21. ['FORMAT_DATETIME', 'dd/mm/yyyy HH:MM:SS'],
  22. ])
  23. i18n.setTranslationMap(map)
  24. expect(i18n.t('yes')).toBe('ja')
  25. })
  26. it('handles placeholders correctly', () => {
  27. // No arguments.
  28. expect(i18n.t('String with 3 placeholders: %s %s %s')).toBe(
  29. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  30. )
  31. })
  32. it('translates dates', () => {
  33. expect(i18n.date('2021-04-09T10:11:12Z')).toBe('09/04/2021')
  34. expect(i18n.dateTime('2021-04-09T10:11:12Z')).toBe('09/04/2021 10:11:12')
  35. expect(i18n.relativeDateTime(new Date().toISOString())).toBe('just now')
  36. })
  37. it('updates (reactive) translations automatically', async () => {
  38. const { container } = renderComponent(CommonSectionMenu, {
  39. props: {
  40. headerLabel: 'Hello world!',
  41. },
  42. slots: {
  43. default: 'Example Content',
  44. },
  45. global: {
  46. mocks: {
  47. i18n,
  48. },
  49. },
  50. })
  51. expect(container).toHaveTextContent('Hallo Welt!')
  52. i18n.setTranslationMap(new Map())
  53. await nextTick()
  54. expect(container).toHaveTextContent('Hello world!')
  55. })
  56. })