Add tests for add-missing-keys-to-other-languages

This commit is contained in:
Hung Viet Nguyen 2022-03-14 22:49:27 +07:00
parent 222eb9bbfc
commit 88149c1e3b
No known key found for this signature in database
GPG Key ID: CAFDC50535B4A074
4 changed files with 62 additions and 6 deletions

View File

@ -0,0 +1,42 @@
'use strict';
const fs = require('fs-extra');
const { addMissingKeyForSingleFile } = require('../add-missing-keys-to-other-languages');
// Do not run this test in --watch mode.
// Since `addMissingKeyForSingleFile` makes changes to the files (`en.json`, `vi.json`),
// accidentally triggering this test to re-run infinite times (because of jest watch mode)
describe('addMissingKeyForSingleFile', () => {
it('should add missing keys from en.json to translation file', async () => {
const TARGET_TRANSLATION_FILE_PATH = 'scripts/front/__tests__/vi.json';
const SOURCE_TRANSLATION_FILE_PATH = 'scripts/front/__tests__/en.json';
// Save original `vi.json` file content
const originalTargetTranslationFileContent = fs.readFileSync(
TARGET_TRANSLATION_FILE_PATH,
'utf8'
);
const originalTargetTranslationFileJSON = JSON.parse(originalTargetTranslationFileContent);
const mainTranslationFileJSON = await fs.readJSON(SOURCE_TRANSLATION_FILE_PATH);
// Add missing keys for `vi.json`
await addMissingKeyForSingleFile(TARGET_TRANSLATION_FILE_PATH);
const updatedTargetTranslationFileJSON = JSON.parse(
fs.readFileSync(TARGET_TRANSLATION_FILE_PATH, 'utf8')
);
// `vi.json` should have all keys from `en.json`
Object.keys(mainTranslationFileJSON).forEach((key) => {
expect(key in updatedTargetTranslationFileJSON).toBe(true);
});
// `vi.json` should keep the current translation
Object.keys(originalTargetTranslationFileContent).forEach((key) => {
expect(updatedTargetTranslationFileJSON[key]).toEqual(originalTargetTranslationFileJSON[key]);
});
// Restore original `vi.json` file content
await fs.writeJSON(TARGET_TRANSLATION_FILE_PATH, originalTargetTranslationFileJSON, {
spaces: 2,
});
});
});

View File

@ -0,0 +1,8 @@
{
"Analytics": "Analytics",
"Auth.components.Oops.text": "Your account has been suspended.",
"Auth.components.Oops.text.admin": "If this is a mistake, please contact your administrator.",
"Auth.components.Oops.title": "Oops...",
"Auth.form.button.forgot-password": "Send Email",
"Auth.form.button.go-home": "GO BACK HOME"
}

View File

@ -0,0 +1,6 @@
{
"Analytics": "Phân Tích",
"Auth.components.Oops.text.admin": "Nếu có sự nhầm lẫn, hãy liên hệ người quản trị",
"Auth.components.Oops.title": "Oops...",
"Auth.form.button.go-home": "QUAY VỀ TRANG CHỦ"
}

View File

@ -12,7 +12,7 @@ const fs = require('fs-extra');
const glob = promisify(require('glob').glob);
const chalk = require('chalk');
const addMissingKeyForSingleFile = async filePath => {
const addMissingKeyForSingleFile = async (filePath) => {
console.log('Start adding missing keys to', filePath);
try {
// Read translation file
@ -38,7 +38,7 @@ const addMissingKeyForSingleFile = async filePath => {
}
};
const addMissingKeys = async lang => {
const addMissingKeys = async (lang) => {
// Get translation files
const corePackageDirs = await glob('packages/core/*');
const pluginsPackageDirs = await glob('packages/plugins/*');
@ -46,12 +46,12 @@ const addMissingKeys = async lang => {
const pathToTranslationsFolder = ['admin', 'src', 'translations'];
const translationFiles = packageDirs
.filter(dir => {
.filter((dir) => {
return fs.existsSync(join(dir, ...pathToTranslationsFolder, `${lang}.json`));
})
.reduce((acc, dir) => {
return [...acc, join(dir, ...pathToTranslationsFolder, `${lang}.json`)];
}, []);
.map((dir) => {
return join(dir, ...pathToTranslationsFolder, `${lang}.json`);
});
console.log('List of files to add missing keys', translationFiles, '\n');
// For each file run addMissingKeyForSingleFile