-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex_spec.ts
156 lines (126 loc) · 5.79 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
import { join } from 'path';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { HostTree } from '@angular-devkit/schematics';
import { getFileContent } from '@schematics/angular/utility/test';
import { isInModuleMetadata } from '../test-utils';
import { moveToRoot } from '../utils';
import { findImports, getSourceFile } from '../ts-utils';
import { Schema as MigrateComponentOptions } from './schema';
describe('Migrate component schematic', () => {
const project = 'some-project';
const componentName = 'a';
const moduleName = 'b';
const componentClassName = 'AComponent';
const schematicRunner = new SchematicTestRunner(
'@nativescript/schematics',
join(__dirname, '../collection.json'),
);
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = new UnitTestTree(new HostTree());
appTree = await setupProject(appTree, schematicRunner, project);
});
describe('When the name of existing component is provided', () => {
const options: MigrateComponentOptions = {
project,
name: componentName,
style: true,
};
const htmlComponentPath = `/src/app/${componentName}/${componentName}.component.html`;
const xmlComponentPath = `/src/app/${componentName}/${componentName}.component.tns.html`;
beforeEach(async () => {
appTree = await schematicRunner.runSchematicAsync('component', {
name: componentName,
nativescript: false,
web: true,
project,
}, appTree)
.toPromise();
appTree = await schematicRunner.runSchematicAsync('migrate-component', options, appTree).toPromise();
});
it('should create an {N} markup file for the component', () => {
expect(appTree.files).toContain(xmlComponentPath);
});
it('should declare the component in the correct NgModule', () => {
const nsModulePath = `/src/app/app.module.tns.ts`;
const content = getFileContent(appTree, nsModulePath);
const matcher = isInModuleMetadata('AppModule', 'declarations', componentClassName, true);
expect(content).toMatch(matcher);
});
xit('should import the component in the correct NgModule using @src', () => {
const nsModulePath = `/src/app/app.module.tns.ts`;
const source = getSourceFile(appTree, nsModulePath);
const imports = findImports(componentClassName, source);
expect(imports.length).toEqual(1);
expect(imports[0].getFullText()).toContain(`@src/app/${componentName}/${componentName}.component`);
});
it('should put the original web template in the {N} markup file', () => {
const html = getFileContent(appTree, htmlComponentPath);
const xml = getFileContent(appTree, xmlComponentPath);
expect(xml.includes(html)).toBeTruthy();
});
});
describe('When component imported in another module is provided', () => {
const options: MigrateComponentOptions = {
project,
name: componentName,
module: moduleName,
style: true,
};
const htmlComponentPath = `/src/app/${componentName}/${componentName}.component.html`;
const xmlComponentPath = `/src/app/${componentName}/${componentName}.component.tns.html`;
beforeEach(async () => {
appTree = await schematicRunner.runSchematicAsync('module', {
project,
name: moduleName,
}, appTree)
.toPromise();
appTree = await schematicRunner.runSchematicAsync('component', {
project,
nativescript: false,
web: true,
name: componentName,
module: moduleName,
}, appTree)
.toPromise();
appTree = await schematicRunner.runSchematicAsync('migrate-component', { ...options }, appTree).toPromise();
});
it('should create an {N} markup file for the component', () => {
expect(appTree.files).toContain(xmlComponentPath);
});
it('should declare the component in the correct NgModule', () => {
const nsModulePath = `/src/app/${moduleName}/${moduleName}.module.tns.ts`;
const content = getFileContent(appTree, nsModulePath);
const matcher = isInModuleMetadata('BModule', 'declarations', componentClassName, true);
expect(content).toMatch(matcher);
});
xit('should import the component in the correct NgModule using @src', () => {
const nsModulePath = `/src/app/${moduleName}/${moduleName}.module.tns.ts`;
const source = getSourceFile(appTree, nsModulePath);
const imports = findImports(componentClassName, source);
expect(imports.length).toEqual(1);
expect(imports[0].getFullText()).toContain(`@src/app/${componentName}/${componentName}.component`);
});
it('should put the original web template in the {N} markup file', () => {
const html = getFileContent(appTree, htmlComponentPath);
const xml = getFileContent(appTree, xmlComponentPath);
expect(xml.includes(html)).toBeTruthy();
});
});
});
const setupProject = async (
appTree: UnitTestTree,
schematicRunner: SchematicTestRunner,
project: string,
) => {
appTree = await schematicRunner.runSchematicAsync('shared', {
name: project,
prefix: '',
sourceDir: 'src',
style: 'css',
theme: true,
sample: false,
}, appTree).toPromise();
appTree = moveToRoot<UnitTestTree>(schematicRunner, appTree, project);
return appTree;
};