|
| 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 { EmptyTree } from '@angular-devkit/schematics'; |
| 10 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 11 | +import { ProjectType } from '../../utility/workspace-models'; |
| 12 | + |
| 13 | +function createWorkSpaceConfig(tree: UnitTestTree) { |
| 14 | + const angularConfig = { |
| 15 | + version: 1, |
| 16 | + projects: { |
| 17 | + app: { |
| 18 | + root: '/project/app', |
| 19 | + sourceRoot: '/project/app/src', |
| 20 | + projectType: ProjectType.Application, |
| 21 | + prefix: 'app', |
| 22 | + architect: { |
| 23 | + build: { |
| 24 | + builder: '@angular/build:application', |
| 25 | + options: { |
| 26 | + localize: true, |
| 27 | + polyfills: [], |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 36 | +} |
| 37 | + |
| 38 | +describe(`Migration to update the workspace configuration`, () => { |
| 39 | + const schematicName = 'update-workspace-config'; |
| 40 | + const schematicRunner = new SchematicTestRunner( |
| 41 | + 'migrations', |
| 42 | + require.resolve('../migration-collection.json'), |
| 43 | + ); |
| 44 | + |
| 45 | + let tree: UnitTestTree; |
| 46 | + beforeEach(() => { |
| 47 | + tree = new UnitTestTree(new EmptyTree()); |
| 48 | + createWorkSpaceConfig(tree); |
| 49 | + }); |
| 50 | + |
| 51 | + it(`should add '@angular/localize/init' to polyfills if localize is enabled`, async () => { |
| 52 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 53 | + const { |
| 54 | + projects: { app }, |
| 55 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 56 | + } = newTree.readJson('/angular.json') as any; |
| 57 | + |
| 58 | + expect(app.architect.build.options.polyfills).toContain('@angular/localize/init'); |
| 59 | + }); |
| 60 | + |
| 61 | + it(`should not add '@angular/localize/init' to polyfills if it already exists`, async () => { |
| 62 | + // Add '@angular/localize/init' manually |
| 63 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 64 | + const config = tree.readJson('/angular.json') as any; |
| 65 | + config.projects.app.architect.build.options.polyfills.push('@angular/localize/init'); |
| 66 | + tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); |
| 67 | + |
| 68 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 69 | + const { |
| 70 | + projects: { app }, |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 72 | + } = newTree.readJson('/angular.json') as any; |
| 73 | + |
| 74 | + const polyfills = app.architect.build.options.polyfills; |
| 75 | + expect(polyfills.filter((p: string) => p === '@angular/localize/init').length).toBe(1); |
| 76 | + }); |
| 77 | + |
| 78 | + it(`should not add polyfills if localize is not enabled`, async () => { |
| 79 | + // Disable 'localize' |
| 80 | + const config = JSON.parse(tree.readContent('/angular.json')); |
| 81 | + config.projects.app.architect.build.options.localize = false; |
| 82 | + tree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); |
| 83 | + |
| 84 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 85 | + const { |
| 86 | + projects: { app }, |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 88 | + } = newTree.readJson('/angular.json') as any; |
| 89 | + |
| 90 | + expect(app.architect.build.options.polyfills).not.toContain('@angular/localize/init'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments