useLocaleUpdate.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { beforeEach, describe, expect, vi } from 'vitest'
  3. import { initializePiniaStore } from '#tests/support/components/renderComponent.ts'
  4. import { waitUntil } from '#tests/support/utils.ts'
  5. import { mockLocalesQuery } from '#shared/graphql/queries/locales.mocks.ts'
  6. import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
  7. import { useLocaleStore } from '#shared/stores/locale.ts'
  8. import { useLocaleUpdate } from '../useLocaleUpdate.ts'
  9. const mockUpdateLocaleMutationHandler = () => {
  10. const sendMock = vi.fn()
  11. MutationHandler.prototype.send = sendMock
  12. return {
  13. sendMock,
  14. }
  15. }
  16. const mockedLocales = [
  17. { locale: 'de', name: 'Deutsch' },
  18. { locale: 'en', name: 'English' },
  19. ]
  20. describe('useLocaleUpdate', () => {
  21. beforeEach(() => {
  22. mockLocalesQuery({ locales: [{ locale: 'en', name: 'English' }] })
  23. initializePiniaStore()
  24. mockLocalesQuery({
  25. locales: mockedLocales,
  26. })
  27. })
  28. it('return translation link and label', () => {
  29. const { translation } = useLocaleUpdate()
  30. expect(translation.link).toBe('https://translations.zammad.org/')
  31. })
  32. it('isSavingLocale is initially false', () => {
  33. const { isSavingLocale } = useLocaleUpdate()
  34. expect(isSavingLocale.value).toBe(false)
  35. })
  36. it('returns correct modelCurrentLocale', () => {
  37. const { modelCurrentLocale } = useLocaleUpdate()
  38. // default locale is 'en'
  39. expect(modelCurrentLocale.value).toBe('en')
  40. })
  41. it('returns a list of locales', async () => {
  42. const { loadLocales } = useLocaleStore()
  43. await loadLocales()
  44. const { localeOptions } = useLocaleUpdate()
  45. const expectedOptions = [
  46. { value: 'de', label: 'Deutsch' },
  47. { value: 'en', label: 'English' },
  48. ]
  49. expect(localeOptions.value).toEqual(expectedOptions)
  50. })
  51. it('updates modelCurrentLocale correctly', async () => {
  52. const { sendMock } = mockUpdateLocaleMutationHandler()
  53. const { modelCurrentLocale, isSavingLocale } = useLocaleUpdate()
  54. modelCurrentLocale.value = 'de'
  55. expect(isSavingLocale.value).toBe(true)
  56. expect(sendMock).toHaveBeenCalledOnce()
  57. expect(sendMock).toHaveBeenCalledWith({
  58. locale: 'de',
  59. })
  60. await waitUntil(() => {
  61. return isSavingLocale.value === false
  62. })
  63. expect(modelCurrentLocale.value).toBe('de')
  64. expect(isSavingLocale.value).toBe(false)
  65. })
  66. })