locale.spec.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { LocalesDocument } from '#shared/graphql/queries/locales.api.ts'
  6. import type { LocalesQuery } from '#shared/graphql/types.ts'
  7. import { EnumTextDirection } from '#shared/graphql/types.ts'
  8. import { useLocaleStore } from '../locale.ts'
  9. const mockQueryResult = (): LocalesQuery => {
  10. return {
  11. locales: [
  12. {
  13. locale: 'de-de',
  14. name: 'Deutsch',
  15. dir: EnumTextDirection.Ltr,
  16. alias: 'de',
  17. active: true,
  18. },
  19. {
  20. locale: 'ar',
  21. name: 'Arabic',
  22. dir: EnumTextDirection.Rtl,
  23. alias: null,
  24. active: true,
  25. },
  26. ],
  27. }
  28. }
  29. const mockClient = () => {
  30. const mockApolloClient = createMockClient()
  31. mockApolloClient.setRequestHandler(LocalesDocument, () => {
  32. return Promise.resolve({ data: mockQueryResult() })
  33. })
  34. provideApolloClient(mockApolloClient)
  35. }
  36. describe('Translations Store', () => {
  37. setActivePinia(createPinia())
  38. mockClient()
  39. const locale = useLocaleStore()
  40. it('is empty by default', () => {
  41. expect(locale.localeData).toBe(null)
  42. })
  43. it('sets rtl correctly', async () => {
  44. expect.assertions(4)
  45. await locale.setLocale('ar')
  46. expect(document.documentElement.getAttribute('dir')).toBe('rtl')
  47. expect(document.documentElement.getAttribute('lang')).toBe('ar')
  48. await locale.setLocale('de-de')
  49. expect(document.documentElement.getAttribute('dir')).toBe('ltr')
  50. expect(document.documentElement.getAttribute('lang')).toBe('de-de')
  51. })
  52. })