* fix: fallback to english challenges All challenges will use the english version if a translated file is not available. SHOW_NEW_CURRICULUM still gates what's shown in the client. * refactor: use closures to simplify createChallenge * refactor: remove messy destructure * refactor: add meta via helper * fix: fallback to [] for meta.required * fix: repair challenge.block * refactor: use CONST_CASE for meta + challenge dirs * fix: catch empty superblocks immediately * fix: clean up path.resolves * fix: invalid syntax in JS project steps * fix: default to english comments and relax tests Instead of always throwing errors when a comment is not translated, the tests now warn while SHOW_UPCOMING_CHANGES is true, so that tests will pass while we're developing and allow translators time to work. They still throw when SHOW_UPCOMING_CHANGES is false to catch issues in production * test: update createCommentMap test * refactor: delete stale comment * refactor: clarify validate with explanatory consts * feat: throw if audited cert falls back to english * fix: stop testing upcoming localized curriculum
140 lines
4.3 KiB
JavaScript
140 lines
4.3 KiB
JavaScript
const path = require('path');
|
|
|
|
const {
|
|
generateChallengeCreator,
|
|
hasEnglishSource,
|
|
createCommentMap
|
|
} = require('./getChallenges');
|
|
|
|
const EXISTING_CHALLENGE_PATH = 'challenge.md';
|
|
const MISSING_CHALLENGE_PATH = 'no/challenge.md';
|
|
|
|
const basePath = '__fixtures__';
|
|
|
|
describe('create non-English challenge', () => {
|
|
describe('generateChallengeCreator', () => {
|
|
describe('createChallenge', () => {
|
|
it('throws if lang is an invalid language', async () => {
|
|
const createChallenge = generateChallengeCreator(basePath, 'notlang');
|
|
await expect(
|
|
createChallenge(EXISTING_CHALLENGE_PATH, {})
|
|
).rejects.toThrow('notlang is not a accepted language');
|
|
});
|
|
it('throws an error if the source challenge is missing', async () => {
|
|
const createChallenge = generateChallengeCreator(basePath, 'chinese');
|
|
await expect(
|
|
createChallenge(MISSING_CHALLENGE_PATH, {})
|
|
).rejects.toThrow(
|
|
`Missing English challenge for
|
|
${MISSING_CHALLENGE_PATH}
|
|
It should be in
|
|
`
|
|
);
|
|
});
|
|
});
|
|
});
|
|
describe('hasEnglishSource', () => {
|
|
it('returns a boolean', async () => {
|
|
const sourceExists = await hasEnglishSource(
|
|
basePath,
|
|
EXISTING_CHALLENGE_PATH
|
|
);
|
|
expect(typeof sourceExists).toBe('boolean');
|
|
});
|
|
it('returns true if the English challenge exists', async () => {
|
|
const sourceExists = await hasEnglishSource(
|
|
basePath,
|
|
EXISTING_CHALLENGE_PATH
|
|
);
|
|
expect(sourceExists).toBe(true);
|
|
});
|
|
it('returns false if the English challenge is missing', async () => {
|
|
const sourceExists = await hasEnglishSource(
|
|
basePath,
|
|
MISSING_CHALLENGE_PATH
|
|
);
|
|
expect(sourceExists).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('createCommentMap', () => {
|
|
const dictionaryDir = path.resolve(
|
|
__dirname,
|
|
'__fixtures__',
|
|
'dictionaries'
|
|
);
|
|
const incompleteDictDir = path.resolve(
|
|
__dirname,
|
|
'__fixtures__',
|
|
'incomplete-dicts'
|
|
);
|
|
|
|
it('returns an object', () => {
|
|
expect(typeof createCommentMap(dictionaryDir)).toBe('object');
|
|
});
|
|
|
|
it('fallback to the untranslated string', () => {
|
|
expect.assertions(2);
|
|
const commentMap = createCommentMap(incompleteDictDir);
|
|
expect(commentMap['To be translated one'].spanish).toEqual(
|
|
'Spanish translation one'
|
|
);
|
|
expect(commentMap['To be translated two'].spanish).toEqual(
|
|
'To be translated two'
|
|
);
|
|
});
|
|
|
|
it('returns an object with an expected form', () => {
|
|
expect.assertions(4);
|
|
const expectedIds = [
|
|
'To be translated one',
|
|
'To be translated two',
|
|
'Not translated one',
|
|
'Not translated two'
|
|
];
|
|
const map = createCommentMap(dictionaryDir);
|
|
expect(Object.keys(map)).toEqual(expect.arrayContaining(expectedIds));
|
|
|
|
const mapValue = map['To be translated one'];
|
|
|
|
expect(Object.keys(mapValue)).toEqual(
|
|
expect.arrayContaining(['chinese', 'spanish'])
|
|
);
|
|
expect(typeof mapValue.chinese).toBe('string');
|
|
expect(typeof mapValue.spanish).toBe('string');
|
|
});
|
|
|
|
it('returns an object with expected values', () => {
|
|
expect.assertions(9);
|
|
const expectedIds = [
|
|
'To be translated one',
|
|
'To be translated two',
|
|
'Not translated one',
|
|
'Not translated two'
|
|
];
|
|
const map = createCommentMap(dictionaryDir);
|
|
expect(Object.keys(map)).toEqual(expect.arrayContaining(expectedIds));
|
|
|
|
const translatedOne = map['To be translated one'];
|
|
|
|
expect(translatedOne.chinese).toBe('Chinese translation one');
|
|
expect(translatedOne.spanish).toBe('Spanish translation one');
|
|
|
|
const translatedTwo = map['To be translated two'];
|
|
|
|
expect(translatedTwo.chinese).toBe('Chinese translation two');
|
|
expect(translatedTwo.spanish).toBe('Spanish translation two');
|
|
|
|
const untranslatedOne = map['Not translated one'];
|
|
|
|
expect(untranslatedOne.chinese).toBe('Not translated one');
|
|
expect(untranslatedOne.spanish).toBe('Not translated one');
|
|
|
|
const untranslatedTwo = map['Not translated two'];
|
|
|
|
expect(untranslatedTwo.chinese).toBe('Not translated two');
|
|
expect(untranslatedTwo.spanish).toBe('Not translated two');
|
|
});
|
|
});
|
|
});
|