|
| 1 | +import { vi } from 'vitest'; |
| 2 | +import type { TranslationsByLanguage } from '@vocab/core'; |
| 3 | +import { pushTranslations } from './phrase-api'; |
| 4 | + |
| 5 | +// Mock global fetch |
| 6 | +const mockFetch = vi.fn(); |
| 7 | +global.fetch = mockFetch; |
| 8 | + |
| 9 | +const getFormDataEntries = (call: [unknown, { body: FormData }]) => |
| 10 | + [...call[1].body.entries()].filter(([key]) => key !== 'file'); |
| 11 | + |
| 12 | +describe('phrase-api', () => { |
| 13 | + beforeEach(() => { |
| 14 | + vi.clearAllMocks(); |
| 15 | + // Reset environment variables |
| 16 | + process.env.PHRASE_API_TOKEN = 'test-token'; |
| 17 | + process.env.PHRASE_PROJECT_ID = 'test-project-id'; |
| 18 | + }); |
| 19 | + |
| 20 | + describe('pushTranslations', () => { |
| 21 | + const mockTranslations: TranslationsByLanguage = { |
| 22 | + en: { |
| 23 | + hello: { message: 'Hello' }, |
| 24 | + world: { message: 'World' }, |
| 25 | + }, |
| 26 | + fr: { |
| 27 | + hello: { message: 'Bonjour' }, |
| 28 | + world: { message: 'Monde' }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const mockUploadResponse = { |
| 33 | + status: 200, |
| 34 | + statusText: 'OK', |
| 35 | + headers: { |
| 36 | + get: vi.fn(() => null), |
| 37 | + }, |
| 38 | + json: vi.fn(() => Promise.resolve({ id: 'upload-123' })), |
| 39 | + }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + mockFetch.mockResolvedValue(mockUploadResponse); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should upload translations for each language', async () => { |
| 46 | + const result = await pushTranslations(mockTranslations, { |
| 47 | + branch: 'test-branch', |
| 48 | + devLanguage: 'en', |
| 49 | + }); |
| 50 | + |
| 51 | + expect(mockFetch).toHaveBeenCalledTimes(2); // One call per language |
| 52 | + expect(result).toEqual({ devLanguageUploadId: 'upload-123' }); |
| 53 | + |
| 54 | + const firstCall = mockFetch.mock.calls[0]; |
| 55 | + expect(firstCall[0]).toBe( |
| 56 | + 'https://api.phrase.com/v2/projects/test-project-id/uploads', |
| 57 | + ); |
| 58 | + |
| 59 | + // Check that fetch was called with correct FormData |
| 60 | + expect(getFormDataEntries(firstCall as any)).toMatchInlineSnapshot(` |
| 61 | + [ |
| 62 | + [ |
| 63 | + "file_format", |
| 64 | + "csv", |
| 65 | + ], |
| 66 | + [ |
| 67 | + "branch", |
| 68 | + "test-branch", |
| 69 | + ], |
| 70 | + [ |
| 71 | + "update_translations", |
| 72 | + "true", |
| 73 | + ], |
| 74 | + [ |
| 75 | + "update_descriptions", |
| 76 | + "true", |
| 77 | + ], |
| 78 | + [ |
| 79 | + "locale_mapping[en]", |
| 80 | + "4", |
| 81 | + ], |
| 82 | + [ |
| 83 | + "format_options[key_index]", |
| 84 | + "1", |
| 85 | + ], |
| 86 | + [ |
| 87 | + "format_options[comment_index]", |
| 88 | + "2", |
| 89 | + ], |
| 90 | + [ |
| 91 | + "format_options[tag_column]", |
| 92 | + "3", |
| 93 | + ], |
| 94 | + [ |
| 95 | + "format_options[enable_pluralization]", |
| 96 | + "false", |
| 97 | + ], |
| 98 | + ] |
| 99 | + `); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should include autoTranslate parameter when enabled', async () => { |
| 103 | + await pushTranslations(mockTranslations, { |
| 104 | + branch: 'test-branch', |
| 105 | + devLanguage: 'en', |
| 106 | + autoTranslate: true, |
| 107 | + }); |
| 108 | + |
| 109 | + const firstCall = mockFetch.mock.calls[0]; |
| 110 | + expect(getFormDataEntries(firstCall as any)).toMatchInlineSnapshot(` |
| 111 | + [ |
| 112 | + [ |
| 113 | + "file_format", |
| 114 | + "csv", |
| 115 | + ], |
| 116 | + [ |
| 117 | + "branch", |
| 118 | + "test-branch", |
| 119 | + ], |
| 120 | + [ |
| 121 | + "update_translations", |
| 122 | + "true", |
| 123 | + ], |
| 124 | + [ |
| 125 | + "update_descriptions", |
| 126 | + "true", |
| 127 | + ], |
| 128 | + [ |
| 129 | + "autotranslate", |
| 130 | + "true", |
| 131 | + ], |
| 132 | + [ |
| 133 | + "locale_mapping[en]", |
| 134 | + "4", |
| 135 | + ], |
| 136 | + [ |
| 137 | + "format_options[key_index]", |
| 138 | + "1", |
| 139 | + ], |
| 140 | + [ |
| 141 | + "format_options[comment_index]", |
| 142 | + "2", |
| 143 | + ], |
| 144 | + [ |
| 145 | + "format_options[tag_column]", |
| 146 | + "3", |
| 147 | + ], |
| 148 | + [ |
| 149 | + "format_options[enable_pluralization]", |
| 150 | + "false", |
| 151 | + ], |
| 152 | + ] |
| 153 | + `); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments