forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_spec.ts
195 lines (167 loc) · 6.46 KB
/
index_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { Builders } from '../utility/workspace-models';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as EnvironmentOptions } from './schema';
describe('Environments Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '15.0.0',
};
const defaultOptions: EnvironmentOptions = {
project: 'foo',
};
const defaultAppOptions: ApplicationOptions = {
name: 'foo',
inlineStyle: true,
inlineTemplate: true,
routing: false,
skipPackageJson: false,
minimal: true,
};
let applicationTree: UnitTestTree;
function runEnvironmentsSchematic(): Promise<UnitTestTree> {
return schematicRunner.runSchematic('environments', defaultOptions, applicationTree);
}
function convertBuilderToLegacyBrowser(): void {
const config = JSON.parse(applicationTree.readContent('/angular.json'));
const build = config.projects.foo.architect.build;
build.builder = Builders.Browser;
build.options = {
...build.options,
main: build.options.browser,
browser: undefined,
};
build.configurations.development = {
...build.configurations.development,
vendorChunk: true,
namedChunks: true,
buildOptimizer: false,
};
applicationTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
}
beforeEach(async () => {
const workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
applicationTree = await schematicRunner.runSchematic(
'application',
defaultAppOptions,
workspaceTree,
);
});
it('should create a default environment typescript file', async () => {
const tree = await runEnvironmentsSchematic();
expect(tree.readText('projects/foo/src/environments/environment.ts')).toEqual(
'export const environment = {};\n',
);
});
it('should create a development configuration environment typescript file', async () => {
const tree = await runEnvironmentsSchematic();
expect(tree.readText('projects/foo/src/environments/environment.development.ts')).toEqual(
'export const environment = {};\n',
);
});
it('should create environment typescript files for additional configurations', async () => {
const initialWorkspace = JSON.parse(applicationTree.readContent('/angular.json'));
initialWorkspace.projects.foo.architect.build.configurations.staging = {};
applicationTree.overwrite('/angular.json', JSON.stringify(initialWorkspace));
const tree = await runEnvironmentsSchematic();
expect(tree.readText('projects/foo/src/environments/environment.development.ts')).toEqual(
'export const environment = {};\n',
);
expect(tree.readText('projects/foo/src/environments/environment.staging.ts')).toEqual(
'export const environment = {};\n',
);
});
it('should update the angular.json file replacements option for the development configuration', async () => {
const tree = await runEnvironmentsSchematic();
const workspace = JSON.parse(tree.readContent('/angular.json'));
const developmentConfiguration =
workspace.projects.foo.architect.build.configurations.development;
expect(developmentConfiguration).toEqual(
jasmine.objectContaining({
fileReplacements: [
{
replace: 'projects/foo/src/environments/environment.ts',
with: 'projects/foo/src/environments/environment.development.ts',
},
],
}),
);
});
it('should update the angular.json file replacements option for additional configurations', async () => {
const initialWorkspace = JSON.parse(applicationTree.readContent('/angular.json'));
initialWorkspace.projects.foo.architect.build.configurations.staging = {};
applicationTree.overwrite('/angular.json', JSON.stringify(initialWorkspace));
const tree = await runEnvironmentsSchematic();
const workspace = JSON.parse(tree.readContent('/angular.json'));
const developmentConfiguration =
workspace.projects.foo.architect.build.configurations.development;
expect(developmentConfiguration).toEqual(
jasmine.objectContaining({
fileReplacements: [
{
replace: 'projects/foo/src/environments/environment.ts',
with: 'projects/foo/src/environments/environment.development.ts',
},
],
}),
);
const stagingConfiguration = workspace.projects.foo.architect.build.configurations.staging;
expect(stagingConfiguration).toEqual(
jasmine.objectContaining({
fileReplacements: [
{
replace: 'projects/foo/src/environments/environment.ts',
with: 'projects/foo/src/environments/environment.staging.ts',
},
],
}),
);
});
it('should update the angular.json file replacements option for server configurations', async () => {
convertBuilderToLegacyBrowser();
await schematicRunner.runSchematic(
'server',
{ project: 'foo', skipInstall: true },
applicationTree,
);
const tree = await runEnvironmentsSchematic();
const workspace = JSON.parse(tree.readContent('/angular.json'));
const developmentConfiguration =
workspace.projects.foo.architect.build.configurations.development;
expect(developmentConfiguration).toEqual(
jasmine.objectContaining({
fileReplacements: [
{
replace: 'projects/foo/src/environments/environment.ts',
with: 'projects/foo/src/environments/environment.development.ts',
},
],
}),
);
const serverDevelopmentConfiguration =
workspace.projects.foo.architect.server.configurations.development;
expect(serverDevelopmentConfiguration).toEqual(
jasmine.objectContaining({
fileReplacements: [
{
replace: 'projects/foo/src/environments/environment.ts',
with: 'projects/foo/src/environments/environment.development.ts',
},
],
}),
);
});
});