-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex_spec.ts
More file actions
405 lines (314 loc) · 17.7 KB
/
Copy pathindex_spec.ts
File metadata and controls
405 lines (314 loc) · 17.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { join } from 'path';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getFileContent } from '@schematics/angular/utility/test';
import { toComponentClassName } from '../../utils';
import {
createEmptyNsOnlyProject,
createEmptySharedProject,
isInModuleMetadata,
} from '../../test-utils';
import { DEFAULT_SHARED_EXTENSIONS } from '../utils';
import { Schema as ComponentOptions } from './schema';
import { findImports, getSourceFile } from '../../ts-utils';
describe('Component Schematic', () => {
const name = 'foo';
const project = 'leproj';
const importPrefix = '@src';
const componentClassName = toComponentClassName(name);
const defaultOptions: ComponentOptions = { name, project };
const schematicRunner = new SchematicTestRunner(
'@nativescript/schematics',
join(__dirname, '../../collection.json'),
);
const rootModulePath = `src/app/app.module.ts`;
const rootNsModulePath = `src/app/app.module.tns.ts`;
const getTemplatePath = (extension: string) => `src/app/${name}/${name}.component${extension}.html`;
const noExtensionTemplatePath = getTemplatePath('');
const nsTemplatePath = getTemplatePath(DEFAULT_SHARED_EXTENSIONS.ns);
const webTemplatePath = getTemplatePath(DEFAULT_SHARED_EXTENSIONS.web);
const getStylesheetPath = (extension: string, style: string = 'css') =>
`src/app/${name}/${name}.component${extension}.${style}`;
const noExtensionStylesheetPath = getStylesheetPath('');
const nsStylesheetPath = getStylesheetPath(DEFAULT_SHARED_EXTENSIONS.ns);
const webStylesheetPath = getStylesheetPath(DEFAULT_SHARED_EXTENSIONS.web);
let appTree: UnitTestTree;
const ensureWebTemplate = (tree: UnitTestTree, path: string) => {
expect(tree.exists(path)).toBeTruthy();
const content = getFileContent(tree, webTemplatePath);
expect(content).toMatch(/\<p\>/);
};
const ensureNsTemplate = (tree: UnitTestTree, path: string) => {
expect(tree.exists(path)).toBeTruthy();
const content = getFileContent(tree, path);
expect(content).toMatch(/Button/);
};
describe('when in ns-only project', () => {
beforeAll(async () => {
appTree = createEmptyNsOnlyProject(project);
const options = { ...defaultOptions, name, nativescript: true, web: false };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
});
it('should create template without extension', () =>
expect(appTree.exists(noExtensionTemplatePath)).toBeTruthy());
it('should not create template with {N} extension', () =>
expect(appTree.exists(nsTemplatePath)).toBeFalsy());
it('should add {N}-specific markup in template', () => ensureNsTemplate(appTree, noExtensionTemplatePath));
it('should create stylesheet without extension', () =>
expect(appTree.exists(noExtensionStylesheetPath)).toBeTruthy());
it('should not create stylesheet with {N} extension', () =>
expect(appTree.exists(nsStylesheetPath)).toBeFalsy());
it('should declare the component in the root NgModule for {N}', () => {
const nsModuleContent = getFileContent(appTree, rootModulePath);
expect(nsModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
});
describe('when in ns-only project, create a component in /home folder', () => {
const homeDir = 'home';
const componentName = `${homeDir}/${name}`;
const componentPath = `src/app/${homeDir}/${name}/${name}.component.ts`;
const templatePath = `src/app/${homeDir}/${name}/${name}.component.html`;
beforeAll(async () => {
appTree = createEmptyNsOnlyProject(project);
const options = { ...defaultOptions, name: componentName, nativescript: true, web: false };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
});
it('should create component in the home folder', () =>
expect(appTree.exists(componentPath)).toBeTruthy());
it('should create template in the home folder', () =>
expect(appTree.exists(templatePath)).toBeTruthy());
it(
'should declare the component in the root NgModule for {N} using name FooComponent with the import to home/foo',
() => {
const nsModuleContent = getFileContent(appTree, rootModulePath);
expect(nsModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
},
);
});
describe('when in ns+web project', () => {
describe('executing ns+web schematic', () => {
beforeAll(async () => {
appTree = createEmptySharedProject(project);
const options = { ...defaultOptions, name, web: true, nativescript: true };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
});
it('should add web-specific markup file', () => ensureWebTemplate(appTree, webTemplatePath));
it('should add {N}-specific markup file', () => ensureNsTemplate(appTree, nsTemplatePath));
it('should add web-specific stylesheet file', () =>
expect(appTree.exists(webStylesheetPath)).toBeTruthy());
it('should add {N}-specific stylesheet file', () =>
expect(appTree.exists(nsStylesheetPath)).toBeTruthy());
it('should declare the component in the the root NgModule for web', () => {
const webModuleContent = getFileContent(appTree, rootModulePath);
expect(webModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
it('should declare the component in the root NgModule for {N}', () => {
const nsModuleContent = getFileContent(appTree, rootNsModulePath);
expect(nsModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
it('should import the component in the root NgModule for {N} using @src', () => {
const source = getSourceFile(appTree, rootModulePath);
const imports = findImports(componentClassName, source);
expect(imports.length).toEqual(1);
expect(imports[0].getFullText()).toContain(`${importPrefix}/app/${name}/${name}.component`);
});
it('should import the component in the root NgModule for {N} using @src', () => {
const source = getSourceFile(appTree, rootNsModulePath);
const imports = findImports(componentClassName, source);
expect(imports.length).toEqual(1);
expect(imports[0].getFullText()).toContain(`${importPrefix}/app/${name}/${name}.component`);
});
});
describe('executing ns-only schematic', () => {
beforeAll(async () => {
appTree = createEmptySharedProject(project);
const options = { ...defaultOptions, web: false, nativescript: true };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
});
it('should add {N}-specific markup file', () => ensureNsTemplate(appTree, nsTemplatePath));
it('should add {N}-specific stylesheet file', () =>
expect(appTree.exists(nsStylesheetPath)).toBeTruthy());
it('should not declare the component in the the root NgModule for web', () => {
const webModuleContent = getFileContent(appTree, rootModulePath);
expect(webModuleContent).not.toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
it('should declare the component in the root NgModule for {N}', () => {
const nsModuleContent = getFileContent(appTree, rootNsModulePath);
expect(nsModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
});
describe('executing web-only schematic', () => {
beforeAll(async () => {
appTree = createEmptySharedProject(project);
const options = { ...defaultOptions, web: true, nativescript: false };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
});
it('should add web-specific markup file', () => ensureWebTemplate(appTree, webTemplatePath));
it('should declare the component in the the root NgModule for web', () => {
const webModuleContent = getFileContent(appTree, rootModulePath);
expect(webModuleContent).toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
it('should not declare the component in the root NgModule for {N}', () => {
const nsModuleContent = getFileContent(appTree, rootNsModulePath);
expect(nsModuleContent).not.toMatch(isInModuleMetadata('AppModule', 'declarations', componentClassName, true));
});
});
});
describe('specifying custom extension', () => {
describe('in ns only project', () => {
const customExtension = '.mobile';
beforeEach(() => {
appTree = createEmptyNsOnlyProject(project, customExtension);
});
xit('should respect specified {N} extension', async () => {
const options = { ...defaultOptions, nsExtension: customExtension, nativescript: true };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const componentTemplatePath = getTemplatePath(customExtension);
expect(appTree.exists(componentTemplatePath)).toBeTruthy();
const componentStylesheetPath = getStylesheetPath(customExtension);
expect(appTree.exists(componentStylesheetPath)).toBeTruthy();
});
xit('should respect specified style extension', async () => {
const style = 'scss';
const options = { ...defaultOptions, nsExtension: customExtension, style, nativescript: true };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const componentStylesheetPath = getStylesheetPath(customExtension, style);
expect(appTree.exists(componentStylesheetPath)).toBeTruthy();
});
});
describe('in ns+web project', () => {
describe('when a custom web extension is specified', () => {
const customExtension = '.web';
const componentOptions = { ...defaultOptions, webExtension: customExtension, web: true };
beforeEach(() => {
appTree = createEmptySharedProject(project, customExtension, '.tns');
});
xit('should create the files with this extension', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const componentTemplatePath = getTemplatePath(customExtension);
expect(appTree.exists(componentTemplatePath)).toBeTruthy();
});
xit('should declare in NgModule', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const webModulePath = `src/app/app.module${customExtension}.ts`;
const nsModulePath = `src/app/app.module.tns.ts`;
const matcher = isInModuleMetadata('AppModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
xit('should respect the module option', async () => {
const moduleName = 'random';
const webModulePath = `src/app/${moduleName}/${moduleName}.module${customExtension}.ts`;
const nsModulePath = `src/app/${moduleName}/${moduleName}.module.tns.ts`;
appTree = await schematicRunner.runSchematicAsync('module', {
project,
name: moduleName,
webExtension: customExtension,
}, appTree).toPromise();
const options = { ...componentOptions, module: moduleName };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const matcher = isInModuleMetadata('RandomModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
});
describe('when a custom {N} extension is specified', () => {
const customExtension = '.mobile';
const componentOptions = { ...defaultOptions, nsExtension: customExtension, nativescript: true };
beforeEach(() => {
appTree = createEmptySharedProject(project, '', customExtension);
});
xit('should create the files with this extension', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const componentTemplatePath = getTemplatePath(customExtension);
expect(appTree.exists(componentTemplatePath)).toBeTruthy();
const componentStylesheetPath = getStylesheetPath(customExtension);
expect(appTree.exists(componentStylesheetPath)).toBeTruthy();
});
xit('should declare in NgModule', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const webModulePath = `src/app/app.module.ts`;
const nsModulePath = `src/app/app.module${customExtension}.ts`;
const matcher = isInModuleMetadata('AppModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
xit('should respect the module option', async () => {
const moduleName = 'random';
const webModulePath = `src/app/${moduleName}/${moduleName}.module.ts`;
const nsModulePath = `src/app/${moduleName}/${moduleName}.module${customExtension}.ts`;
appTree = await schematicRunner.runSchematicAsync('module', {
project,
name: moduleName,
nsExtension: customExtension,
}, appTree).toPromise();
const options = { ...componentOptions, module: moduleName };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const matcher = isInModuleMetadata('RandomModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
});
describe('when custom web and {N} extensions are specified', () => {
const nsExtension = '.mobile';
const webExtension = '.web';
const componentOptions = { ...defaultOptions, nsExtension, webExtension, web: true, nativescript: true };
beforeEach(() => {
appTree = createEmptySharedProject(project, webExtension, nsExtension);
});
xit('should create the files with these extensions', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const nsTemplate = getTemplatePath(nsExtension);
const webTemplate = getTemplatePath(webExtension);
expect(appTree.exists(nsTemplate)).toBeTruthy();
expect(appTree.exists(webTemplate)).toBeTruthy();
const nsStylesheet = getStylesheetPath(nsExtension);
const webStylesheet = getStylesheetPath(webExtension);
expect(appTree.exists(nsStylesheet)).toBeTruthy();
expect(appTree.exists(webStylesheet)).toBeTruthy();
});
xit('should declare in NgModule', async () => {
const options = { ...componentOptions };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const webModulePath = `src/app/app.module${webExtension}.ts`;
const nsModulePath = `src/app/app.module${nsExtension}.ts`;
const matcher = isInModuleMetadata('AppModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
xit('should respect the module option', async () => {
const moduleName = 'random';
const webModulePath = `src/app/${moduleName}/${moduleName}.module${webExtension}.ts`;
const nsModulePath = `src/app/${moduleName}/${moduleName}.module${nsExtension}.ts`;
appTree = await schematicRunner.runSchematicAsync('module', {
project,
name: moduleName,
webExtension,
nsExtension,
}, appTree).toPromise();
const options = { ...componentOptions, module: moduleName };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const matcher = isInModuleMetadata('RandomModule', 'declarations', componentClassName, true);
const webModuleContent = getFileContent(appTree, webModulePath);
expect(webModuleContent).toMatch(matcher);
const nsModuleContent = getFileContent(appTree, nsModulePath);
expect(nsModuleContent).toMatch(matcher);
});
});
});
});
});