-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex_spec.ts
264 lines (208 loc) · 11.1 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
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
import { join } from 'path';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getFileContent } from '@schematics/angular/utility/test';
import { Schema as ModuleOptions } from './schema';
import { toNgModuleClassName } from '../../utils';
import {
createEmptySharedProject,
createEmptyNsOnlyProject,
isInModuleMetadata,
} from '../../test-utils';
import { DEFAULT_SHARED_EXTENSIONS } from '../utils';
describe('Module Schematic', () => {
const name = 'foo';
const project = 'test';
const moduleClassName = toNgModuleClassName(name);
const defaultOptions: ModuleOptions = {
project,
name,
};
const schematicRunner = new SchematicTestRunner(
'@nativescript/schematics',
join(__dirname, '../../collection.json'),
);
const getModulePath = (extension: string) => `/src/app/${name}/${name}.module${extension}.ts`;
const noExtensionModulePath = getModulePath('');
const nsModulePath = getModulePath(DEFAULT_SHARED_EXTENSIONS.ns);
const webModulePath = getModulePath(DEFAULT_SHARED_EXTENSIONS.web);
const commonFilePath = `/src/app/${name}/${name}.common.ts`;
const getRoutingModulePath = (extension: string) => `/src/app/${name}/${name}-routing.module${extension}.ts`;
const noExtensionRoutingModulePath = getRoutingModulePath('');
const nsRoutingModulePath = getRoutingModulePath(DEFAULT_SHARED_EXTENSIONS.ns);
const webRoutingModulePath = getRoutingModulePath(DEFAULT_SHARED_EXTENSIONS.web);
let appTree: UnitTestTree;
describe('when in ns-only project', () => {
beforeEach(() => {
appTree = createEmptyNsOnlyProject(project);
});
let tree: UnitTestTree;
describe('with default options', () => {
beforeEach(async () => {
tree = await schematicRunner.runSchematicAsync('module', defaultOptions, appTree).toPromise();
});
it('should create tns module file with no extension', () => {
expect(tree.exists(noExtensionModulePath)).toBeTruthy();
expect(getFileContent(tree, noExtensionModulePath)).toContain('NativeScriptCommonModule');
expect(getFileContent(tree, noExtensionModulePath)).toContain(`class ${moduleClassName}`);
});
it('should not create files with .tns extension', () => {
expect(tree.exists(nsModulePath)).toBeFalsy();
});
it('should not create a common file', () => {
expect(tree.exists(commonFilePath)).toBeFalsy();
});
it('should not have CommonModule imported', () => {
const content = getFileContent(tree, noExtensionModulePath);
expect(content).not.toMatch(`import { CommonModule } from '@angular/common'`);
expect(content).not.toMatch(isInModuleMetadata(moduleClassName, 'imports', 'CommonModule', true));
});
it('should have NativeScriptCommonModule imported', () => {
const content = getFileContent(tree, noExtensionModulePath);
expect(content).toMatch(`import { NativeScriptCommonModule } from '@nativescript/angular'`);
});
it('should have NO_ERRORS_SCHEMA imported', () => {
const content = getFileContent(tree, noExtensionModulePath);
expect(content).toMatch(/import { [^}]*NO_ERRORS_SCHEMA(.*)} from '@angular\/core';/);
});
it('should have NO_ERRORS_SCHEMA declared', () => {
const content = getFileContent(tree, noExtensionModulePath);
expect(content).toMatch(
isInModuleMetadata(moduleClassName, 'schemas', 'NO_ERRORS_SCHEMA', true));
});
});
xit('should respect passed extension', async () => {
const customExtension = '.mobile';
const options = { ...defaultOptions, routing: true, nsExtension: customExtension };
const testTree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const modulePath = getModulePath(customExtension);
expect(testTree.exists(modulePath)).toBeTruthy();
const routingModulePath = getRoutingModulePath(customExtension);
expect(testTree.exists(routingModulePath)).toBeTruthy();
});
it('should not have NativeScriptCommonModule imported if that is specified explicitly', async () => {
const options = { ...defaultOptions, commonModule: false };
const testTree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const content = getFileContent(testTree, noExtensionModulePath);
expect(content).not.toMatch(`import { NativeScriptCommonModule } from '@nativescript/angular'`);
});
it('should not have RouterModule imported in the routing module', async () => {
const options = { ...defaultOptions, routing: true };
const testTree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const content = getFileContent(testTree, noExtensionModulePath);
expect(content).not.toMatch(`import { RouterModule } from '@angular/router'`);
expect(content).not.toMatch(
isInModuleMetadata(moduleClassName, 'exports', 'RouterModule.forChild', true));
expect(content).not.toMatch(
isInModuleMetadata(moduleClassName, 'exports', 'RouterModule', true));
});
xit('should have NativeScriptRouterModule imported', async () => {
const options = { ...defaultOptions, routing: true };
const testTree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const content = getFileContent(testTree, noExtensionRoutingModulePath);
expect(content).toMatch(`import { NativeScriptRouterModule } from '@nativescript/angular'`);
});
});
describe('when in ns+web project', () => {
beforeEach(() => {
appTree = createEmptySharedProject(project);
});
describe('executing ns-only schematic', () => {
const nsOnlyOptions = { ...defaultOptions, nativescript: true, web: false };
it('should create ns module file', async () => {
const options = { ...nsOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.files).toContain(nsModulePath);
expect(getFileContent(tree, nsModulePath)).toContain('CommonModule');
expect(getFileContent(tree, nsModulePath)).toContain(`class ${moduleClassName}`);
});
it('should not create web module file', async () => {
const options = { ...nsOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(webModulePath)).toBeFalsy();
});
it('should not create a common file', async () => {
const options = { ...nsOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(commonFilePath)).toBeFalsy();
});
xit('should respect passed extension', async () => {
const customExtension = '.mobile';
const options = { ...nsOnlyOptions, nsExtension: customExtension, routing: true };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const modulePath = getModulePath(customExtension);
expect(tree.exists(modulePath)).toBeTruthy();
const routingModulePath = getRoutingModulePath(customExtension);
expect(tree.exists(routingModulePath)).toBeTruthy();
});
});
describe('executing web-only schematic', () => {
const webOnlyOptions = { ...defaultOptions, nativescript: false, web: true };
it('should create web module file', async () => {
const options = { ...webOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.files).toContain(webModulePath);
expect(getFileContent(tree, webModulePath)).toContain('CommonModule');
expect(getFileContent(tree, webModulePath)).toContain(`class ${moduleClassName}`);
});
it('should not create ns module file', async () => {
const options = { ...webOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(nsModulePath)).toBeFalsy();
});
it('should not create a common file', async () => {
const options = { ...webOnlyOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(commonFilePath)).toBeFalsy();
});
xit('should respect passed extension', async () => {
const customExtension = '.web';
const options = { ...webOnlyOptions, webExtension: customExtension, routing: true };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const modulePath = getModulePath(customExtension);
expect(tree.exists(modulePath)).toBeTruthy();
const routingModulePath = getRoutingModulePath(customExtension);
expect(tree.exists(routingModulePath)).toBeTruthy();
});
});
describe('executing web+ns schematic', () => {
const nsWebOptions = {
...defaultOptions,
nativescript: true,
web: true,
};
it('should generate both web and ns module files', async () => {
const options = { ...nsWebOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(nsModulePath)).toBeTruthy();
expect(tree.exists(webModulePath)).toBeTruthy();
});
it('should create a common file', async () => {
const options = { ...nsWebOptions };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(commonFilePath)).toBeTruthy();
});
it('should create both routing modules when routing flag is passed', async () => {
const options = { ...nsWebOptions, routing: true };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
expect(tree.exists(nsRoutingModulePath)).toBeTruthy();
expect(tree.exists(webRoutingModulePath)).toBeTruthy();
});
xit('should respect passed extension', async () => {
const nsExtension = '.mobile';
const webExtension = '.web';
const options = { ...nsWebOptions, nsExtension, webExtension, routing: true };
const tree = await schematicRunner.runSchematicAsync('module', options, appTree).toPromise();
const customWebModulePath = getModulePath(webExtension);
const customNsModulePath = getModulePath(nsExtension);
expect(tree.exists(customWebModulePath)).toBeTruthy();
expect(tree.exists(customNsModulePath)).toBeTruthy();
expect(tree.exists(customWebModulePath)).toBeTruthy();
expect(tree.exists(customNsModulePath)).toBeTruthy();
const customWebRoutingModulePath = getRoutingModulePath(webExtension);
const customNsRoutingModulePath = getRoutingModulePath(nsExtension);
expect(tree.exists(customWebRoutingModulePath)).toBeTruthy();
expect(tree.exists(customNsRoutingModulePath)).toBeTruthy();
});
});
});
});