Skip to content

Commit f5c12a6

Browse files
filipesilvavikerman
authored andcommitted
test: refactor i18n tests
The app-shell and service worker tests were not refactored because they are disabled. They should be refactored when re-enabled.
1 parent 88a8cc5 commit f5c12a6

File tree

8 files changed

+301
-667
lines changed

8 files changed

+301
-667
lines changed

tests/legacy-cli/e2e/tests/i18n/aot.ts

Lines changed: 0 additions & 158 deletions
This file was deleted.

tests/legacy-cli/e2e/tests/i18n/ivy-localize-app-shell.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const snapshots = require('../../ng-snapshot/package.json');
1515

1616
export default async function () {
1717
// TEMP: disable pending i18n updates
18+
// TODO: when re-enabling, use setupI18nConfig and helpers like other i18n tests.
1819
return;
1920

2021
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];

tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl.ts

Lines changed: 20 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,45 @@
1-
import * as express from 'express';
2-
import { resolve } from 'path';
3-
import { getGlobalVariable } from '../../utils/env';
4-
import {
5-
appendToFile,
6-
copyFile,
7-
expectFileToExist,
8-
expectFileToMatch,
9-
replaceInFile,
10-
writeFile,
11-
} from '../../utils/fs';
12-
import { ng, npm } from '../../utils/process';
1+
import { appendToFile, expectFileToMatch } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
133
import { updateJsonFile } from '../../utils/project';
144
import { expectToFail } from '../../utils/utils';
15-
import { readNgVersion } from '../../utils/version';
5+
import { baseDir, externalServer, langTranslations, setupI18nConfig } from './legacy';
166

