Files
freeCodeCamp/tools/challenge-parser/translation-parser/index.test.js
Oliver Eyton-Williams e118dda13a fix: order imports and remove circular dependencies (#41824)
* fix: remove circular dependency

redux depended on templates/Challenges/redux and vice versa.  This
meant that import order mattered and confusing bugs could arise.

(cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc)

* feat: require imports to be in alphabetical order

Import order generally does not matter, but there are edge cases
(circular  imports and css imports, for example) where changing order
changes behaviour

(cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c)

* chore: order imports

* fix: lift up challenge description + title comps

This brings the classic Show closer to the others as they
now all create the description and title components

* fix: remove donation-saga/index circular import

(cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207)

* refactor: extract action-types from settings

(cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd)

* fix: lint errors

* feat: prevent useless renames
2021-08-02 08:39:40 -05:00

353 lines
13 KiB
JavaScript

const {
ENGLISH_CHALLENGE_NO_FILES
} = require('./__fixtures__/challenge-objects');
const { SIMPLE_TRANSLATION } = require('./__mocks__/mock-comments');
const {
translateComments,
translateCommentsInChallenge,
translateGeneric
} = require('.');
let logSpy;
describe('translation parser', () => {
beforeEach(() => {
logSpy = jest.spyOn(console, 'warn').mockImplementation();
});
afterEach(() => {
logSpy.mockRestore();
});
describe('translateGeneric', () => {
it('returns an object containing translated text', () => {
expect.assertions(1);
const seed = `// Add your code below this line
Add your code above this line `;
const transSeed = `// (Chinese) Add your code below this line (Chinese)
Add your code above this line `;
const knownComments = Object.keys(SIMPLE_TRANSLATION);
const config = {
knownComments,
dict: SIMPLE_TRANSLATION,
lang: 'chinese'
};
const actual = translateGeneric(
{ text: seed, commentCounts: new Map() },
config,
'((?<!https?:)//\\s*)',
'(\\s*$)'
);
expect(actual.text).toBe(transSeed);
});
it('returns an object containing a count of the replaced comments', () => {
expect.assertions(1);
const seed = `// Add your code below this line
// Add your code above this line
// Add your code below this line
`;
const expectedCommentCounts = new Map();
expectedCommentCounts
.set('(Chinese) Add your code below this line (Chinese)', 2)
.set('(Chinese) Add your code above this line (Chinese)', 1);
const knownComments = Object.keys(SIMPLE_TRANSLATION);
const config = {
knownComments,
dict: SIMPLE_TRANSLATION,
lang: 'chinese'
};
const actual = translateGeneric(
{ text: seed, commentCounts: new Map() },
config,
'((?<!https?:)//\\s*)',
'(\\s*$)'
);
expect(actual.commentCounts).toEqual(expectedCommentCounts);
});
});
describe('translateCommentsInChallenge', () => {
it('returns a clone of the challenge if there are no comments', () => {
expect(
translateCommentsInChallenge(
ENGLISH_CHALLENGE_NO_FILES,
'chinese',
SIMPLE_TRANSLATION
)
).toEqual(ENGLISH_CHALLENGE_NO_FILES);
});
});
describe('translateComments', () => {
it('replaces single line English comments with their translations', () => {
const seed = `// Add your code below this line
Add your code above this line `;
const transSeed = `// (Chinese) Add your code below this line (Chinese)
Add your code above this line `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
});
it('does not translate urls', () => {
const seed = `http:// Add your code below this line
Add your code above this line `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(seed);
const seedS = `https:// Add your code below this line
Add your code above this line `;
expect(
translateComments(seedS, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(seedS);
});
it('replaces inline English comments with their translations', () => {
const seed = `inline comment // Add your code below this line
Add your code above this line `;
const transSeed = `inline comment // (Chinese) Add your code below this line (Chinese)
Add your code above this line `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
});
it('replaces multiple English comments with their translations', () => {
const seed = `inline comment // Add your code below this line
// Add your code below this line `;
const transSeed = `inline comment // (Chinese) Add your code below this line (Chinese)
// (Chinese) Add your code below this line (Chinese) `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
});
it('replaces multiline English comments with their translations', () => {
const seed = `multiline comment /* Add your code below this line */
/* Add your code above this line */ change code below this line `;
const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */
/* (Chinese) Add your code above this line (Chinese) */ change code below this line `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
});
it('replaces repeated multiline comments with their translations', () => {
const seed = `multiline comment /* Add your code below this line */
/* Add your code below this line */ change code below this line `;
const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */
/* (Chinese) Add your code below this line (Chinese) */ change code below this line `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
});
it('ignores empty comments', () => {
expect.assertions(1);
const seed = '//';
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(seed);
});
it('only replaces text inside comments, not between them', () => {
const seed = `multiline comment /* Add your code below this line */
/* Add your code above this line */ Add your code below this line /* */ `;
const transSeed = `multiline comment /* (Chinese) Add your code below this line (Chinese) */
/* (Chinese) Add your code above this line (Chinese) */ Add your code below this line /* */ `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(transSeed);
const seedTwo = `multiline /* */ Add your code below this line /* */ `;
expect(
translateComments(seedTwo, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(seedTwo);
});
it('replaces English html comments with their translations', () => {
const seed = `<div> <!-- Add your code below this line -->
<!-- Add your code above this line --> <span>change code below this line</span> `;
const transSeed = `<div> <!-- (Chinese) Add your code below this line (Chinese) -->
<!-- (Chinese) Add your code above this line (Chinese) --> <span>change code below this line</span> `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(transSeed);
});
it('replaces css comments with their translations', () => {
const seed = `<style>
/* Add your code below this line */
</style>`;
const transSeed = `<style>
/* (Chinese) Add your code below this line (Chinese) */
</style>`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(transSeed);
});
it('replaces multiple css comments with their translations', () => {
const seed = `<style>
/* Add your code below this line */
/* Add your code below this line */
</style>`;
const transSeed = `<style>
/* (Chinese) Add your code below this line (Chinese) */
/* (Chinese) Add your code below this line (Chinese) */
</style>`;
const commentCounts = new Map();
commentCounts.set('(Chinese) Add your code below this line (Chinese)', 2);
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
).toEqual({ text: transSeed, commentCounts });
});
it('ignores css comments outside style tags', () => {
const seed = `/* Add your code below this line */`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(seed);
});
it('ignores css comments between style tags', () => {
const seed = `<style>
</style>
/* Add your code below this line */
<style>
</style>`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(seed);
});
it('only replaces inside English html comments', () => {
const seed = `<div> <!-- --> Add your code below this line <!-- -->`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(seed);
});
it('replaces English JSX comments with their translations', () => {
const seed = `{ /* Add your code below this line */ }
{ /* Add your code above this line */ } <span>change code below this line</span> `;
const transSeed = `{ /* (Chinese) Add your code below this line (Chinese) */ }
{ /* (Chinese) Add your code above this line (Chinese) */ } <span>change code below this line</span> `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx').text
).toBe(transSeed);
});
it('replaces English script comments with their translations', () => {
const seed = `<script>
// Add your code below this line
</script>`;
const transSeed = `<script>
// (Chinese) Add your code below this line (Chinese)
</script>`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(transSeed);
});
it('replaces multiple script comments with their translations', () => {
const seed = `<script>
/* Add your code below this line */
// Add your code below this line
</script>`;
const transSeed = `<script>
/* (Chinese) Add your code below this line (Chinese) */
// (Chinese) Add your code below this line (Chinese)
</script>`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html').text
).toBe(transSeed);
});
it('ignores html comments inside JavaScript', () => {
const seed = `<div> <!-- Add your code below this line
Add your code above this line --> <span>change code below this line</span> `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js').text
).toBe(seed);
});
it('ignores html comments inside jsx', () => {
const seed = `<div> <!-- Add your code below this line
Add your code above this line --> <span>change code below this line</span> `;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx').text
).toBe(seed);
});
it('throws if there is not an exact match (js)', () => {
expect.assertions(2);
const seedMulti = `/* Add your code below this line
Add your code above this line */ <span>change code below this line</span> `;
const seedInline = `// Add your code below this line, please`;
expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js')
).toThrow();
expect(() =>
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js')
).toThrow();
});
it('only replaces exact matches (jsx)', () => {
expect.assertions(1);
const seedMulti = `{ /* Add your code below this line
Add your code above this line */ } <span>change code below this line</span> `;
expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'jsx')
).toThrow();
});
it('only replaces exact matches (html)', () => {
expect.assertions(1);
const seed = `<div> <!-- Add your code below this line
Add your code above this line --> <span>change code below this line</span> `;
expect(() =>
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
});
it('only translates jsx comments once', () => {
const seed = `{ /* Add your code below this line */ }`;
const transSeed = `{ /* (Chinese) Add your code below this line (Chinese) */ }`;
expect(
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'jsx').text
).toBe(transSeed);
});
it('throws if the comment is not in the dictionary', () => {
expect.assertions(6);
const seedJSX = `{ /* this is not a comment */ }`;
const seedInline = `// this is not a comment `;
const seedMulti = `/* this is not a comment */`;
const seedCSS = `<style>
/* this is not a comment */
</style>`;
const seedHTML = `<div> <!-- this is not a comment --> `;
const seedScript = `<script> // this is not a comment </script>`;
expect(() =>
translateComments(seedJSX, 'chinese', SIMPLE_TRANSLATION, 'jsx')
).toThrow();
expect(() =>
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js')
).toThrow();
expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js')
).toThrow();
expect(() =>
translateComments(seedCSS, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
expect(() =>
translateComments(seedHTML, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
expect(() =>
translateComments(seedScript, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
});
});
});