// @vitest-environment happy-dom import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, type MockInstance, vi} from 'vitest'; import {mount} from '@vue/test-utils'; import {useQueryStore} from '../../stores'; import {globalLogger} from '../../services'; import {doComponentTestSetup, doComponentTestTeardown, mockDefaultTranslations} from '../../__tests__'; import {useLanguage} from './useLanguage'; let warnSpy: MockInstance; const commonSetup = () => { useQueryStore().registerContextQueries(); return useLanguage(); }; describe('useLanguage', () => { beforeAll(() => { mockDefaultTranslations.mockReturnValue({ my_translation: 'My translation', my_translation_with_replacement: 'My translation with replacement: {platform.backofficeUrl}', translation_with_custom_replacer: '{platform.name} said {word}', }); }); beforeEach(() => { doComponentTestSetup(); warnSpy = vi.spyOn(globalLogger, 'warn'); }); afterEach(() => { warnSpy.mockClear(); doComponentTestTeardown(); }); afterAll(() => { mockDefaultTranslations.mockReset(); }); describe('translate', () => { it('translates strings', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ translate("my_translation") }}
', }); expect(wrapper.find('div').element.innerText).toBe('My translation'); expect(warnSpy).not.toHaveBeenCalled(); }); it('warns about missing translations', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ translate("missing") }}
', }); expect(wrapper.find('div').element.innerText).toBe('missing'); expect(warnSpy).toHaveBeenCalledWith('Missing translation: missing'); }); it('translates strings with replacements', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ translate("my_translation_with_replacement") }}
', }); expect(wrapper.find('div').element.innerText).toBe( 'My translation with replacement: https://backoffice.test.myparcel.nl', ); expect(warnSpy).not.toHaveBeenCalled(); }); it('handles non-translatables', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ translate({text: "plain text"}) }}
', }); expect(wrapper.find('div').element.innerText).toBe('plain text'); }); it('translates translatables', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ translate({key: "translation_with_custom_replacer", args: {word: "wow" } }) }}
', }); expect(wrapper.find('div').element.innerText).toBe('test said wow'); }); }); describe('has', () => { it('returns true for existing translations', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ has("my_translation") }}
', }); expect(wrapper.find('div').element.innerText).toBe('true'); expect(warnSpy).not.toHaveBeenCalled(); }); it('returns false for missing translations', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ has("missing") }}
', }); expect(wrapper.find('div').element.innerText).toBe('false'); expect(warnSpy).not.toHaveBeenCalled(); }); it('returns true for non-translatables', () => { const wrapper = mount({ setup: commonSetup, template: '
{{ has({text: "plain text"}) }}
', }); expect(wrapper.find('div').element.innerText).toBe('true'); }); }); });