17-
export default async function() {
18-
let localizeVersion = '@angular/localize@' + readNgVersion();
19-
if (getGlobalVariable('argv')['ng-snapshots']) {
20-
localizeVersion = require('../../ng-snapshot/package.json').dependencies['@angular/localize'];
21-
}
22-
await npm('install', `${localizeVersion}`);
237

8+
export default async function () {
9+
// Setup i18n tests and config.
10+
await setupI18nConfig();
11+
12+
// Ensure a DL build is used.
2413
await updateJsonFile('tsconfig.json', config => {
2514
config.compilerOptions.target = 'es2015';
2615
config.angularCompilerOptions.disableTypeScriptVersionCheck = true;
2716
});
2817

29-
const baseDir = 'dist/test-project';
30-
31-
// Set configurations for each locale.
32-
const langTranslations = [
33-
// TODO: re-enable all locales once localeData support is added.
34-
// { lang: 'en-US', translation: 'Hello i18n!' },
35-
{ lang: 'fr', translation: 'Bonjour i18n!' },
36-
];
37-
38-
await updateJsonFile('angular.json', workspaceJson => {
39-
const appProject = workspaceJson.projects['test-project'];
40-
const appArchitect = appProject.architect || appProject.targets;
41-
const serveConfigs = appArchitect['serve'].configurations;
42-
const e2eConfigs = appArchitect['e2e'].configurations;
43-
const buildConfigs = appArchitect['build'].configurations;
44-
45-
// Make default builds prod.
46-
appArchitect['build'].options.optimization = true;
47-
appArchitect['build'].options.buildOptimizer = true;
48-
appArchitect['build'].options.aot = true;
49-
appArchitect['build'].options.fileReplacements = [
50-
{
51-
replace: 'src/environments/environment.ts',
52-
with: 'src/environments/environment.prod.ts',
53-
},
54-
];
55-
56-
// Enable localization for all locales
57-
// TODO: re-enable all locales once localeData support is added.
58-
// appArchitect['build'].options.localize = true;
59-
appArchitect['build'].options.localize = ['fr'];
60-
// Always error on missing translations.
61-
appArchitect['build'].options.i18nMissingTranslation = 'error';
62-
63-
// Add locale definitions to the project
64-
// tslint:disable-next-line: no-any
65-
const i18n: Record<string, any> = (appProject.i18n = { locales: {} });
66-
for (const { lang } of langTranslations) {
67-
if (lang == 'en-US') {
68-
i18n.sourceLocale = lang;
69-
} else {
70-
i18n.locales[lang] = `src/locale/messages.${lang}.xlf`;
71-
}
72-
73-
buildConfigs[lang] = { localize: [lang] };
74-
serveConfigs[lang] = { browserTarget: `test-project:build:${lang}` };
75-
e2eConfigs[lang] = {
76-
specs: [`./src/app.${lang}.e2e-spec.ts`],
77-
devServerTarget: `test-project:serve:${lang}`,
78-
};
79-
}
80-
});
81-
82-
// Add a translatable element.
83-
await writeFile(
84-
'src/app/app.component.html',
85-
'<h1 i18n="An introduction header for this sample">Hello i18n!</h1>',
86-
);
87-
88-
// Extract the translation messages and copy them for each language.
89-
await ng('xi18n', '--output-path=src/locale');
90-
await expectFileToExist('src/locale/messages.xlf');
91-
await expectFileToMatch('src/locale/messages.xlf', `source-language="en-US"`);
92-
await expectFileToMatch('src/locale/messages.xlf', `An introduction header for this sample`);
93-
94-
for (const { lang, translation } of langTranslations) {
95-
if (lang != 'en-US') {
96-
await copyFile('src/locale/messages.xlf', `src/locale/messages.${lang}.xlf`);
97-
await replaceInFile(
98-
`src/locale/messages.${lang}.xlf`,
99-
'source-language="en-US"',
100-
`source-language="en-US" target-language="${lang}"`,
101-
);
102-
await replaceInFile(
103-
`src/locale/messages.${lang}.xlf`,
104-
'<source>Hello i18n!</source>',
105-
`<source>Hello i18n!</source>\n<target>${translation}</target>`,
106-
);
107-
}
108-
}
18+
// TODO: re-enable all locales once localeData support is added.
19+
const tempLangTranslations = langTranslations.filter(l => l.lang == 'fr');
10920

11021
// Build each locale and verify the output.
11122
// NOTE: this should not fail in general, but multi-locale translation is currently disabled.
11223
// TODO: remove expectToFail once localeData support is added.
11324
await expectToFail(() => ng('build', '--localize', 'true'));
11425
await ng('build');
115-
for (const { lang, translation } of langTranslations) {
116-
await expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, translation);
117-
await expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, translation);
118-
await expectToFail(() => expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, '$localize`'));
119-
await expectToFail(() => expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, '$localize`'));
120-
await expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, lang);
121-
await expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, lang);
122-
123-
// Ivy i18n doesn't yet work with `ng serve` so we must use a separate server.
124-
const app = express();
125-
app.use(express.static(resolve(baseDir, lang)));
126-
const server = app.listen(4200, 'localhost');
26+
for (const { lang, outputPath, translation } of tempLangTranslations) {
27+
await expectFileToMatch(`${outputPath}/main-es5.js`, translation.helloPartial);
28+
await expectFileToMatch(`${outputPath}/main-es2015.js`, translation.helloPartial);
29+
await expectToFail(() => expectFileToMatch(`${outputPath}/main-es5.js`, '$localize`'));
30+
await expectToFail(() => expectFileToMatch(`${outputPath}/main-es2015.js`, '$localize`'));
31+
await expectFileToMatch(`${outputPath}/main-es5.js`, lang);
32+
await expectFileToMatch(`${outputPath}/main-es2015.js`, lang);
33+
34+
const server = externalServer(outputPath);
12735
try {
128-
// Add E2E test for locale
129-
await writeFile(
130-
'e2e/src/app.e2e-spec.ts',
131-
`
132-
import { browser, logging, element, by } from 'protractor';
133-
describe('workspace-project App', () => {
134-
it('should display welcome message', () => {
135-
browser.get(browser.baseUrl);
136-
expect(element(by.css('h1')).getText()).toEqual('${translation}');
137-
});
138-
afterEach(async () => {
139-
// Assert that there are no errors emitted from the browser
140-
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
141-
expect(logs).not.toContain(jasmine.objectContaining({
142-
level: logging.Level.SEVERE,
143-
} as logging.Entry));
144-
});
145-
});
146-
`,
147-
);
148-
14936
// Execute without a devserver.
150-
await ng('e2e', '--devServerTarget=');
37+
await ng('e2e', `--configuration=${lang}`, '--devServerTarget=');
15138
} finally {
15239
server.close();
15340
}
15441
}
15542

156-
// Verify locale data registration (currently only for single locale builds)
157-
await ng('build', '--optimization', 'false', '-c', 'fr', '--i18n-missing-translation', 'error');
158-
await expectFileToMatch(`${baseDir}/fr/main-es5.js`, 'registerLocaleData');
159-
await expectFileToMatch(`${baseDir}/fr/main-es2015.js`, 'registerLocaleData');
160-
16143
// Verify missing translation behaviour.
16244
await appendToFile('src/app/app.component.html', '<p i18n>Other content</p>');
16345
await ng('build', '--i18n-missing-translation', 'ignore');

0 commit comments

Comments
 (0)