123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { beforeEach, describe, expect, vi } from 'vitest'
- import { initializePiniaStore } from '#tests/support/components/renderComponent.ts'
- import { waitUntil } from '#tests/support/utils.ts'
- import { mockLocalesQuery } from '#shared/graphql/queries/locales.mocks.ts'
- import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
- import { useLocaleStore } from '#shared/stores/locale.ts'
- import { useLocaleUpdate } from '../useLocaleUpdate.ts'
- const mockUpdateLocaleMutationHandler = () => {
- const sendMock = vi.fn()
- MutationHandler.prototype.send = sendMock
- return {
- sendMock,
- }
- }
- const mockedLocales = [
- { locale: 'de', name: 'Deutsch' },
- { locale: 'en', name: 'English' },
- ]
- describe('useLocaleUpdate', () => {
- beforeEach(() => {
- mockLocalesQuery({ locales: [{ locale: 'en', name: 'English' }] })
- initializePiniaStore()
- mockLocalesQuery({
- locales: mockedLocales,
- })
- })
- it('return translation link and label', () => {
- const { translation } = useLocaleUpdate()
- expect(translation.link).toBe('https://translations.zammad.org/')
- })
- it('isSavingLocale is initially false', () => {
- const { isSavingLocale } = useLocaleUpdate()
- expect(isSavingLocale.value).toBe(false)
- })
- it('returns correct modelCurrentLocale', () => {
- const { modelCurrentLocale } = useLocaleUpdate()
- // default locale is 'en'
- expect(modelCurrentLocale.value).toBe('en')
- })
- it('returns a list of locales', async () => {
- const { loadLocales } = useLocaleStore()
- await loadLocales()
- const { localeOptions } = useLocaleUpdate()
- const expectedOptions = [
- { value: 'de', label: 'Deutsch' },
- { value: 'en', label: 'English' },
- ]
- expect(localeOptions.value).toEqual(expectedOptions)
- })
- it('updates modelCurrentLocale correctly', async () => {
- const { sendMock } = mockUpdateLocaleMutationHandler()
- const { modelCurrentLocale, isSavingLocale } = useLocaleUpdate()
- modelCurrentLocale.value = 'de'
- expect(isSavingLocale.value).toBe(true)
- expect(sendMock).toHaveBeenCalledOnce()
- expect(sendMock).toHaveBeenCalledWith({
- locale: 'de',
- })
- await waitUntil(() => {
- return isSavingLocale.value === false
- })
- expect(modelCurrentLocale.value).toBe('de')
- expect(isSavingLocale.value).toBe(false)
- })
- })
|