@@ -3,10 +3,12 @@ import { join } from 'path';
3
3
import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
4
4
import { getFileContent } from '@schematics/angular/utility/test' ;
5
5
6
- import { createEmptyNsOnlyProject , createEmptySharedProject , toComponentClassName } from '../../utils' ;
6
+ import { createEmptyNsOnlyProject , createEmptySharedProject , toComponentClassName , callRuleSync } from '../../utils' ;
7
7
import { DEFAULT_SHARED_EXTENSIONS } from '../utils' ;
8
8
import { isInComponentMetadata , isInModuleMetadata } from '../../test-utils' ;
9
9
import { Schema as ComponentOptions } from './schema' ;
10
+ import { Schema as ApplicationOptions } from '../../ng-new/shared/schema' ;
11
+ import { move } from '@angular-devkit/schematics' ;
10
12
11
13
describe ( 'Component Schematic' , ( ) => {
12
14
const name = 'foo' ;
@@ -144,7 +146,7 @@ describe('Component Schematic', () => {
144
146
describe ( 'specifying custom extension' , ( ) => {
145
147
describe ( 'in ns only project' , ( ) => {
146
148
beforeEach ( ( ) => {
147
- appTree = createEmptyNsOnlyProject ( project ) ;
149
+ appTree = createEmptyNsOnlyProject ( project , '.mobile' ) ;
148
150
} ) ;
149
151
150
152
it ( 'should respect specified {N} extension' , ( ) => {
@@ -155,40 +157,173 @@ describe('Component Schematic', () => {
155
157
const componentTemplatePath = getTemplatePath ( customExtension ) ;
156
158
expect ( appTree . exists ( componentTemplatePath ) ) . toBeTruthy ( ) ;
157
159
} ) ;
158
- } )
160
+ } ) ;
161
+
159
162
describe ( 'in ns+web project' , ( ) => {
160
- beforeEach ( ( ) => {
161
- appTree = createEmptySharedProject ( project ) ;
163
+ describe ( 'when a custom web extension is specified' , ( ) => {
164
+ const customExtension = '.web' ;
165
+ const componentOptions = { ...defaultOptions , webExtension : customExtension , web : true } ;
166
+
167
+ beforeEach ( ( ) => {
168
+ appTree = createEmptySharedProject ( project , customExtension , '.tns' ) ;
169
+ } ) ;
170
+
171
+ it ( 'should create the files with this extension' , ( ) => {
172
+ const options = { ...componentOptions } ;
173
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
174
+
175
+ const componentTemplatePath = getTemplatePath ( customExtension ) ;
176
+ expect ( appTree . exists ( componentTemplatePath ) ) . toBeTruthy ( ) ;
177
+ } ) ;
178
+
179
+ it ( 'should declare in NgModule' , ( ) => {
180
+ const options = { ...componentOptions } ;
181
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
182
+
183
+ const webModulePath = `src/app/app.module${ customExtension } .ts` ;
184
+ const nsModulePath = `src/app/app.module.tns.ts` ;
185
+ const matcher = isInModuleMetadata ( 'AppModule' , 'declarations' , componentClassName , true ) ;
186
+
187
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
188
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
189
+
190
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
191
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
192
+ } ) ;
193
+
194
+ it ( 'should respect the module option' , ( ) => {
195
+ const moduleName = 'random' ;
196
+ const webModulePath = `src/app/${ moduleName } /${ moduleName } .module${ customExtension } .ts` ;
197
+ const nsModulePath = `src/app/${ moduleName } /${ moduleName } .module.tns.ts` ;
198
+ appTree = schematicRunner . runSchematic ( 'module' , {
199
+ project,
200
+ name : moduleName ,
201
+ webExtension : customExtension ,
202
+ } , appTree ) ;
203
+
204
+ const options = { ...componentOptions , module : moduleName } ;
205
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
206
+
207
+ const matcher = isInModuleMetadata ( 'RandomModule' , 'declarations' , componentClassName , true ) ;
208
+
209
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
210
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
211
+
212
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
213
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
214
+ } ) ;
162
215
} ) ;
163
216
164
- it ( 'should respect specified {N} extension', ( ) => {
217
+ describe ( 'when a custon {N} extension is specified ', ( ) => {
165
218
const customExtension = '.mobile' ;
166
- const options = { ...defaultOptions , nsExtension : customExtension , nativescript : true } ;
167
- appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
219
+ const componentOptions = { ...defaultOptions , nsExtension : customExtension , nativescript : true } ;
168
220
169
- const componentTemplatePath = getTemplatePath ( customExtension ) ;
170
- expect ( appTree . exists ( componentTemplatePath ) ) . toBeTruthy ( ) ;
171
- } ) ;
221
+ beforeEach ( ( ) => {
222
+ appTree = createEmptySharedProject ( project , '' , customExtension ) ;
223
+ } ) ;
172
224
173
- it ( 'should respect specified web extension' , ( ) => {
174
- const customExtension = '.web' ;
175
- const options = { ...defaultOptions , webExtension : customExtension , web : true } ;
176
- appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
225
+ it ( 'should create the files with this extension' , ( ) => {
226
+ const options = { ...componentOptions } ;
227
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
177
228
178
- const componentTemplatePath = getTemplatePath ( customExtension ) ;
179
- expect ( appTree . exists ( componentTemplatePath ) ) . toBeTruthy ( ) ;
229
+ const componentTemplatePath = getTemplatePath ( customExtension ) ;
230
+ expect ( appTree . exists ( componentTemplatePath ) ) . toBeTruthy ( ) ;
231
+ } ) ;
232
+
233
+ it ( 'should declare in NgModule' , ( ) => {
234
+ const options = { ...componentOptions } ;
235
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
236
+
237
+ const webModulePath = `src/app/app.module.ts` ;
238
+ const nsModulePath = `src/app/app.module${ customExtension } .ts` ;
239
+ const matcher = isInModuleMetadata ( 'AppModule' , 'declarations' , componentClassName , true ) ;
240
+
241
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
242
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
243
+
244
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
245
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
246
+ } ) ;
247
+
248
+ it ( 'should respect the module option' , ( ) => {
249
+ const moduleName = 'random' ;
250
+ const webModulePath = `src/app/${ moduleName } /${ moduleName } .module.ts` ;
251
+ const nsModulePath = `src/app/${ moduleName } /${ moduleName } .module${ customExtension } .ts` ;
252
+ appTree = schematicRunner . runSchematic ( 'module' , {
253
+ project,
254
+ name : moduleName ,
255
+ nsExtension : customExtension ,
256
+ } , appTree ) ;
257
+
258
+ const options = { ...componentOptions , module : moduleName } ;
259
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
260
+
261
+ const matcher = isInModuleMetadata ( 'RandomModule' , 'declarations' , componentClassName , true ) ;
262
+
263
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
264
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
265
+
266
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
267
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
268
+ } ) ;
180
269
} ) ;
181
270
182
- it ( 'should respect both web and {N} extensions', ( ) => {
271
+ describe ( 'when custom web and {N} extensions are specified ', ( ) => {
183
272
const nsExtension = '.mobile' ;
184
273
const webExtension = '.web' ;
185
- const options = { ...defaultOptions , nsExtension, webExtension, web : true , nativescript : true } ;
186
- appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
187
-
188
- const nsTemplate = getTemplatePath ( nsExtension ) ;
189
- const webTemplate = getTemplatePath ( webExtension ) ;
190
- expect ( appTree . exists ( nsTemplate ) ) . toBeTruthy ( ) ;
191
- expect ( appTree . exists ( webTemplate ) ) . toBeTruthy ( ) ;
274
+ const componentOptions = { ...defaultOptions , nsExtension, webExtension, web : true , nativescript : true } ;
275
+
276
+ beforeEach ( ( ) => {
277
+ appTree = createEmptySharedProject ( project , webExtension , nsExtension ) ;
278
+ } ) ;
279
+
280
+ it ( 'should create the files with these extensions' , ( ) => {
281
+ const options = { ...componentOptions } ;
282
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
283
+
284
+ const nsTemplate = getTemplatePath ( nsExtension ) ;
285
+ const webTemplate = getTemplatePath ( webExtension ) ;
286
+ expect ( appTree . exists ( nsTemplate ) ) . toBeTruthy ( ) ;
287
+ expect ( appTree . exists ( webTemplate ) ) . toBeTruthy ( ) ;
288
+ } ) ;
289
+
290
+ it ( 'should declare in NgModule' , ( ) => {
291
+ const options = { ...componentOptions } ;
292
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
293
+
294
+ const webModulePath = `src/app/app.module${ webExtension } .ts` ;
295
+ const nsModulePath = `src/app/app.module${ nsExtension } .ts` ;
296
+ const matcher = isInModuleMetadata ( 'AppModule' , 'declarations' , componentClassName , true ) ;
297
+
298
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
299
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
300
+
301
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
302
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
303
+ } ) ;
304
+
305
+ it ( 'should respect the module option' , ( ) => {
306
+ const moduleName = 'random' ;
307
+ const webModulePath = `src/app/${ moduleName } /${ moduleName } .module${ webExtension } .ts` ;
308
+ const nsModulePath = `src/app/${ moduleName } /${ moduleName } .module${ nsExtension } .ts` ;
309
+ appTree = schematicRunner . runSchematic ( 'module' , {
310
+ project,
311
+ name : moduleName ,
312
+ webExtension,
313
+ nsExtension,
314
+ } , appTree ) ;
315
+
316
+ const options = { ...componentOptions , module : moduleName } ;
317
+ appTree = schematicRunner . runSchematic ( 'component' , options , appTree ) ;
318
+
319
+ const matcher = isInModuleMetadata ( 'RandomModule' , 'declarations' , componentClassName , true ) ;
320
+
321
+ const webModuleContent = getFileContent ( appTree , webModulePath ) ;
322
+ expect ( webModuleContent ) . toMatch ( matcher ) ;
323
+
324
+ const nsModuleContent = getFileContent ( appTree , nsModulePath ) ;
325
+ expect ( nsModuleContent ) . toMatch ( matcher ) ;
326
+ } ) ;
192
327
} ) ;
193
328
} )
194
329
} ) ;
0 commit comments