import deDE from '@squirrel/locales/de-DE.json'; import enUS from '@squirrel/locales/en-US.json'; import esES from '@squirrel/locales/es-ES.json'; import frFR from '@squirrel/locales/fr-FR.json'; import { SquirrelPlugin } from '@squirrel/plugin/index'; import { type App, createApp, nextTick, ref } from 'vue'; type MockI18nMessages = { [locale: string]: Record; }; // Mock i18n instance that matches the expected interface const createMockI18n = (initialLocale = 'en-US', initialMessages: MockI18nMessages = {}) => { const locale = ref(initialLocale); const messages: MockI18nMessages = { ...initialMessages }; const availableLocales = Object.keys(messages); return { global: { locale, mergeLocaleMessage: vi.fn((localeKey: string, newMessages: Record) => { if (!messages[localeKey]) { messages[localeKey] = {}; availableLocales.push(localeKey); } Object.assign(messages[localeKey], newMessages); }), getLocaleMessage: vi.fn((localeKey: string) => { return messages[localeKey] || {}; }), availableLocales, }, }; }; describe('SquirrelPlugin', () => { let app: App; let mockI18n: ReturnType; beforeEach(() => { app = createApp({}); mockI18n = createMockI18n(); }); it('should install without errors', () => { expect(() => { app.use(SquirrelPlugin, mockI18n); }).not.toThrow(); }); it('should merge en-US messages when locale is en-US', async () => { mockI18n = createMockI18n('en-US', { 'en-US': {} }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('en-US', enUS); }); it('should merge fr-CA messages when locale is fr-CA', async () => { mockI18n = createMockI18n('fr-FR', { 'fr-FR': {} }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('fr-FR', frFR); }); it('should merge de-DE messages when locale is de-DE', async () => { mockI18n = createMockI18n('de-DE', { 'de-DE': {} }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('de-DE', deDE); }); it('should merge es-ES messages when locale is es-ES', async () => { mockI18n = createMockI18n('es-ES', { 'es-ES': {} }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('es-ES', esES); }); it('should not merge messages for unsupported locales', async () => { mockI18n = createMockI18n('pt-BR', { 'pt-BR': {} }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).not.toHaveBeenCalled(); }); it('should not merge messages when squirrel key already exists', async () => { mockI18n = createMockI18n('en-US', { 'en-US': { squirrel: { someKey: 'someValue' } }, }); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).not.toHaveBeenCalled(); }); it('should react to locale changes', async () => { // Only have en-US available initially, so watcher doesn't get unwatched mockI18n = createMockI18n('en-US', { 'en-US': {}, 'fr-FR': {} }); mockI18n.global.availableLocales = ['en-US']; // Only one locale available app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('en-US', enUS); // Clear the mock to test the next call mockI18n.global.mergeLocaleMessage.mockClear(); // Add fr-CA to available locales and change to it mockI18n.global.availableLocales.push('fr-FR'); mockI18n.global.locale.value = 'fr-FR'; await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('fr-FR', frFR); }); it('should stop watching when all Squirrel locales are available', async () => { // Create mock with all locales available initially mockI18n = createMockI18n('en-US', { 'de-DE': {}, 'en-US': {}, 'es-ES': {}, 'fr-FR': {} }); mockI18n.global.availableLocales = ['de-DE', 'en-US', 'es-ES', 'fr-FR']; app.use(SquirrelPlugin, mockI18n); await nextTick(); // Should merge the current locale expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('en-US', enUS); // Clear the mock mockI18n.global.mergeLocaleMessage.mockClear(); // Change locale after unwatching should not trigger more merges mockI18n.global.locale.value = 'fr-FR'; await nextTick(); // Should not merge again since watcher was stopped expect(mockI18n.global.mergeLocaleMessage).not.toHaveBeenCalled(); }); it('should handle empty existing messages', async () => { mockI18n = createMockI18n('en-US', { 'en-US': {} }); mockI18n.global.getLocaleMessage = vi.fn().mockReturnValue({}); app.use(SquirrelPlugin, mockI18n); await nextTick(); expect(mockI18n.global.mergeLocaleMessage).toHaveBeenCalledWith('en-US', enUS); }); });