|
| 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { concatMap, count, take, timeout } from 'rxjs/operators'; |
| 10 | +import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index'; |
| 11 | +import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; |
| 12 | + |
| 13 | +describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { |
| 14 | + describe('Behavior: "localize works in watch mode"', () => { |
| 15 | + beforeEach(() => { |
| 16 | + harness.useProject('test', { |
| 17 | + root: '.', |
| 18 | + sourceRoot: 'src', |
| 19 | + cli: { |
| 20 | + cache: { |
| 21 | + enabled: false, |
| 22 | + }, |
| 23 | + }, |
| 24 | + i18n: { |
| 25 | + locales: { |
| 26 | + 'fr': 'src/locales/messages.fr.xlf', |
| 27 | + }, |
| 28 | + }, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('localize works in watch mode', async () => { |
| 33 | + harness.useTarget('build', { |
| 34 | + ...BASE_OPTIONS, |
| 35 | + watch: true, |
| 36 | + localize: true, |
| 37 | + }); |
| 38 | + |
| 39 | + await harness.writeFile( |
| 40 | + 'src/app/app.component.html', |
| 41 | + ` |
| 42 | + <p id="hello" i18n="An introduction header for this sample">Hello {{ title }}! </p> |
| 43 | + `, |
| 44 | + ); |
| 45 | + |
| 46 | + await harness.writeFile('src/locales/messages.fr.xlf', TRANSLATION_FILE_CONTENT); |
| 47 | + |
| 48 | + const buildCount = await harness |
| 49 | + .execute() |
| 50 | + .pipe( |
| 51 | + timeout(BUILD_TIMEOUT), |
| 52 | + concatMap(async ({ result }, index) => { |
| 53 | + expect(result?.success).toBe(true); |
| 54 | + |
| 55 | + switch (index) { |
| 56 | + case 0: { |
| 57 | + harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); |
| 58 | + |
| 59 | + // Trigger rebuild |
| 60 | + await harness.appendToFile('src/app/app.component.html', '\n\n'); |
| 61 | + break; |
| 62 | + } |
| 63 | + case 1: { |
| 64 | + harness.expectFile('dist/fr/main.js').content.toContain('Bonjour'); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + }), |
| 69 | + take(2), |
| 70 | + count(), |
| 71 | + ) |
| 72 | + .toPromise(); |
| 73 | + |
| 74 | + expect(buildCount).toBe(2); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +const TRANSLATION_FILE_CONTENT = ` |
| 80 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 81 | + <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> |
| 82 | + <file target-language="en-US" datatype="plaintext" original="ng2.template"> |
| 83 | + <body> |
| 84 | + <trans-unit id="4286451273117902052" datatype="html"> |
| 85 | + <target>Bonjour <x id="INTERPOLATION" equiv-text="{{ title }}"/>! </target> |
| 86 | + <context-group purpose="location"> |
| 87 | + <context context-type="targetfile">src/app/app.component.html</context> |
| 88 | + <context context-type="linenumber">2,3</context> |
| 89 | + </context-group> |
| 90 | + <note priority="1" from="description">An introduction header for this sample</note> |
| 91 | + </trans-unit> |
| 92 | + </body> |
| 93 | + </file> |
| 94 | + </xliff> |
| 95 | +`; |
0 commit comments