|
| 1 | +/** |
| 2 | + * loadLanguage Tests |
| 3 | + * |
| 4 | + * Verifies that the loadLanguage helper correctly unwraps the |
| 5 | + * @objectstack/spec REST API envelope (`{ data: { locale, translations } }`) |
| 6 | + * while remaining backward-compatible with flat mock/dev responses. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 10 | + |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | +// Re-implement the loadLanguage logic under test (mirrors apps/console/src/main.tsx) |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +async function loadLanguage(lang: string): Promise<Record<string, unknown>> { |
| 15 | + try { |
| 16 | + const res = await fetch(`/api/v1/i18n/translations/${lang}`); |
| 17 | + if (!res.ok) { |
| 18 | + console.warn(`[i18n] Failed to load translations for '${lang}': HTTP ${res.status}`); |
| 19 | + return {}; |
| 20 | + } |
| 21 | + const json = await res.json(); |
| 22 | + // Unwrap the spec REST API envelope when present |
| 23 | + if (json && typeof json === 'object' && json.data && json.data.translations && typeof json.data.translations === 'object') { |
| 24 | + return json.data.translations as Record<string, unknown>; |
| 25 | + } |
| 26 | + // Fallback: mock server / local dev returns flat translation objects |
| 27 | + return json; |
| 28 | + } catch (err) { |
| 29 | + console.warn(`[i18n] Failed to load translations for '${lang}':`, err); |
| 30 | + return {}; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | +// Tests |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +describe('loadLanguage', () => { |
| 38 | + const fetchSpy = vi.fn<(...args: any[]) => Promise<Response>>(); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + vi.stubGlobal('fetch', fetchSpy); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + vi.restoreAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('unwraps spec REST API envelope { data: { locale, translations } }', async () => { |
| 49 | + const translations = { crm: { contact: '联系人', account: '客户' } }; |
| 50 | + fetchSpy.mockResolvedValue({ |
| 51 | + ok: true, |
| 52 | + json: async () => ({ data: { locale: 'zh-CN', translations } }), |
| 53 | + } as Response); |
| 54 | + |
| 55 | + const result = await loadLanguage('zh-CN'); |
| 56 | + expect(result).toEqual(translations); |
| 57 | + expect(fetchSpy).toHaveBeenCalledWith('/api/v1/i18n/translations/zh-CN'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns flat JSON when mock/dev server returns without envelope', async () => { |
| 61 | + const flat = { crm: { contact: 'Contact', account: 'Account' } }; |
| 62 | + fetchSpy.mockResolvedValue({ |
| 63 | + ok: true, |
| 64 | + json: async () => flat, |
| 65 | + } as Response); |
| 66 | + |
| 67 | + const result = await loadLanguage('en'); |
| 68 | + expect(result).toEqual(flat); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns empty object on HTTP error', async () => { |
| 72 | + fetchSpy.mockResolvedValue({ ok: false, status: 404 } as Response); |
| 73 | + |
| 74 | + const result = await loadLanguage('xx'); |
| 75 | + expect(result).toEqual({}); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns empty object on network failure', async () => { |
| 79 | + fetchSpy.mockRejectedValue(new TypeError('Failed to fetch')); |
| 80 | + |
| 81 | + const result = await loadLanguage('en'); |
| 82 | + expect(result).toEqual({}); |
| 83 | + }); |
| 84 | + |
| 85 | + it('handles envelope with null translations gracefully (fallback)', async () => { |
| 86 | + fetchSpy.mockResolvedValue({ |
| 87 | + ok: true, |
| 88 | + json: async () => ({ data: { locale: 'en', translations: null } }), |
| 89 | + } as Response); |
| 90 | + |
| 91 | + const result = await loadLanguage('en'); |
| 92 | + // translations is null (not an object), so fallback to full JSON |
| 93 | + expect(result).toEqual({ data: { locale: 'en', translations: null } }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('handles envelope with missing data field (fallback)', async () => { |
| 97 | + const flat = { hello: 'world' }; |
| 98 | + fetchSpy.mockResolvedValue({ |
| 99 | + ok: true, |
| 100 | + json: async () => flat, |
| 101 | + } as Response); |
| 102 | + |
| 103 | + const result = await loadLanguage('en'); |
| 104 | + expect(result).toEqual(flat); |
| 105 | + }); |
| 106 | +}); |
0 commit comments