|
| 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 { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 10 | +import { Schema as ApplicationOptions } from '../application/schema'; |
| 11 | +import { Schema as WorkspaceOptions } from '../workspace/schema'; |
| 12 | +import { Schema as EnvironmentOptions } from './schema'; |
| 13 | + |
| 14 | +describe('Application Schematic', () => { |
| 15 | + const schematicRunner = new SchematicTestRunner( |
| 16 | + '@schematics/angular', |
| 17 | + require.resolve('../collection.json'), |
| 18 | + ); |
| 19 | + |
| 20 | + const workspaceOptions: WorkspaceOptions = { |
| 21 | + name: 'workspace', |
| 22 | + newProjectRoot: 'projects', |
| 23 | + version: '15.0.0', |
| 24 | + }; |
| 25 | + |
| 26 | + const defaultOptions: EnvironmentOptions = { |
| 27 | + project: 'foo', |
| 28 | + }; |
| 29 | + |
| 30 | + const defaultAppOptions: ApplicationOptions = { |
| 31 | + name: 'foo', |
| 32 | + inlineStyle: true, |
| 33 | + inlineTemplate: true, |
| 34 | + routing: false, |
| 35 | + skipPackageJson: false, |
| 36 | + minimal: true, |
| 37 | + }; |
| 38 | + |
| 39 | + let applicationTree: UnitTestTree; |
| 40 | + const messages: string[] = []; |
| 41 | + schematicRunner.logger.subscribe((x) => messages.push(x.message)); |
| 42 | + |
| 43 | + function runEnvironmentsSchematic(): Promise<UnitTestTree> { |
| 44 | + return schematicRunner.runSchematic('environments', defaultOptions, applicationTree); |
| 45 | + } |
| 46 | + |
| 47 | + beforeEach(async () => { |
| 48 | + messages.length = 0; |
| 49 | + const workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions); |
| 50 | + applicationTree = await schematicRunner.runSchematic( |
| 51 | + 'application', |
| 52 | + defaultAppOptions, |
| 53 | + workspaceTree, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should create a default environment typescript file', async () => { |
| 58 | + const tree = await runEnvironmentsSchematic(); |
| 59 | + expect(tree.readText('projects/foo/src/environments/environment.ts')).toEqual( |
| 60 | + 'export const environment = {};\n', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should create a development configuration environment typescript file', async () => { |
| 65 | + const tree = await runEnvironmentsSchematic(); |
| 66 | + expect(tree.readText('projects/foo/src/environments/environment.development.ts')).toEqual( |
| 67 | + 'export const environment = {};\n', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should create environment typescript files for additional configurations', async () => { |
| 72 | + const initialWorkspace = JSON.parse(applicationTree.readContent('/angular.json')); |
| 73 | + initialWorkspace.projects.foo.architect.build.configurations.staging = {}; |
| 74 | + applicationTree.overwrite('/angular.json', JSON.stringify(initialWorkspace)); |
| 75 | + |
| 76 | + const tree = await runEnvironmentsSchematic(); |
| 77 | + expect(tree.readText('projects/foo/src/environments/environment.development.ts')).toEqual( |
| 78 | + 'export const environment = {};\n', |
| 79 | + ); |
| 80 | + |
| 81 | + expect(tree.readText('projects/foo/src/environments/environment.staging.ts')).toEqual( |
| 82 | + 'export const environment = {};\n', |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should update the angular.json file replacements option for the development configuration', async () => { |
| 87 | + const tree = await runEnvironmentsSchematic(); |
| 88 | + const workspace = JSON.parse(tree.readContent('/angular.json')); |
| 89 | + |
| 90 | + const developmentConfiguration = |
| 91 | + workspace.projects.foo.architect.build.configurations.development; |
| 92 | + expect(developmentConfiguration).toEqual( |
| 93 | + jasmine.objectContaining({ |
| 94 | + fileReplacements: [ |
| 95 | + { |
| 96 | + replace: 'projects/foo/src/environments/environment.ts', |
| 97 | + with: 'projects/foo/src/environments/environment.development.ts', |
| 98 | + }, |
| 99 | + ], |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should update the angular.json file replacements option for additional configurations', async () => { |
| 105 | + const initialWorkspace = JSON.parse(applicationTree.readContent('/angular.json')); |
| 106 | + initialWorkspace.projects.foo.architect.build.configurations.staging = {}; |
| 107 | + applicationTree.overwrite('/angular.json', JSON.stringify(initialWorkspace)); |
| 108 | + |
| 109 | + const tree = await runEnvironmentsSchematic(); |
| 110 | + const workspace = JSON.parse(tree.readContent('/angular.json')); |
| 111 | + |
| 112 | + const developmentConfiguration = |
| 113 | + workspace.projects.foo.architect.build.configurations.development; |
| 114 | + expect(developmentConfiguration).toEqual( |
| 115 | + jasmine.objectContaining({ |
| 116 | + fileReplacements: [ |
| 117 | + { |
| 118 | + replace: 'projects/foo/src/environments/environment.ts', |
| 119 | + with: 'projects/foo/src/environments/environment.development.ts', |
| 120 | + }, |
| 121 | + ], |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + const stagingConfiguration = workspace.projects.foo.architect.build.configurations.staging; |
| 126 | + expect(stagingConfiguration).toEqual( |
| 127 | + jasmine.objectContaining({ |
| 128 | + fileReplacements: [ |
| 129 | + { |
| 130 | + replace: 'projects/foo/src/environments/environment.ts', |
| 131 | + with: 'projects/foo/src/environments/environment.staging.ts', |
| 132 | + }, |
| 133 | + ], |
| 134 | + }), |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should update the angular.json file replacements option for server configurations', async () => { |
| 139 | + await schematicRunner.runSchematic( |
| 140 | + 'universal', |
| 141 | + { project: 'foo', skipInstall: true }, |
| 142 | + applicationTree, |
| 143 | + ); |
| 144 | + |
| 145 | + const tree = await runEnvironmentsSchematic(); |
| 146 | + const workspace = JSON.parse(tree.readContent('/angular.json')); |
| 147 | + |
| 148 | + const developmentConfiguration = |
| 149 | + workspace.projects.foo.architect.build.configurations.development; |
| 150 | + expect(developmentConfiguration).toEqual( |
| 151 | + jasmine.objectContaining({ |
| 152 | + fileReplacements: [ |
| 153 | + { |
| 154 | + replace: 'projects/foo/src/environments/environment.ts', |
| 155 | + with: 'projects/foo/src/environments/environment.development.ts', |
| 156 | + }, |
| 157 | + ], |
| 158 | + }), |
| 159 | + ); |
| 160 | + |
| 161 | + const serverDevelopmentConfiguration = |
| 162 | + workspace.projects.foo.architect.server.configurations.development; |
| 163 | + expect(serverDevelopmentConfiguration).toEqual( |
| 164 | + jasmine.objectContaining({ |
| 165 | + fileReplacements: [ |
| 166 | + { |
| 167 | + replace: 'projects/foo/src/environments/environment.ts', |
| 168 | + with: 'projects/foo/src/environments/environment.development.ts', |
| 169 | + }, |
| 170 | + ], |
| 171 | + }), |
| 172 | + ); |
| 173 | + }); |
| 174 | +}); |
0 commit comments