|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { join } from 'path'; |
| 10 | +import { expectFileToMatch } from '../../utils/fs'; |
| 11 | +import { ng } from '../../utils/process'; |
| 12 | +import { updateJsonFile } from '../../utils/project'; |
| 13 | +import { baseDir, baseHrefs, externalServer, langTranslations, setupI18nConfig } from './setup'; |
| 14 | + |
| 15 | +export default async function () { |
| 16 | + // Setup i18n tests and config. |
| 17 | + await setupI18nConfig(); |
| 18 | + |
| 19 | + const URL_SUB_PATH: Record<string, string> = { |
| 20 | + 'en-US': '', |
| 21 | + 'fr': 'fr', |
| 22 | + 'de': 'deutsche', |
| 23 | + }; |
| 24 | + |
| 25 | + // Update angular.json |
| 26 | + await updateJsonFile('angular.json', (workspaceJson) => { |
| 27 | + const appProject = workspaceJson.projects['test-project']; |
| 28 | + // tslint:disable-next-line: no-any |
| 29 | + const i18n: Record<string, any> = appProject.i18n; |
| 30 | + |
| 31 | + i18n.sourceLocale = { |
| 32 | + subPath: URL_SUB_PATH['en-US'], |
| 33 | + }; |
| 34 | + |
| 35 | + i18n.locales['fr'] = { |
| 36 | + translation: i18n.locales['fr'], |
| 37 | + subPath: URL_SUB_PATH['fr'], |
| 38 | + }; |
| 39 | + |
| 40 | + i18n.locales['de'] = { |
| 41 | + translation: i18n.locales['de'], |
| 42 | + subPath: URL_SUB_PATH['de'], |
| 43 | + }; |
| 44 | + }); |
| 45 | + |
| 46 | + // Build each locale and verify the output. |
| 47 | + await ng('build'); |
| 48 | + for (const { lang } of langTranslations) { |
| 49 | + if (baseHrefs[lang] === undefined) { |
| 50 | + throw new Error('Invalid E2E test setup: unexpected locale ' + lang); |
| 51 | + } |
| 52 | + |
| 53 | + const outputPath = join(baseDir, URL_SUB_PATH[lang]); |
| 54 | + |
| 55 | + // Verify the HTML lang attribute is present |
| 56 | + await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`); |
| 57 | + |
| 58 | + // Verify the HTML base HREF attribute is present |
| 59 | + await expectFileToMatch(`${outputPath}/index.html`, `href="${baseHrefs[lang] || '/'}"`); |
| 60 | + |
| 61 | + // Execute Application E2E tests for a production build without dev server |
| 62 | + const { server, port, url } = await externalServer( |
| 63 | + outputPath, |
| 64 | + (baseHrefs[lang] as string) || '/', |
| 65 | + ); |
| 66 | + try { |
| 67 | + await ng( |
| 68 | + 'e2e', |
| 69 | + `--port=${port}`, |
| 70 | + `--configuration=${lang}`, |
| 71 | + '--dev-server-target=', |
| 72 | + `--base-url=${url}`, |
| 73 | + ); |
| 74 | + } finally { |
| 75 | + server.close(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments