|
| 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 | +import { loadLanguage } from '../loadLanguage'; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Tests |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +describe('loadLanguage', () => { |
| 16 | + const fetchSpy = vi.fn<(...args: Parameters<typeof fetch>) => Promise<Response>>(); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + vi.stubGlobal('fetch', fetchSpy); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.restoreAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('unwraps spec REST API envelope { data: { locale, translations } }', async () => { |
| 27 | + const translations = { crm: { contact: '联系人', account: '客户' } }; |
| 28 | + fetchSpy.mockResolvedValue({ |
| 29 | + ok: true, |
| 30 | + json: async () => ({ data: { locale: 'zh-CN', translations } }), |
| 31 | + } as Response); |
| 32 | + |
| 33 | + const result = await loadLanguage('zh-CN'); |
| 34 | + expect(result).toEqual(translations); |
| 35 | + expect(fetchSpy).toHaveBeenCalledWith('/api/v1/i18n/translations/zh-CN'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns flat JSON when mock/dev server returns without envelope', async () => { |
| 39 | + const flat = { crm: { contact: 'Contact', account: 'Account' } }; |
| 40 | + fetchSpy.mockResolvedValue({ |
| 41 | + ok: true, |
| 42 | + json: async () => flat, |
| 43 | + } as Response); |
| 44 | + |
| 45 | + const result = await loadLanguage('en'); |
| 46 | + expect(result).toEqual(flat); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns empty object on HTTP error', async () => { |
| 50 | + fetchSpy.mockResolvedValue({ ok: false, status: 404 } as Response); |
| 51 | + |
| 52 | + const result = await loadLanguage('xx'); |
| 53 | + expect(result).toEqual({}); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns empty object on network failure', async () => { |
| 57 | + fetchSpy.mockRejectedValue(new TypeError('Failed to fetch')); |
| 58 | + |
| 59 | + const result = await loadLanguage('en'); |
| 60 | + expect(result).toEqual({}); |
| 61 | + }); |
| 62 | + |
| 63 | + it('handles envelope with null translations gracefully (fallback)', async () => { |
| 64 | + fetchSpy.mockResolvedValue({ |
| 65 | + ok: true, |
| 66 | + json: async () => ({ data: { locale: 'en', translations: null } }), |
| 67 | + } as Response); |
| 68 | + |
| 69 | + const result = await loadLanguage('en'); |
| 70 | + // translations is null (not an object), so fallback to full JSON |
| 71 | + expect(result).toEqual({ data: { locale: 'en', translations: null } }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('handles envelope with missing data field (fallback)', async () => { |
| 75 | + const flat = { hello: 'world' }; |
| 76 | + fetchSpy.mockResolvedValue({ |
| 77 | + ok: true, |
| 78 | + json: async () => flat, |
| 79 | + } as Response); |
| 80 | + |
| 81 | + const result = await loadLanguage('en'); |
| 82 | + expect(result).toEqual(flat); |
| 83 | + }); |
| 84 | +}); |
0 commit comments