translations.spec.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { createPinia, setActivePinia } from 'pinia'
  3. import { createMockClient } from 'mock-apollo-client'
  4. import { provideApolloClient } from '@vue/apollo-composable'
  5. import { i18n } from '@shared/i18n'
  6. import { TranslationsDocument } from '@shared/graphql/queries/translations.api'
  7. import type { TranslationsPayload } from '@shared/graphql/types'
  8. import { useTranslationsStore } from '../translations'
  9. const mockQueryResult = (
  10. locale: string,
  11. cacheKey: string | null,
  12. ): TranslationsPayload => {
  13. if (cacheKey === 'MOCKED_CACHE_KEY') {
  14. return {
  15. isCacheStillValid: true,
  16. cacheKey,
  17. translations: {},
  18. }
  19. }
  20. if (locale === 'de-de') {
  21. return {
  22. isCacheStillValid: false,
  23. cacheKey: 'MOCKED_CACHE_KEY',
  24. translations: {
  25. Login: 'Anmeldung',
  26. },
  27. }
  28. }
  29. return {
  30. isCacheStillValid: false,
  31. cacheKey: 'MOCKED_CACHE_KEY',
  32. translations: {
  33. Login: 'Login (translated)',
  34. },
  35. }
  36. }
  37. let lastQueryResult: TranslationsPayload
  38. const mockClient = () => {
  39. const mockApolloClient = createMockClient()
  40. mockApolloClient.setRequestHandler(TranslationsDocument, (variables) => {
  41. lastQueryResult = mockQueryResult(variables.locale, variables.cacheKey)
  42. return Promise.resolve({ data: { translations: lastQueryResult } })
  43. })
  44. provideApolloClient(mockApolloClient)
  45. }
  46. describe('Translations Store', () => {
  47. setActivePinia(createPinia())
  48. const translations = useTranslationsStore()
  49. mockClient()
  50. it('is empty by default', () => {
  51. expect(translations.cacheKey).toBe('CACHE_EMPTY')
  52. expect(translations.translationData).toStrictEqual({})
  53. expect(i18n.t('Login')).toBe('Login')
  54. })
  55. it('loads translations without cache', async () => {
  56. expect.assertions(4)
  57. await translations.load('de-de')
  58. expect(lastQueryResult.isCacheStillValid).toBe(false)
  59. expect(translations.cacheKey.length).toBeGreaterThan(5)
  60. expect(translations.translationData).toHaveProperty('Login', 'Anmeldung')
  61. expect(i18n.t('Login')).toBe('Anmeldung')
  62. })
  63. it('switch to en-us translations', async () => {
  64. expect.assertions(3)
  65. await translations.load('en-us')
  66. expect(lastQueryResult.isCacheStillValid).toBe(false)
  67. expect(translations.cacheKey.length).toBeGreaterThan(5)
  68. expect(translations.translationData).toHaveProperty(
  69. 'Login',
  70. 'Login (translated)',
  71. )
  72. })
  73. it('loads translations from a warm cache', async () => {
  74. expect.assertions(5)
  75. await translations.load('de-de')
  76. expect(lastQueryResult.isCacheStillValid).toBe(true)
  77. expect(lastQueryResult.translations).toStrictEqual({})
  78. expect(translations.cacheKey.length).toBeGreaterThan(5)
  79. expect(translations.translationData).toHaveProperty('Login', 'Anmeldung')
  80. expect(i18n.t('Login')).toBe('Anmeldung')
  81. })
  82. })