|
| 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 { appendToFile, prependToFile, replaceInFile, writeFile } from '../../utils/fs'; |
| 10 | +import { ng } from '../../utils/process'; |
| 11 | +import { updateJsonFile } from '../../utils/project'; |
| 12 | + |
| 13 | +export default async function () { |
| 14 | + // Add app routing. |
| 15 | + // This is done automatically on a new app with --routing. |
| 16 | + await prependToFile('src/app/app.module.ts', `import { RouterModule } from '@angular/router';`); |
| 17 | + await replaceInFile( |
| 18 | + 'src/app/app.module.ts', |
| 19 | + `imports: [`, |
| 20 | + `imports: [ RouterModule.forRoot([]),`, |
| 21 | + ); |
| 22 | + await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>'); |
| 23 | + |
| 24 | + // Add lazy route. |
| 25 | + await ng('generate', 'module', 'lazy', '--route', 'lazy', '--module', 'app.module'); |
| 26 | + |
| 27 | + // Add lazy route e2e |
| 28 | + await writeFile( |
| 29 | + 'e2e/src/app.e2e-spec.ts', |
| 30 | + ` |
| 31 | + import { browser, logging, element, by } from 'protractor'; |
| 32 | +
|
| 33 | + describe('workspace-project App', () => { |
| 34 | + it('should display lazy route', async () => { |
| 35 | + await browser.get(browser.baseUrl + '/lazy'); |
| 36 | + expect(await element(by.css('app-lazy p')).getText()).toEqual('lazy works!'); |
| 37 | + }); |
| 38 | +
|
| 39 | + afterEach(async () => { |
| 40 | + // Assert that there are no errors emitted from the browser |
| 41 | + const logs = await browser.manage().logs().get(logging.Type.BROWSER); |
| 42 | + expect(logs).not.toContain(jasmine.objectContaining({ |
| 43 | + level: logging.Level.SEVERE, |
| 44 | + })); |
| 45 | + }); |
| 46 | + }); |
| 47 | + `, |
| 48 | + ); |
| 49 | + |
| 50 | + const testCases = [ |
| 51 | + { |
| 52 | + aot: false, |
| 53 | + csp: `trusted-types angular angular#unsafe-bypass angular#unsafe-jit angular#bundler; require-trusted-types-for 'script';`, |
| 54 | + }, |
| 55 | + { |
| 56 | + aot: true, |
| 57 | + csp: `trusted-types angular angular#unsafe-bypass angular#bundler; require-trusted-types-for 'script';`, |
| 58 | + }, |
| 59 | + ]; |
| 60 | + |
| 61 | + for (const { aot, csp } of testCases) { |
| 62 | + await updateJsonFile('angular.json', (json) => { |
| 63 | + const architect = json['projects']['test-project']['architect']; |
| 64 | + architect['build']['options']['aot'] = aot; |
| 65 | + if (!architect['serve']['options']) architect['serve']['options'] = {}; |
| 66 | + architect['serve']['options']['headers'] = { |
| 67 | + 'Content-Security-Policy': csp, |
| 68 | + }; |
| 69 | + }); |
| 70 | + |
| 71 | + try { |
| 72 | + await ng('e2e'); |
| 73 | + } catch (error) { |
| 74 | + console.error(`Test case AOT ${aot} with CSP header ${csp} failed.`); |
| 75 | + throw error; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments