-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate currentlySupportedLangs.jsx dynamically
instead of hardcoded `transifex_langs = "ar,fr,es_419,zh_CN"` now it generates the file based on the `atlas pull` result
- Loading branch information
1 parent
fd5375f
commit 5af01a9
Showing
10 changed files
with
181 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Tests for the generateSupportedLangs.js command line. | ||
|
||
import path from 'path'; | ||
import { main as realMain } from './generateSupportedLangs'; | ||
|
||
const sempleAppsDirectory = path.join(__dirname, '../../../../test-apps'); | ||
|
||
// History for `process.stdout.write` mock calls. | ||
const logHistory = { | ||
log: [], | ||
latest: null, | ||
}; | ||
|
||
// History for `fs.writeFileSync` mock calls. | ||
const writeFileHistory = { | ||
log: [], | ||
latest: null, | ||
}; | ||
|
||
// Mock for process.stdout.write | ||
const log = (text) => { | ||
logHistory.latest = text; | ||
logHistory.log.push(text); | ||
}; | ||
|
||
// Mock for fs.writeFileSync | ||
const writeFileSync = (filename, content) => { | ||
const entry = { filename, content }; | ||
writeFileHistory.latest = entry; | ||
writeFileHistory.log.push(entry); | ||
}; | ||
|
||
// Main with mocked output | ||
const main = (args) => realMain({ | ||
log, | ||
writeFileSync, | ||
i18nMessagesDir: `${sempleAppsDirectory}/app-with-translations/src/i18n/messages`, | ||
...args, | ||
}); | ||
|
||
// Clean up mock histories | ||
beforeEach(() => { | ||
logHistory.log = []; | ||
logHistory.latest = null; | ||
writeFileHistory.log = []; | ||
writeFileHistory.latest = null; | ||
}); | ||
|
||
describe('help document', () => { | ||
it('should print help for --help', () => { | ||
const success = main({ | ||
i18nMessagesDir: '--help', | ||
}); | ||
expect(logHistory.latest).toMatch( | ||
"generateSupportedLangs.js — Script to generate the 'src/i18n/messages/currentlySupportedLangs.jsx'" | ||
); | ||
expect(success).toBe(true); | ||
}); | ||
|
||
it('should print help for -h', () => { | ||
const success = main({ | ||
i18nMessagesDir: '--help', | ||
}); | ||
expect(logHistory.latest).toMatch( | ||
"generateSupportedLangs.js — Script to generate the 'src/i18n/messages/currentlySupportedLangs.jsx'" | ||
); | ||
expect(success).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('generate with three languages', () => { | ||
it('should generate currentlySupportedLangs.jsx', () => { | ||
const success = main({ | ||
i18nMessagesDir: `${sempleAppsDirectory}/app-with-translations/src/i18n/messages`, | ||
}); | ||
|
||
expect(writeFileHistory.log.length).toBe(1); | ||
expect(writeFileHistory.latest.filename).toBe(`${sempleAppsDirectory}/app-with-translations/src/i18n/messages/currentlySupportedLangs.jsx`); | ||
expect(success).toBe(true); // Languages generated successfully | ||
|
||
// It should write the file with the following content: | ||
// - import 'react-intl/locale-data/ar' and ar.json messages | ||
// - import 'react-intl/locale-data/fr' and fr.json messages | ||
// - import fr_CA.json messages without duplicating the `fr` import because it's the same language | ||
// - import 'react-intl/locale-data/zh' and zh_CN.json messages | ||
// - export the imported locale-data | ||
expect(writeFileHistory.latest.content).toEqual(`// This file is generated by the "generateSupportedLangs.js" script. | ||
import './ar.json'; | ||
import './fr.json'; | ||
import './fr_CA.json'; | ||
import './zh_CN.json'; | ||
import arData from 'react-intl/locale-data/ar'; | ||
import frData from 'react-intl/locale-data/fr'; | ||
import zhData from 'react-intl/locale-data/zh'; | ||
export default { | ||
'ar': arData, | ||
'fr': frData, | ||
'fr-ca': frData, | ||
'zh-cn': zhData, | ||
}; | ||
`); | ||
}); | ||
}); | ||
|
||
describe('generate errors', () => { | ||
it('should fail with no languages', () => { | ||
const success = main({ | ||
i18nMessagesDir: `${sempleAppsDirectory}/app-without-translations/src/i18n/messages`, | ||
}); | ||
|
||
// It should fail with the following error message: | ||
expect(logHistory.latest).toContain('generateSupportedLangs.js: Error: No language files found in the "'); | ||
|
||
expect(writeFileHistory.log).toEqual([]); | ||
expect(success).toBe(false); // No languages to generate | ||
}); | ||
|
||
it('should fail with no MESSAGES_DIR parameter', () => { | ||
const success = main({ | ||
i18nMessagesDir: '', | ||
}); | ||
|
||
// It should fail with the following error message: | ||
expect(logHistory.latest).toBe('generateSupportedLangs.js: Error: The "MESSAGES_DIR" parameter is required.\n'); | ||
|
||
expect(writeFileHistory.log).toEqual([]); | ||
expect(success).toBe(false); // MESSAGES_DIR parameter is required | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Test apps | ||
|
||
These test apps are used by the `src/utils/i18n/scripts/generateSupportedLangs.test.js` file. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Empty file to preserve directory structure |