Skip to content

Commit 39ab56c

Browse files
committed
test: add unit tests for refactor-nsng-modules
1 parent 4b356cc commit 39ab56c

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

Diff for: src/refactor-nsng-modules/index_spec.ts

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import { getFileContent } from '@schematics/angular/utility/test';
3+
import { VirtualTree } from '@angular-devkit/schematics';
4+
import * as path from 'path';
5+
6+
import { isInModuleMetadata } from '../test-utils';
7+
import { Schema } from './schema';
8+
9+
describe('Refactor NsNg Modules Schematic', () => {
10+
const schematicRunner = new SchematicTestRunner(
11+
'nativescript-schematics',
12+
path.join(__dirname, '../collection.json'),
13+
);
14+
15+
const sourceDir = 'src';
16+
const defaultOptions: Schema = { sourceDir };
17+
18+
const rootModulePath = `${sourceDir}/app.module.ts`;
19+
const rootModuleContent = `
20+
import { NgModule } from "@angular/core";
21+
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
22+
23+
@NgModule({
24+
imports: [
25+
NativeScriptModule,
26+
],
27+
})
28+
export class AppModule { }
29+
`;
30+
31+
const initAppTree = () => {
32+
const appTree = new VirtualTree();
33+
appTree.create(`${sourceDir}/package.json`, `{ "main": "main.js" }`);
34+
appTree.create(`${sourceDir}/main.ts`, `
35+
import { platformNativeScriptDynamic } from 'nativescript-angular/platform';
36+
import { AppModule } from './app.module';
37+
38+
platformNativeScriptDynamic().bootstrapModule(AppModule);
39+
`);
40+
41+
appTree.create(rootModulePath, rootModuleContent);
42+
43+
return appTree;
44+
};
45+
46+
47+
describe('when no changes are required', () => {
48+
let tree;
49+
beforeEach(() => {
50+
const appTree = initAppTree();
51+
tree = schematicRunner.runSchematic('refactor-nsng-modules', defaultOptions, appTree);
52+
});
53+
54+
it('should not change the tree', () => {
55+
expect(tree.files.length).toEqual(3);
56+
expect(tree.exists(rootModulePath)).toEqual(true);
57+
expect(getFileContent(tree, rootModulePath)).toEqual(rootModuleContent);
58+
});
59+
});
60+
61+
describe('when a feature module has NativeScriptModule imported', () => {
62+
const featureModuleName = `LoginModule`;
63+
const featureModulePath = `${sourceDir}/feature.module.ts`;
64+
65+
let tree;
66+
let featureModuleContent;
67+
68+
beforeEach(() => {
69+
const appTree = initAppTree();
70+
appTree.create(featureModulePath, `
71+
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
72+
import { NativeScriptFormsModule } from "nativescript-angular/forms";
73+
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
74+
75+
import { loginRouting } from "./login.routing";
76+
import { LoginComponent } from "./login.component";
77+
78+
79+
@NgModule({
80+
imports: [
81+
NativeScriptFormsModule,
82+
NativeScriptModule,
83+
loginRouting
84+
],
85+
declarations: [
86+
LoginComponent
87+
],
88+
schemas: [NO_ERRORS_SCHEMA]
89+
})
90+
export class ${featureModuleName} { }
91+
`);
92+
93+
tree = schematicRunner.runSchematic('refactor-nsng-modules', defaultOptions, appTree);
94+
featureModuleContent = getFileContent(tree, featureModulePath);
95+
});
96+
97+
it('should remove the NativeScriptModule import', () => {
98+
expect(featureModuleContent).not.toMatch(`NativeScriptModule`);
99+
expect(featureModuleContent)
100+
.not.toMatch('import { NativeScriptModule } from "nativescript-angular/nativescript.module";'
101+
);
102+
});
103+
104+
it('should add the NativeScriptCommonModule to the module metadata', () => {
105+
expect(featureModuleContent)
106+
.toMatch(
107+
isInModuleMetadata(featureModuleName, 'imports', 'NativeScriptCommonModule', true)
108+
);
109+
});
110+
111+
it('should not change the root module', () => {
112+
expect(getFileContent(tree, rootModulePath)).toEqual(rootModuleContent);
113+
});
114+
});
115+
116+
describe('when a feature module has NativeScriptAnimationsModule imported', () => {
117+
const featureModuleName = `SomeModule`;
118+
const featureModulePath = `${sourceDir}/nested/dir/some.module.ts`;
119+
120+
let tree;
121+
let featureModuleContent;
122+
123+
beforeEach(() => {
124+
const appTree = initAppTree();
125+
appTree.create(featureModulePath, `
126+
import { NativeScriptAnimationsModule } from "nativescript-angular/animations";
127+
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
128+
129+
@NgModule({
130+
imports: [
131+
NativeScriptAnimationsModule,
132+
],
133+
schemas: [NO_ERRORS_SCHEMA]
134+
})
135+
export class ${featureModuleName} { }
136+
`);
137+
138+
tree = schematicRunner.runSchematic('refactor-nsng-modules', defaultOptions, appTree);
139+
featureModuleContent = getFileContent(tree, featureModulePath);
140+
});
141+
142+
it('should remove the NativeScriptAnimationsModule import', () => {
143+
expect(featureModuleContent).not.toMatch(`NativeScriptAnimationsModule`);
144+
expect(featureModuleContent)
145+
.not.toMatch('import { NativeScriptAnimationsModule } from "nativescript-angular/animations";'
146+
);
147+
});
148+
149+
it('should add the animations module to the root module', () => {
150+
const newRootModuleContent = getFileContent(tree, rootModulePath);
151+
expect(newRootModuleContent).toMatch(`NativeScriptAnimationsModule`);
152+
expect(newRootModuleContent)
153+
.toMatch('import { NativeScriptAnimationsModule } from "nativescript-angular/animations";'
154+
);
155+
});
156+
});
157+
158+
describe('when a feature module has both NativeScriptModule and NativeScriptAnimationsModule imported', () => {
159+
const featureModuleName = `FeatureModule`;
160+
const featureModulePath = `${sourceDir}/dir/feature-1.module.ts`;
161+
162+
let tree;
163+
let featureModuleContent;
164+
165+
beforeEach(() => {
166+
const appTree = initAppTree();
167+
appTree.create(featureModulePath, `
168+
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
169+
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
170+
import { NativeScriptAnimationsModule } from "nativescript-angular/animations";
171+
172+
@NgModule({
173+
imports: [
174+
NativeScriptModule,
175+
NativeScriptAnimationsModule,
176+
],
177+
schemas: [NO_ERRORS_SCHEMA]
178+
})
179+
export class ${featureModuleName} { }
180+
`);
181+
182+
tree = schematicRunner.runSchematic('refactor-nsng-modules', defaultOptions, appTree);
183+
featureModuleContent = getFileContent(tree, featureModulePath);
184+
});
185+
186+
it('should remove the NativeScriptAnimationsModule import', () => {
187+
expect(featureModuleContent).not.toMatch(`NativeScriptAnimationsModule`);
188+
expect(featureModuleContent)
189+
.not.toMatch('import { NativeScriptAnimationsModule } from "nativescript-angular/animations";'
190+
);
191+
});
192+
193+
it('should add the animations module to the root module', () => {
194+
const newRootModuleContent = getFileContent(tree, rootModulePath);
195+
expect(newRootModuleContent).toMatch(`NativeScriptAnimationsModule`);
196+
expect(newRootModuleContent)
197+
.toMatch('import { NativeScriptAnimationsModule } from "nativescript-angular/animations";'
198+
);
199+
});
200+
201+
it('should add the animations module to the root module', () => {
202+
const newRootModuleContent = getFileContent(tree, rootModulePath);
203+
expect(newRootModuleContent).toMatch(`NativeScriptAnimationsModule`);
204+
expect(newRootModuleContent)
205+
.toMatch('import { NativeScriptAnimationsModule } from "nativescript-angular/animations";'
206+
);
207+
});
208+
209+
it('should remove the NativeScriptModule import', () => {
210+
expect(featureModuleContent).not.toMatch(`NativeScriptModule`);
211+
expect(featureModuleContent)
212+
.not.toMatch('import { NativeScriptModule } from "nativescript-angular/nativescript.module";'
213+
);
214+
});
215+
216+
it('should import the NativeScriptCommonModule to the feature module', () => {
217+
expect(featureModuleContent).toMatch('import { NativeScriptCommonModule } from "nativescript-angular/common"');
218+
});
219+
220+
it('should add the NativeScriptCommonModule to the module metadata', () => {
221+
expect(featureModuleContent).toMatch('NativeScriptCommonModule');
222+
});
223+
224+
});
225+
});

0 commit comments

Comments
 (0)