-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex_spec.ts
313 lines (256 loc) · 13.3 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
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
import { resolve } from 'path';
import { HostTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as AddNsOptions } from './schema';
import { getFileContent } from '@schematics/angular/utility/test';
import * as stripJsonComments from 'strip-json-comments';
describe('Add {N} schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@nativescript/schematics',
resolve(__dirname, '../collection.json'),
);
const project = 'foo';
const defaultOptions: AddNsOptions = {
project,
nsExtension: 'tns',
webExtension: '',
sample: false,
skipAutoGeneratedComponent: false,
};
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = new UnitTestTree(new HostTree());
appTree = await setupProject(appTree, schematicRunner, project);
});
describe('when using the default options', () => {
beforeEach(async () => {
appTree = await schematicRunner.runSchematicAsync('add-ns', defaultOptions, appTree)
.toPromise();
});
it('should add dependency to NativeScript schematics', () => {
const configFile = '/angular.json';
expect(appTree.files).toContain(configFile);
const configFileContent = JSON.parse(stripJsonComments(getFileContent(appTree, configFile)));
expect(configFileContent.cli.defaultCollection).toBe('@nativescript/schematics');
});
xit('should add {N} specific files', () => {
const files = appTree.files;
expect(files).toContain('/nativescript.config.ts');
expect(files).toContain('/tsconfig.tns.json');
expect(files).toContain('/src/app.css');
expect(files).toContain('/src/main.tns.ts');
expect(files).toContain('/src/package.json');
expect(files).toContain('/src/app/app.module.tns.ts');
expect(files).toContain('/src/app/app.component.tns.ts');
expect(files).toContain('/src/app/app.component.tns.html');
});
it('should add native app resources', () => {
expect(appTree.files).toContain('/App_Resources/Android/app.gradle');
expect(appTree.files).toContain('/App_Resources/iOS/Info.plist');
});
it('should add {N} specifics to gitignore', () => {
const gitignorePath = '/.gitignore';
expect(appTree.files).toContain(gitignorePath);
const gitignore = getFileContent(appTree, gitignorePath);
expect(gitignore.includes('node_modules/')).toBeTruthy();
expect(gitignore.includes('platforms/')).toBeTruthy();
expect(gitignore.includes('hooks/')).toBeTruthy();
expect(gitignore.includes('src/**/*.js')).toBeTruthy();
});
it('should add all required dependencies to the package.json', () => {
const packageJsonPath = '/package.json';
expect(appTree.files).toContain(packageJsonPath);
const packageJson = JSON.parse(stripJsonComments(getFileContent(appTree, packageJsonPath)));
const { dependencies, devDependencies } = packageJson;
expect(dependencies).toBeDefined();
expect(dependencies['@nativescript/angular']).toBeDefined();
expect(dependencies['@nativescript/theme']).toBeDefined();
expect(dependencies['@nativescript/core']).toBeDefined();
expect(dependencies['reflect-metadata']).toBeDefined();
expect(devDependencies['@nativescript/webpack']).toBeDefined();
expect(devDependencies['@nativescript/tslint-rules']).toBeDefined();
});
it('should add run scripts to the package json', () => {
const packageJsonPath = '/package.json';
expect(appTree.files).toContain(packageJsonPath);
const packageJson = JSON.parse(stripJsonComments(getFileContent(appTree, packageJsonPath)));
const { scripts } = packageJson;
expect(scripts).toBeDefined();
expect(scripts.android).toEqual('ns run android --no-hmr');
expect(scripts.ios).toEqual('ns run ios --no-hmr');
// expect(scripts.ngcc).toEqual('ngcc --properties es2015 module main --first-only');
// expect(scripts.postinstall).toEqual('npm run ngcc');
});
// it('should add NativeScript key to the package json', () => {
// const packageJsonPath = '/package.json';
// expect(appTree.files).toContain(packageJsonPath);
// const packageJson = JSON.parse(stripJsonComments(getFileContent(appTree, packageJsonPath)));
// const { nativescript } = packageJson;
// expect(nativescript).toBeDefined();
// expect(nativescript.id).toEqual('org.nativescript.ngsample');
// });
it('should modify the tsconfig.app.json (web) to include files and path mappings', () => {
const webTsConfigPath = '/tsconfig.app.json';
expect(appTree.files).toContain(webTsConfigPath);
const webTsconfig = JSON.parse(stripJsonComments(getFileContent(appTree, webTsConfigPath)));
const files = webTsconfig.files;
expect(files).toBeDefined();
expect(files.includes('src/main.ts')).toBeTruthy();
expect(files.includes('src/polyfills.ts')).toBeTruthy();
const paths = webTsconfig.compilerOptions.paths;
expect(paths).toBeDefined();
expect(paths['@src/*']).toBeDefined();
const maps = paths['@src/*'];
expect(maps).toContain('src/*.web');
expect(maps).toContain('src/*');
});
it('should create the tsconfig.tns.json with files and path mappings', () => {
const nsTsConfigPath = '/tsconfig.tns.json';
expect(appTree.files).toContain(nsTsConfigPath);
const nsTsConfig = JSON.parse(stripJsonComments(getFileContent(appTree, nsTsConfigPath)));
const files = nsTsConfig.files;
expect(files).toBeDefined();
expect(files).toContain('src/main.tns.ts');
const paths = nsTsConfig.compilerOptions.paths;
expect(paths).toBeDefined();
expect(paths['@src/*']).toBeDefined();
const maps = paths['@src/*'];
expect(maps).toContain('src/*.tns.ts');
expect(maps).toContain('src/*.ts');
});
it('should create the tsconfig.spec.json (web) with files', () => {
const specTsConfigPath = '/tsconfig.spec.json';
expect(appTree.files).toContain(specTsConfigPath);
const specTsConfig = JSON.parse(stripJsonComments(getFileContent(appTree, specTsConfigPath)));
const files = specTsConfig.files;
expect(files).toBeDefined();
expect(files.includes('src/test.ts')).toBeTruthy();
expect(files.includes('src/polyfills.ts')).toBeTruthy();
});
xit('should modify the base tsconfig.json to include path mappings', () => {
const baseTsConfigPath = '/tsconfig.base.json';
expect(appTree.files).toContain(baseTsConfigPath);
const baseTsConfig = JSON.parse(stripJsonComments(getFileContent(appTree, baseTsConfigPath)));
const paths = baseTsConfig.compilerOptions.paths;
expect(paths).toBeDefined();
expect(paths['@src/*']).toBeDefined();
const maps = paths['@src/*'];
expect(maps).toContain('src/*.android.ts');
expect(maps).toContain('src/*.ios.ts');
expect(maps).toContain('src/*.tns.ts');
expect(maps).toContain('src/*.web.ts');
expect(maps).toContain('src/*');
});
it('should modify tslint.json to include rule for using remapped imports', () => {
const tsLintConfigPath = '/tslint.json';
expect(appTree.files).toContain(tsLintConfigPath);
const tsLintConfig = JSON.parse(stripJsonComments(getFileContent(appTree, tsLintConfigPath)));
const { extends: tsLintExtends, rules: tsLintRules } = tsLintConfig;
expect(tsLintExtends).toEqual(jasmine.any(Array));
expect(tsLintExtends).toContain('@nativescript/tslint-rules');
expect(tsLintRules).toEqual(jasmine.any(Object));
expect(Object.keys(tsLintRules)).toContain('prefer-mapped-imports');
const rule = tsLintRules['prefer-mapped-imports'];
const ruleOptions = rule[1];
const actualBaseUrl = ruleOptions['base-url'];
expect(actualBaseUrl).toEqual('./');
});
it('should generate a sample shared component', () => {
const { files } = appTree;
const appRoutingModuleContent = appTree.readContent('/src/app/app-routing.module.tns.ts');
const appComponentTemplate = appTree.readContent('/src/app/app.component.tns.html');
expect(files).toContain('/src/app/auto-generated/auto-generated.component.ts');
expect(files).toContain('/src/app/auto-generated/auto-generated.component.html');
expect(files).toContain('/src/app/auto-generated/auto-generated.component.tns.html');
expect(appRoutingModuleContent).toMatch(
/import { AutoGeneratedComponent } from '@src\/app\/auto-generated\/auto-generated.component'/,
);
expect(appRoutingModuleContent).toMatch(
/{\s+path: 'auto-generated',\s+component: AutoGeneratedComponent,\s+},/g,
);
expect(appComponentTemplate).not.toContain(
'<Label text="Entry Component works" textWrap="true"></Label>',
);
});
});
describe('when the skipAutoGeneratedComponent flag is raised', () => {
beforeEach(async () => {
const options = {
...defaultOptions,
skipAutoGeneratedComponent: true,
};
appTree = await schematicRunner.runSchematicAsync('add-ns', options, appTree).toPromise();
});
it('should not add a sample shared component', () => {
const { files } = appTree;
const appRoutingModuleContent = appTree.readContent('/src/app/app-routing.module.tns.ts');
const appComponentTemplate = appTree.readContent('/src/app/app.component.tns.html');
expect(files).not.toContain('/src/app/auto-generated/auto-generated.component.css');
expect(files).not.toContain('/src/app/auto-generated/auto-generated.component.html');
expect(files).not.toContain('/src/app/auto-generated/auto-generated.component.ts');
expect(appRoutingModuleContent).not.toMatch(
/import { AutoGeneratedComponent } from '.\/auto-generated\/auto-generated.component'/,
);
expect(appRoutingModuleContent).toContain(
'export const routes: Routes = []',
);
expect(appComponentTemplate).toContain(
`This is just a fun sample for you to play with`,
);
});
});
describe('when the sample flag is raised', () => {
beforeEach(async () => {
const options = {
...defaultOptions,
sample: true,
};
appTree = await schematicRunner.runSchematicAsync('add-ns', options, appTree).toPromise();
});
it('should generate sample files', () => {
const { files } = appTree;
expect(files).toContain('/src/app/barcelona/barcelona.common.ts');
expect(files).toContain('/src/app/barcelona/barcelona.module.ts');
expect(files).toContain('/src/app/barcelona/barcelona.module.tns.ts');
expect(files).toContain('/src/app/barcelona/player.service.ts');
expect(files).toContain('/src/app/barcelona/player.model.ts');
expect(files).toContain('/src/app/barcelona/players/players.component.ts');
expect(files).toContain('/src/app/barcelona/players/players.component.html');
expect(files).toContain('/src/app/barcelona/players/players.component.tns.html');
expect(files).toContain('/src/app/barcelona/player-detail/player-detail.component.ts');
expect(files).toContain('/src/app/barcelona/player-detail/player-detail.component.html');
expect(files).toContain('/src/app/barcelona/player-detail/player-detail.component.tns.html');
});
it('should configure routing for redirection', () => {
const appRoutingModuleContent = appTree.readContent('/src/app/app-routing.module.tns.ts');
expect(appRoutingModuleContent).toMatch(
/{\s+path: '',\s+redirectTo: '\/players',\s+pathMatch: 'full',\s+},/g,
);
});
});
});
async function setupProject(
tree: UnitTestTree,
schematicRunner: SchematicTestRunner,
name: string,
) {
tree = await schematicRunner.runExternalSchematicAsync(
'@schematics/angular',
'workspace',
{
name: 'workspace',
version: '10.1.0',
newProjectRoot: '',
},
).toPromise();
tree = await schematicRunner.runExternalSchematicAsync(
'@schematics/angular',
'application',
{
name,
projectRoot: '',
},
tree,
).toPromise();
return tree;
}