|
| 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 { EmptyTree } from '@angular-devkit/schematics'; |
| 10 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 11 | +import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models'; |
| 12 | + |
| 13 | +describe(`Migration to replace 'defaultCollection' option.`, () => { |
| 14 | + const schematicName = 'replace-default-collection-option'; |
| 15 | + const schematicRunner = new SchematicTestRunner( |
| 16 | + 'migrations', |
| 17 | + require.resolve('../migration-collection.json'), |
| 18 | + ); |
| 19 | + |
| 20 | + let tree: UnitTestTree; |
| 21 | + beforeEach(() => { |
| 22 | + tree = new UnitTestTree(new EmptyTree()); |
| 23 | + }); |
| 24 | + |
| 25 | + it(`should replace 'defaultCollection' with 'schematicCollections' at the root level`, async () => { |
| 26 | + const angularConfig = { |
| 27 | + version: 1, |
| 28 | + projects: {}, |
| 29 | + cli: { |
| 30 | + defaultCollection: 'foo', |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 35 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 36 | + const { cli } = JSON.parse(newTree.readContent('/angular.json')); |
| 37 | + |
| 38 | + expect(cli.defaultCollection).toBeUndefined(); |
| 39 | + expect(cli.schematicCollections).toEqual(['foo']); |
| 40 | + }); |
| 41 | + |
| 42 | + it(`should not error when 'cli' is not defined`, async () => { |
| 43 | + const angularConfig: WorkspaceSchema = { |
| 44 | + version: 1, |
| 45 | + projects: {}, |
| 46 | + }; |
| 47 | + |
| 48 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 49 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 50 | + const { cli } = JSON.parse(newTree.readContent('/angular.json')); |
| 51 | + |
| 52 | + expect(cli).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it(`should replace 'defaultCollection' with 'schematicCollections' at the project level`, async () => { |
| 56 | + const angularConfig = { |
| 57 | + version: 1, |
| 58 | + cli: { |
| 59 | + defaultCollection: 'foo', |
| 60 | + }, |
| 61 | + projects: { |
| 62 | + test: { |
| 63 | + sourceRoot: '', |
| 64 | + root: '', |
| 65 | + prefix: '', |
| 66 | + projectType: ProjectType.Application, |
| 67 | + cli: { |
| 68 | + defaultCollection: 'bar', |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }; |
| 73 | + |
| 74 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 75 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 76 | + const { |
| 77 | + projects: { test }, |
| 78 | + } = JSON.parse(newTree.readContent('/angular.json')); |
| 79 | + |
| 80 | + expect(test.cli.defaultCollection).toBeUndefined(); |
| 81 | + expect(test.cli.schematicCollections).toEqual(['bar']); |
| 82 | + }); |
| 83 | + |
| 84 | + it(`should not replace 'defaultCollection' with 'schematicCollections', when it is already defined`, async () => { |
| 85 | + const angularConfig = { |
| 86 | + version: 1, |
| 87 | + projects: {}, |
| 88 | + cli: { |
| 89 | + defaultCollection: 'foo', |
| 90 | + schematicCollections: ['bar'], |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 95 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 96 | + const { cli } = JSON.parse(newTree.readContent('/angular.json')); |
| 97 | + |
| 98 | + expect(cli.defaultCollection).toBeUndefined(); |
| 99 | + expect(cli.schematicCollections).toEqual(['bar']); |
| 100 | + }); |
| 101 | +}); |
0 commit comments