Repository URL to install this package:
|
Version:
4.2.0 ▾
|
const fs = require('fs');
const {
getProjectTranslations,
getMergedProjectTranslations,
downloadMergedTranslationFiles,
updateTranslationFile,
uploadTranslationFiles,
} = require('./translations');
const api = require('./api');
const { unflatten } = require('./transform');
jest.mock('./api');
jest.mock('fs');
describe('Translation handling', () => {
test('getProjectTranslations', async () => {
api.getProjectLanguages.mockResolvedValue([{ code: 'en' }]);
api.getProjectMessagesByLanguage.mockResolvedValue({ key: 'value' });
const translations = await getProjectTranslations('1234', {
apiKey: 'apiKey',
secret: 'secret',
});
expect(translations).toEqual({ en: { key: 'value' } });
});
test('getMergedProjectTranslations', async () => {
api.getProjectLanguages.mockResolvedValue([{ code: 'en' }]);
api.getProjectMessagesByLanguage.mockImplementation(({ projectId }) => {
switch (projectId) {
case '1234':
return Promise.resolve({ key1: 'value1' });
case '5678':
return Promise.resolve({ key2: 'value2' });
default:
return Promise.reject(new Error('oops'));
}
});
const translations = await getMergedProjectTranslations(['1234', '5678'], {
apiKey: 'apiKey',
secret: 'secret',
});
expect(translations).toEqual({ en: { key1: 'value1', key2: 'value2' } });
});
test('downloadMergedTranslationFiles', async () => {
api.getProjectLanguages.mockResolvedValue([{ code: 'en' }]);
api.getProjectMessagesByLanguage.mockImplementation(({ projectId }) => {
switch (projectId) {
case '1234':
return Promise.resolve({ key1: 'value1' });
case '5678':
return Promise.resolve({ key2: 'value2' });
default:
return Promise.reject(new Error('ooops'));
}
});
fs.writeFile.mockImplementation((file, content, callback) => {
callback(null);
});
const translations = await downloadMergedTranslationFiles(['1234', '5678'], 'i18n', {
apiKey: 'apiKey',
secret: 'secret',
});
expect(translations).toEqual({ en: { key1: 'value1', key2: 'value2' } });
expect(fs.writeFile).toHaveBeenCalledWith(
'i18n/en.json',
JSON.stringify({ key1: 'value1', key2: 'value2' }),
expect.any(Function)
);
});
test('updateTranslationFiles', async () => {
const sourceMessages = {
'recipe.favorite.name': 'Boozy Baked French Toast',
'recipe.dish3.name': 'French Onion Soup',
'recipe.dish4.name': 'Spinach and Cheese Strata',
'recipe.dish5.name': 'Pasta al Pomodoro',
};
const baseMessages = {
recipe: {
dish1: {
name: 'Boozy Baked French Toast',
},
dish2: {
name: 'Eggs in Marinara Sauce',
},
dish3: {
name: 'Tomato Soup',
},
},
};
fs.writeFile.mockReset();
fs.readFile.mockImplementation((file, callback) => {
callback(null, JSON.stringify(baseMessages));
});
fs.writeFile.mockImplementation((file, content, callback) => {
callback(null);
});
await updateTranslationFile('i18n/en.json', sourceMessages, true);
const fileContents = JSON.parse(fs.writeFile.mock.calls[0][1]);
expect(fileContents).toEqual(unflatten(sourceMessages));
});
test('uploadTranslationFiles', async () => {
const baseMessages = {
key1: 'value1',
};
api.getProjectLanguages.mockResolvedValue([{ code: 'en' }, { code: 'de' }]);
api.uploadProjectTranslation.mockResolvedValue();
fs.readFile.mockReset();
fs.readFile.mockImplementation((file, callback) => {
callback(null, JSON.stringify(baseMessages));
});
await uploadTranslationFiles('i18n', '1234', {
apiKey: 'apiKey',
secret: 'secret',
});
expect(fs.readFile).toHaveBeenCalledWith('i18n/en.json', expect.any(Function));
expect(fs.readFile).toHaveBeenCalledWith('i18n/de.json', expect.any(Function));
const uploadedMessages = api.uploadProjectTranslation.mock.calls[0][1];
expect(uploadedMessages).toEqual(baseMessages);
});
});