locale.spec.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 { LocalesDocument } from '@shared/graphql/queries/locales.api'
  6. import type { LocalesQuery } from '@shared/graphql/types'
  7. import { EnumTextDirection } from '@shared/graphql/types'
  8. import { useLocaleStore } from '../locale'
  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. })