translations.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { provideApolloClient } from '@vue/apollo-composable'
  3. import { createMockClient } from 'mock-apollo-client'
  4. import { createPinia, setActivePinia } from 'pinia'
  5. import { TranslationsDocument } from '#shared/graphql/queries/translations.api.ts'
  6. import type { TranslationsPayload } from '#shared/graphql/types.ts'
  7. import { i18n } from '#shared/i18n.ts'
  8. import { useTranslationsStore } from '../translations.ts'
  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. })