forked from angular/angular
/
index_spec.ts
194 lines (180 loc) · 8.01 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
/**
* @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.io/license
*/
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {localizePolyfill} from './index';
describe('ng-add schematic', () => {
const countInstances = (str: string, substr: string) => str.split(substr).length - 1;
const defaultOptions = {name: 'demo'};
let host: UnitTestTree;
let schematicRunner: SchematicTestRunner;
// The real polyfills file is bigger than this, but for the test it shouldn't matter.
const polyfillsContent =
`/***************************************************************************************************
* Zone JS is required by default for Angular itself.
*/
import 'zone.js/dist/zone';`;
const mainServerContent = `import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
export { AppServerModule } from './app/app.server.module';
export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
beforeEach(() => {
host = new UnitTestTree(new HostTree());
host.create('package.json', JSON.stringify({
'devDependencies': {
// The default (according to `ng-add` in its package.json) is for `@angular/localize` to be
// saved to `devDependencies`.
'@angular/localize': '~0.0.0-PLACEHOLDER',
}
}));
host.create('src/polyfills.ts', polyfillsContent);
host.create('src/another-polyfills.ts', polyfillsContent);
host.create('src/unrelated-polyfills.ts', polyfillsContent);
host.create('src/another-unrelated-polyfills.ts', polyfillsContent);
host.create('src/main.server.ts', mainServerContent);
host.create('src/another-main.server.ts', mainServerContent);
host.create('src/unrelated-main.server.ts', mainServerContent);
host.create('src/another-unrelated-main.server.ts', mainServerContent);
host.create('angular.json', JSON.stringify({
version: 1,
projects: {
'demo': {
architect: {
build: {
builder: '@angular-devkit/build-angular:browser',
options: {
polyfills: 'src/polyfills.ts',
},
configurations: {
production: {
polyfills: 'src/another-polyfills.ts',
}
}
},
'another-build': {
builder: '@angular-devkit/build-angular:browser',
options: {
polyfills: 'src/polyfills.ts',
},
configurations: {
production: {
polyfills: 'src/another-polyfills.ts',
}
}
},
server: {
builder: '@angular-devkit/build-angular:server',
options: {
main: 'src/main.server.ts',
},
configurations: {
production: {
main: 'src/another-main.server.ts',
}
}
},
'another-server': {
builder: '@angular-devkit/build-angular:server',
options: {
main: 'src/main.server.ts',
},
configurations: {
production: {
main: 'src/another-main.server.ts',
}
}
},
'not-browser-or-server': {
builder: '@angular-devkit/build-angular:something-else',
options: {
polyfills: 'src/unrelated-polyfills.ts',
main: 'src/unrelated-main.server.ts',
},
configurations: {
production: {
polyfills: 'src/other-unrelated-polyfills.ts',
main: 'src/another-unrelated-main.server.ts',
}
}
},
},
}
},
defaultProject: 'demo',
}));
schematicRunner =
new SchematicTestRunner('@angular/localize', require.resolve('../collection.json'));
});
it('should add localize polyfill to polyfill files', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.readContent('/src/polyfills.ts')).toContain(localizePolyfill);
expect(host.readContent('/src/another-polyfills.ts')).toContain(localizePolyfill);
});
it('should add localize polyfill to server main files', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.readContent('/src/main.server.ts')).toContain(localizePolyfill);
expect(host.readContent('/src/another-main.server.ts')).toContain(localizePolyfill);
});
it('should add localize polyfill at the start of file', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const content = host.readContent('/src/polyfills.ts');
expect(content.indexOf(localizePolyfill)).toBeLessThan(content.indexOf(polyfillsContent));
});
it('should not add localize polyfill to files referenced in other targets files', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.readContent('/src/unrelated-polyfills.ts')).not.toContain(localizePolyfill);
expect(host.readContent('/src/another-unrelated-polyfills.ts')).not.toContain(localizePolyfill);
expect(host.readContent('/src/unrelated-main.server.ts')).not.toContain(localizePolyfill);
expect(host.readContent('/src/another-unrelated-main.server.ts'))
.not.toContain(localizePolyfill);
});
it('should only add localize polyfill once if multiple builds reference it', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const content = host.readContent('/src/polyfills.ts');
expect(countInstances(content, localizePolyfill)).toBe(1);
});
it('should not add localize polyfill if it\'s already there', async () => {
const polyfillVariation = localizePolyfill.replace(/'/g, '"');
host.overwrite('/src/polyfills.ts', `${localizePolyfill}\n${polyfillsContent}`);
host.overwrite('/src/another-polyfills.ts', `${polyfillVariation}\n${polyfillsContent}`);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(countInstances(host.readContent('/src/polyfills.ts'), localizePolyfill)).toBe(1);
expect(countInstances(host.readContent('/src/another-polyfills.ts'), localizePolyfill)).toBe(0);
});
it('should not break when there are no polyfills', async () => {
host.overwrite('angular.json', JSON.stringify({
version: 1,
projects: {
'demo': {
architect: {},
}
},
defaultProject: 'demo',
}));
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
});
it('should add package to `devDependencies` by default', async () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const packageJsonText = host.readContent('/package.json');
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize'])
.toBe('~0.0.0-PLACEHOLDER');
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize']).toBeUndefined();
});
it('should add package to `dependencies` if `useAtRuntime` is `true`', async () => {
host = await schematicRunner
.runSchematicAsync('ng-add', {...defaultOptions, useAtRuntime: true}, host)
.toPromise();
const packageJsonText = host.readContent('/package.json');
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize'])
.toBe('~0.0.0-PLACEHOLDER');
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize']).toBeUndefined();
});
});