Skip to content

Commit

Permalink
Add test fixture for fixing translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikek2252 committed May 24, 2021
1 parent 3b1f982 commit 9d26824
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 6 deletions.
88 changes: 83 additions & 5 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ exports[`Test Fixtures decorators 1`] = `
"
`;

exports[`Test Fixtures decorators 2`] = `Map {}`;

exports[`Test Fixtures embedded-conditionals 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
👏 No unused translations were found!
👏 No missing translations were found!
"
`;

exports[`Test Fixtures embedded-conditionals 2`] = `Map {}`;

exports[`Test Fixtures emblem 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand All @@ -26,6 +42,8 @@ exports[`Test Fixtures emblem 1`] = `
"
`;

exports[`Test Fixtures emblem 2`] = `Map {}`;

exports[`Test Fixtures missing-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand All @@ -41,6 +59,8 @@ exports[`Test Fixtures missing-translations 1`] = `
"
`;

exports[`Test Fixtures missing-translations 2`] = `Map {}`;

exports[`Test Fixtures no-issues 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand All @@ -53,34 +73,90 @@ exports[`Test Fixtures no-issues 1`] = `
"
`;

exports[`Test Fixtures unused-translations 1`] = `
exports[`Test Fixtures no-issues 2`] = `Map {}`;

exports[`Test Fixtures remove-unused-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
⚠️ Found 2 unused translations!
⚠️ Found 3 unused translations!
- foo (used in de.json and en.json)
- extra (used in de.json)
- foo.bar.unnested (used in de.json and en.json)
👏 No missing translations were found!
"
`;

exports[`Test Fixtures yaml-translations 1`] = `
exports[`Test Fixtures remove-unused-translations 2`] = `
Map {
"translations/de.json" => "{
\\"hbs-translation\\": \\"Lenkstage\\",
\\"js-translation\\": \\"Affentheater\\"
}",
"translations/en.json" => "{
\\"hbs-translation\\": \\"HBS!\\",
\\"js-translation\\": \\"JS!\\"
}",
}
`;

exports[`Test Fixtures remove-unused-translations-nested 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
👏 No unused translations were found!
⚠️ Found 3 unused translations!
- foo (used in de.json and en.json)
- extra (used in de.json)
- parent.child.unusedKey (used in de.json and en.json)
👏 No missing translations were found!
"
`;

exports[`Test Fixtures embedded-conditionals 1`] = `
exports[`Test Fixtures remove-unused-translations-nested 2`] = `
Map {
"translations/de.json" => "{
\\"hbs-translation\\": \\"Lenkstage\\",
\\"js-translation\\": \\"Affentheater\\",
\\"parent\\": {
\\"secondChild\\": \\"usedKey\\"
}
}",
"translations/en.json" => "{
\\"hbs-translation\\": \\"HBS!\\",
\\"js-translation\\": \\"JS!\\",
\\"parent\\": {
\\"secondChild\\": \\"usedKey\\"
}
}",
}
`;

exports[`Test Fixtures unused-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
⚠️ Found 2 unused translations!
- foo (used in de.json and en.json)
- extra (used in de.json)
👏 No missing translations were found!
"
`;

exports[`Test Fixtures unused-translations 2`] = `Map {}`;

exports[`Test Fixtures yaml-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
Expand All @@ -91,3 +167,5 @@ exports[`Test Fixtures embedded-conditionals 1`] = `
👏 No missing translations were found!
"
`;

exports[`Test Fixtures yaml-translations 2`] = `Map {}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default Controller.extend({
foo: computed('intl.locale', function() {
return this.intl.t('js-translation');
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{t "hbs-translation"}}
{{t "parent.secondChild"}}
12 changes: 12 additions & 0 deletions fixtures/remove-unused-translations-nested/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"hbs-translation": "Lenkstage",
"js-translation": "Affentheater",
"foo": "BARR!",
"extra": "drei",
"parent": {
"child": {
"unusedKey": "unused Key",
},
"secondChild": "usedKey"
}
}
11 changes: 11 additions & 0 deletions fixtures/remove-unused-translations-nested/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hbs-translation": "HBS!",
"js-translation": "JS!",
"foo": "bar",
"parent": {
"child": {
"unusedKey": "unused Key",
},
"secondChild": "usedKey"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default Controller.extend({
foo: computed('intl.locale', function() {
return this.intl.t('js-translation');
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{t "hbs-translation"}}
7 changes: 7 additions & 0 deletions fixtures/remove-unused-translations/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"hbs-translation": "Lenkstage",
"js-translation": "Affentheater",
"foo": "BARR!",
"extra": "drei",
"foo.bar.unnested": "unnested key"
}
6 changes: 6 additions & 0 deletions fixtures/remove-unused-translations/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hbs-translation": "HBS!",
"js-translation": "JS!",
"foo": "bar",
"foo.bar.unnested": "unnested key"
}
20 changes: 19 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ let fixtures = fs.readdirSync(`${__dirname}/fixtures/`);

describe('Test Fixtures', () => {
let output;
let writtenFiles;
let fixturesWithErrors = ['emblem', 'missing-translations', 'unused-translations'];
let fixturesWithFix = ['remove-unused-translations', 'remove-unused-translations-nested'];

beforeEach(() => {
output = '';
writtenFiles = new Map();
});

function log(str = '') {
output += `${str}\n`;
}
function writeToFile(filePath, updatedTranslations) {
let fileName = filePath.split('/').slice(-2).join('/');
writtenFiles.set(fileName, updatedTranslations);
}

for (let fixture of fixtures) {
test(fixture, async () => {
let returnValue = await run(`${__dirname}/fixtures/${fixture}`, { log, color: false });
if (fixturesWithFix.includes(fixture)) {
process.argv.push('--fix');
}

let returnValue = await run(`${__dirname}/fixtures/${fixture}`, {
log,
color: false,
writeToFile,
});

let expectedReturnValue = fixturesWithErrors.includes(fixture) ? 1 : 0;

expect(returnValue).toEqual(expectedReturnValue);
expect(output).toMatchSnapshot();
expect(writtenFiles).toMatchSnapshot();

process.argv = process.argv.filter(arg => arg !== '--fix');
});
}
});
Expand Down

0 comments on commit 9d26824

Please sign in to comment.