-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
update-module-name.spec.ts
277 lines (239 loc) · 8.65 KB
/
update-module-name.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { moveGenerator } from '@nrwl/workspace';
import { Schema } from '../schema';
import { updateModuleName } from './update-module-name';
import libraryGenerator from '../../library/library';
import { Linter } from '@nrwl/linter';
import { UnitTestRunner } from '../../../utils/test-runners';
describe('updateModuleName Rule', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});
it('should handle nesting resulting in the same project name', async () => {
const updatedModulePath = '/libs/my/first/src/lib/my-first.module.ts';
await libraryGenerator(tree, {
name: 'my-first',
simpleModuleName: true,
});
const schema: Schema = {
projectName: 'my-first',
destination: 'my/first',
updateImportPath: true,
};
await moveGenerator(tree, schema);
updateModuleName(tree, { ...schema, destination: 'my/first' });
expect(tree.exists(updatedModulePath)).toBe(true);
const moduleFile = tree.read(updatedModulePath).toString('utf-8');
expect(moduleFile).toContain(`export class MyFirstModule { }`);
});
describe('move to subfolder', () => {
const updatedModulePath =
'/libs/shared/my-first/src/lib/shared-my-first.module.ts';
const updatedModuleSpecPath =
'/libs/shared/my-first/src/lib/shared-my-first.module.spec.ts';
const indexPath = '/libs/shared/my-first/src/index.ts';
const secondModulePath = '/libs/my-second/src/lib/my-second.module.ts';
const schema: Schema = {
projectName: 'my-first',
destination: 'shared/my-first',
updateImportPath: true,
};
beforeEach(async () => {
await libraryGenerator(tree, {
name: 'my-first',
buildable: false,
enableIvy: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
await libraryGenerator(tree, {
name: 'my-second',
buildable: false,
enableIvy: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
tree.write(
'/libs/my-first/src/lib/my-first.module.ts',
`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [CommonModule]
})
export class MyFirstModule {}`
);
tree.write(
'/libs/my-first/src/lib/my-first.module.spec.ts',
`import { async, TestBed } from '@angular/core/testing';
import { MyFirstModule } from './my-first.module';
describe('MyFirstModule', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MyFirstModule]
}).compileComponents();
}));
it('should create', () => {
expect(MyFirstModule).toBeDefined();
});
});`
);
tree.write(
secondModulePath,
`import { MyFirstModule } from '@proj/my-first';
export class MySecondModule extends MyFirstModule {}
`
);
await moveGenerator(tree, schema);
});
it('should rename the module files and update the module name', async () => {
updateModuleName(tree, schema);
expect(tree.exists(updatedModulePath)).toBe(true);
expect(tree.exists(updatedModuleSpecPath)).toBe(true);
const moduleFile = tree.read(updatedModulePath).toString('utf-8');
expect(moduleFile).toContain(`export class SharedMyFirstModule {}`);
const moduleSpecFile = tree.read(updatedModuleSpecPath).toString('utf-8');
expect(moduleSpecFile).toContain(
`import { SharedMyFirstModule } from './shared-my-first.module';`
);
expect(moduleSpecFile).toContain(
`describe('SharedMyFirstModule', () => {`
);
expect(moduleSpecFile).toContain(`imports: [SharedMyFirstModule]`);
expect(moduleSpecFile).toContain(
`expect(SharedMyFirstModule).toBeDefined();`
);
});
it('should update any references to the module', async () => {
updateModuleName(tree, schema);
const importerFile = tree.read(secondModulePath).toString('utf-8');
expect(importerFile).toContain(
`import { SharedMyFirstModule } from '@proj/shared-my-first';`
);
expect(importerFile).toContain(
`export class MySecondModule extends SharedMyFirstModule {}`
);
});
it('should update the index.ts file which exports the module', async () => {
updateModuleName(tree, schema);
const indexFile = tree.read(indexPath).toString('utf-8');
expect(indexFile).toContain(
`export * from './lib/shared-my-first.module';`
);
});
});
describe('rename', () => {
const schema: Schema = {
projectName: 'my-source',
destination: 'my-destination',
updateImportPath: true,
};
const modulePath = '/libs/my-destination/src/lib/my-destination.module.ts';
const moduleSpecPath =
'/libs/my-destination/src/lib/my-destination.module.spec.ts';
const indexPath = '/libs/my-destination/src/index.ts';
const importerPath = '/libs/my-importer/src/lib/my-importing-file.ts';
beforeEach(async () => {
// fake a mid-move tree:
await libraryGenerator(tree, {
name: 'my-destination',
buildable: false,
enableIvy: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
tree.write(
'/libs/my-destination/src/lib/my-source.module.ts',
`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [CommonModule]
})
export class MySourceModule {}`
);
tree.write(
'/libs/my-destination/src/lib/my-source.module.spec.ts',
`import { async, TestBed } from '@angular/core/testing';
import { MySourceModule } from './my-source.module';
describe('MySourceModule', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MySourceModule]
}).compileComponents();
}));
it('should create', () => {
expect(MySourceModule).toBeDefined();
});
});`
);
tree.write(
indexPath,
`export * from './lib/my-source.module';
`
);
tree.delete(modulePath);
tree.delete(moduleSpecPath);
await libraryGenerator(tree, {
name: 'my-importer',
buildable: false,
enableIvy: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
tree.write(
importerPath,
`import { MySourceModule } from '@proj/my-destination';
export class MyExtendedSourceModule extends MySourceModule {}
`
);
});
it('should rename the module files and update the module name', async () => {
updateModuleName(tree, schema);
expect(tree.exists(modulePath)).toBe(true);
expect(tree.exists(moduleSpecPath)).toBe(true);
const moduleFile = tree.read(modulePath).toString('utf-8');
expect(moduleFile).toContain(`export class MyDestinationModule {}`);
const moduleSpecFile = tree.read(moduleSpecPath).toString('utf-8');
expect(moduleSpecFile).toContain(
`import { MyDestinationModule } from './my-destination.module';`
);
expect(moduleSpecFile).toContain(
`describe('MyDestinationModule', () => {`
);
expect(moduleSpecFile).toContain(`imports: [MyDestinationModule]`);
expect(moduleSpecFile).toContain(
`expect(MyDestinationModule).toBeDefined();`
);
});
it('should update any references to the module', async () => {
updateModuleName(tree, schema);
const importerFile = tree.read(importerPath).toString('utf-8');
expect(importerFile).toContain(
`import { MyDestinationModule } from '@proj/my-destination';`
);
expect(importerFile).toContain(
`export class MyExtendedSourceModule extends MyDestinationModule {}`
);
});
it('should update the index.ts file which exports the module', async () => {
updateModuleName(tree, schema);
const indexFile = tree.read(indexPath).toString('utf-8');
expect(indexFile).toContain(
`export * from './lib/my-destination.module';`
);
});
});
});