i18n.spec.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (C) 2012-2021 Zammad Foundation, https://zammad-foundation.org/
  2. import { i18n } from '@common/utils/i18n'
  3. import CommonHelloWorld from '@common/components/common/CommonHelloWorld.vue'
  4. import { shallowMount } from '@vue/test-utils'
  5. import { nextTick } from 'vue'
  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. [
  16. 'String with 3 placeholders: %s %s %s',
  17. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  18. ],
  19. ])
  20. i18n.setTranslationMap(map)
  21. expect(i18n.t('yes')).toBe('ja')
  22. })
  23. it('handles placeholders correctly', () => {
  24. // No arguments.
  25. expect(i18n.t('String with 3 placeholders: %s %s %s')).toBe(
  26. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  27. )
  28. })
  29. it('updates (reactive) translations automatically', async () => {
  30. expect.assertions(2)
  31. const msg = 'Hello world!'
  32. const wrapper = shallowMount(CommonHelloWorld, {
  33. props: { msg, show: true },
  34. global: {
  35. mocks: {
  36. i18n,
  37. },
  38. },
  39. })
  40. expect(wrapper.text()).toMatch('Hallo Welt!')
  41. i18n.setTranslationMap(new Map())
  42. await nextTick()
  43. expect(wrapper.text()).toMatch('Hello world!')
  44. })
  45. })