Files
BoosAPI/desktop/libs/markstream-vue/test/use-safe-i18n.test.ts
T
boos e0a5f57793 feat(desktop): embed BoosAPI Desktop client as a subproject
Vendor the rebranded BoosAPI Desktop client (Tauri + Vue) into metapi as
a monorepo subdirectory under desktop/. Source only — node_modules,
dist, Cargo target and the upstream .git history are excluded.

desktop/ keeps its own independent Cargo workspace (desktop/src-tauri
with crates/*); it is not part of the backend boosapi crate, so a
`cargo check` at the metapi root still compiles only the backend. Build
the desktop app from desktop/src-tauri per desktop/README.md.

Note: app icons are still placeholder assets pending the final BoosAPI
logo. The updater signing keys live outside the repo and are not
included.
2026-06-22 11:32:57 +08:00

116 lines
3.2 KiB
TypeScript

import { mount } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue'
import { createI18n } from 'vue-i18n'
import { useSafeI18n as useVue2SafeI18n } from '../packages/markstream-vue2/src/composables/useSafeI18n'
import { useSafeI18n } from '../src/composables/useSafeI18n'
const TestComponent = defineComponent({
setup() {
const { t } = useSafeI18n()
return { t }
},
template: '<span>{{ t("common.copy") }}</span>',
})
const originalVueI18nUse = (globalThis as any).$vueI18nUse
afterEach(() => {
vi.restoreAllMocks()
if (originalVueI18nUse === undefined)
delete (globalThis as any).$vueI18nUse
else
(globalThis as any).$vueI18nUse = originalVueI18nUse
})
describe('useSafeI18n', () => {
it('prefers the current app translator without mutating globalThis', () => {
delete (globalThis as any).$vueI18nUse
const appT = vi.fn((key: string) => key === 'common.copy' ? 'Kopieren' : key)
const wrapper = mount(TestComponent, {
global: {
config: {
globalProperties: {
$t: appT,
},
},
},
})
expect(wrapper.text()).toBe('Kopieren')
expect(appT).toHaveBeenCalledWith('common.copy')
expect((globalThis as any).$vueI18nUse).toBeUndefined()
})
it('uses the built-in fallback without triggering vue-i18n missing-key warnings', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const i18n = createI18n({
legacy: false,
globalInjection: true,
locale: 'zh',
fallbackLocale: 'en',
messages: {
zh: {},
en: {},
},
})
const wrapper = mount(TestComponent, {
global: {
plugins: [i18n],
},
})
expect(wrapper.text()).toBe('Copy')
expect(warn).not.toHaveBeenCalled()
})
it('still supports an explicitly provided global vue-i18n hook', () => {
const globalHook = vi.fn(() => ({
t: (key: string) => key === 'common.copy' ? 'Global Copy' : key,
}))
;(globalThis as any).$vueI18nUse = globalHook
const wrapper = mount(TestComponent)
expect(wrapper.text()).toBe('Global Copy')
expect(globalHook).toHaveBeenCalledTimes(1)
expect((globalThis as any).$vueI18nUse).toBe(globalHook)
})
})
describe('markstream-vue2 useSafeI18n', () => {
it('uses the built-in fallback without triggering hooked vue-i18n missing-key warnings', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const i18n = createI18n({
legacy: false,
globalInjection: true,
locale: 'zh',
fallbackLocale: 'en',
messages: {
zh: {},
en: {},
},
})
;(globalThis as any).$vueI18nUse = () => i18n.global
const { t } = useVue2SafeI18n()
expect(t('common.copy')).toBe('Copy')
expect(warn).not.toHaveBeenCalled()
})
it('still supports hooked translations', () => {
;(globalThis as any).$vueI18nUse = () => ({
t: (key: string) => key === 'common.copy' ? 'Copier' : key,
te: (key: string) => key === 'common.copy',
})
const { t } = useVue2SafeI18n()
expect(t('common.copy')).toBe('Copier')
})
})