-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
entry_components_spec.ts
182 lines (148 loc) Β· 4.8 KB
/
entry_components_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
/**
* @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 {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import * as shx from 'shelljs';
describe('entryComponents migration', () => {
let runner: SchematicTestRunner;
let host: TempScopedNodeJsSyncHost;
let tree: UnitTestTree;
let tmpDirPath: string;
let previousWorkingDir: string;
beforeEach(() => {
runner = new SchematicTestRunner('test', require.resolve('../migrations.json'));
host = new TempScopedNodeJsSyncHost();
tree = new UnitTestTree(new HostTree(host));
writeFile('/tsconfig.json', JSON.stringify({
compilerOptions: {
lib: ['es2015'],
strictNullChecks: true,
},
}));
writeFile('/angular.json', JSON.stringify({
version: 1,
projects: {t: {root: '', architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);
// Switch into the temporary directory path. This allows us to run
// the schematic against our custom unit test tree.
shx.cd(tmpDirPath);
});
afterEach(() => {
shx.cd(previousWorkingDir);
shx.rm('-r', tmpDirPath);
});
it('should remove `entryComponents` usages from NgModule', async () => {
writeFile('/index.ts', `
import { NgModule, Component } from '@angular/core';
@Component({selector: 'my-comp', template: ''})
export class MyComp {}
@NgModule({
declarations: [MyComp],
entryComponents: [MyComp],
exports: [MyComp]
})
export class MyModule {}
`);
await runMigration();
expect(stripWhitespace(tree.readContent('/index.ts'))).toContain(stripWhitespace(`
@NgModule({
declarations: [MyComp],
exports: [MyComp]
})
`));
});
it('should remove `entryComponents` usages from Component', async () => {
writeFile('/index.ts', `
import { Component } from '@angular/core';
@Component({selector: 'comp-a', template: ''})
export class CompA {}
@Component({
selector: 'comp-b',
entryComponents: [CompA],
template: ''
})
export class CompB {}
`);
await runMigration();
expect(stripWhitespace(tree.readContent('/index.ts'))).toContain(stripWhitespace(`
@Component({
selector: 'comp-b',
template: ''
})
`));
});
it('should remove multiple `entryComponents` usages from a single file', async () => {
writeFile('/index.ts', `
import { NgModule, Component } from '@angular/core';
@Component({selector: 'comp-a', template: ''})
export class CompA {}
@Component({
selector: 'comp-b',
entryComponents: [CompA],
template: ''
})
export class CompB {}
@NgModule({
declarations: [CompA, CompB],
entryComponents: [CompB],
exports: [CompA, CompB]
})
export class MyModule {}
`);
await runMigration();
const content = stripWhitespace(tree.readContent('/index.ts'));
expect(content).toContain(stripWhitespace(`
@Component({
selector: 'comp-b',
template: ''
})
`));
expect(content).toContain(stripWhitespace(`
@NgModule({
declarations: [CompA, CompB],
exports: [CompA, CompB]
})
`));
});
it('should not remove `entryComponents` usages from decorators that do not come from Angular',
async () => {
writeFile('/index.ts', `
import { Component } from '@angular/core';
import { NgModule } from '@not-angular/core';
@Component({selector: 'my-comp', template: ''})
export class MyComp {}
@NgModule({
declarations: [MyComp],
entryComponents: [MyComp],
exports: [MyComp]
})
export class MyModule {}
`);
await runMigration();
expect(stripWhitespace(tree.readContent('/index.ts'))).toContain(stripWhitespace(`
@NgModule({
declarations: [MyComp],
entryComponents: [MyComp],
exports: [MyComp]
})
`));
});
function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}
function runMigration() {
return runner.runSchematicAsync('migration-entry-components', {}, tree).toPromise();
}
function stripWhitespace(contents: string) {
return contents.replace(/\s/g, '');
}
});