forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex_spec.ts
More file actions
602 lines (505 loc) · 26.1 KB
/
index_spec.ts
File metadata and controls
602 lines (505 loc) · 26.1 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Style as AppStyle, Schema as ApplicationOptions } from '../application/schema';
import { createAppModule } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { ChangeDetection, Schema as ComponentOptions, Style } from './schema';
describe('Component Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const defaultOptions: ComponentOptions = {
name: 'foo',
// path: 'src/app',
inlineStyle: false,
inlineTemplate: false,
displayBlock: false,
changeDetection: ChangeDetection.OnPush,
style: Style.Css,
type: 'Component',
skipTests: false,
module: undefined,
export: false,
project: 'bar',
ngHtml: false,
};
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const appOptions: ApplicationOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: AppStyle.Css,
skipTests: false,
skipPackageJson: false,
};
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
});
it('should contain a TestBed compileComponents call', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(tsContent).toContain('compileComponents()');
});
it('should not set change detection when default is OnPush', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).not.toMatch(/import.*ChangeDetectionStrategy/);
expect(tsContent).not.toMatch(/changeDetection:/);
expect(tsContent).not.toMatch(/ChangeDetectionStrategy/);
});
it('should set changeDetection to Eager when requested', async () => {
const options = { ...defaultOptions, changeDetection: 'Eager' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toMatch(/import .*ChangeDetectionStrategy/);
expect(tsContent).toContain('changeDetection: ChangeDetectionStrategy.Eager,');
});
it('should not set view encapsulation', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).not.toMatch(/encapsulation: ViewEncapsulation/);
});
it('should set view encapsulation to Emulated', async () => {
const options = { ...defaultOptions, viewEncapsulation: 'Emulated' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toMatch(/encapsulation: ViewEncapsulation.Emulated/);
});
it('should set view encapsulation to None', async () => {
const options = { ...defaultOptions, viewEncapsulation: 'None' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toMatch(/encapsulation: ViewEncapsulation.None/);
});
it('should create a flat component', async () => {
const options = { ...defaultOptions, flat: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/bar/src/app/foo.component.css',
'/projects/bar/src/app/foo.component.html',
'/projects/bar/src/app/foo.component.spec.ts',
'/projects/bar/src/app/foo.component.ts',
]),
);
});
it('should handle upper case paths', async () => {
const pathOption = 'projects/bar/src/app/SOME/UPPER/DIR';
const options = { ...defaultOptions, path: pathOption };
const tree = await schematicRunner.runSchematic('component', options, appTree);
let files = tree.files;
let root = `/${pathOption}/foo/foo.component`;
expect(files).toEqual(
jasmine.arrayContaining([`${root}.css`, `${root}.html`, `${root}.spec.ts`, `${root}.ts`]),
);
const options2 = { ...options, name: 'BAR' };
const tree2 = await schematicRunner.runSchematic('component', options2, tree);
files = tree2.files;
root = `/${pathOption}/bar/bar.component`;
expect(files).toEqual(
jasmine.arrayContaining([`${root}.css`, `${root}.html`, `${root}.spec.ts`, `${root}.ts`]),
);
});
it('should create a component in a sub-directory', async () => {
const options = { ...defaultOptions, path: 'projects/bar/src/app/a/b/c' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const files = tree.files;
const root = `/${options.path}/foo/foo.component`;
expect(files).toEqual(
jasmine.arrayContaining([`${root}.css`, `${root}.html`, `${root}.spec.ts`, `${root}.ts`]),
);
});
it('should use the prefix', async () => {
const options = { ...defaultOptions, prefix: 'pre' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/selector: 'pre-foo'/);
});
it('should error when name starts with a digit', async () => {
const options = { ...defaultOptions, name: '1-one' };
await expectAsync(
schematicRunner.runSchematic('component', options, appTree),
).toBeRejectedWithError('Selector "app-1-one" is invalid.');
});
it('should error when class name contains invalid characters', async () => {
const options = { ...defaultOptions, name: '404' };
await expectAsync(
schematicRunner.runSchematic('component', options, appTree),
).toBeRejectedWithError('Class name "404Component" is invalid.');
});
it('should allow dash in selector before a number', async () => {
const options = { ...defaultOptions, name: 'one-1' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
expect(content).toMatch(/selector: 'app-one-1'/);
});
it('should allow dash in selector before a number and with a custom prefix', async () => {
const options = { ...defaultOptions, name: 'one-1', prefix: 'pre' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
expect(content).toMatch(/selector: 'pre-one-1'/);
});
it('should allow dash in selector before a number and without a prefix', async () => {
const options = { ...defaultOptions, name: 'one-2', selector: 'one-2' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-2/one-2.component.ts');
expect(content).toMatch(/selector: 'one-2'/);
});
it('should use the default project prefix if none is passed', async () => {
const options = { ...defaultOptions, prefix: undefined };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/selector: 'app-foo'/);
});
it('should use the supplied prefix if it is ""', async () => {
const options = { ...defaultOptions, prefix: '' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/selector: 'foo'/);
});
it('should respect the inlineTemplate option', async () => {
const options = { ...defaultOptions, inlineTemplate: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/template: /);
expect(content).not.toMatch(/templateUrl: /);
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.html');
});
it('should respect the inlineStyle option', async () => {
const options = { ...defaultOptions, inlineStyle: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/styles: `/);
expect(content).not.toMatch(/styleUrl: /);
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
});
it('should respect the displayBlock option when inlineStyle is `false`', async () => {
const options = { ...defaultOptions, displayBlock: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.css');
expect(content).toMatch(/:host {\r?\n {2}display: block;\r?\n}/);
});
it('should respect the displayBlock option when inlineStyle is `false` and use correct syntax for `scss`', async () => {
const options = { ...defaultOptions, displayBlock: true, style: 'scss' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.scss');
expect(content).toMatch(/:host {\r?\n {2}display: block;\r?\n}/);
});
it('should respect the displayBlock option when inlineStyle is `false` and use correct syntax for `sass', async () => {
const options = { ...defaultOptions, displayBlock: true, style: 'sass' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.sass');
expect(content).toMatch(/\\:host\r?\n {2}display: block;\r?\n/);
});
it('should respect the displayBlock option when inlineStyle is `true`', async () => {
const options = { ...defaultOptions, displayBlock: true, inlineStyle: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/:host {\r?\n(\s*)display: block;(\s*)}\r?\n/);
});
it('should respect the style option', async () => {
const options = { ...defaultOptions, style: Style.Sass };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toMatch(/styleUrl: '.\/foo.component.sass/);
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.sass');
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
});
it('should respect the style=none option', async () => {
const options = { ...defaultOptions, style: Style.None };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).not.toMatch(/styleUrls: /);
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.none');
});
it('should respect the type option', async () => {
const options = { ...defaultOptions, type: 'Route' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.route.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.route.spec.ts');
expect(content).toContain('export class FooRoute');
expect(testContent).toContain("describe('FooRoute'");
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
});
it('should allow empty string in the type option', async () => {
const options = { ...defaultOptions, type: '' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
expect(content).toContain('export class Foo');
expect(testContent).toContain("describe('Foo'");
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');
});
it('should not use `.ng.html` extension when ngHtml is false', async () => {
const options = { ...defaultOptions, ngHtml: false };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toContain('foo.component.html');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.html');
});
it('should use `.ng.html` extension when ngHtml is true', async () => {
const options = { ...defaultOptions, ngHtml: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toContain('foo.component.ng.html');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.ng.html');
});
it('should not use `.ng.html` extension when ngHtml is not present', async () => {
const options = { ...defaultOptions, ngHtml: undefined };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).toContain('foo.component.html');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.component.html');
});
it('should create the right selector with a path in the name', async () => {
const options = { ...defaultOptions, name: 'sub/test' };
appTree = await schematicRunner.runSchematic('component', options, appTree);
const content = appTree.readContent('/projects/bar/src/app/sub/test/test.component.ts');
expect(content).toMatch(/selector: 'app-test'/);
});
it('should respect the skipSelector option', async () => {
const options = { ...defaultOptions, name: 'sub/test', skipSelector: true };
appTree = await schematicRunner.runSchematic('component', options, appTree);
const content = appTree.readContent('/projects/bar/src/app/sub/test/test.component.ts');
expect(content).not.toMatch(/selector: 'app-test'/);
});
it('should respect the skipTests option', async () => {
const options = { ...defaultOptions, skipTests: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/bar/src/app/foo/foo.component.css',
'/projects/bar/src/app/foo/foo.component.html',
'/projects/bar/src/app/foo/foo.component.ts',
]),
);
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.spec.ts');
});
it('should respect templateUrl when style=none and changeDetection=Eager', async () => {
const options = { ...defaultOptions, style: Style.None, changeDetection: 'Eager' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).not.toMatch(/styleUrls: /);
expect(content).toMatch(/templateUrl: '.\/foo.component.html',\n/);
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.Eager/);
});
it('should respect inlineTemplate when style=none and changeDetection=Eager', async () => {
const options = {
...defaultOptions,
style: Style.None,
changeDetection: 'Eager',
inlineTemplate: true,
};
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(content).not.toMatch(/styleUrls: /);
expect(content).toMatch(/template: `(\n(.|)*){3}\n\s*`,\n/);
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.Eager,/);
});
it('should create a standalone component', async () => {
const options = { ...defaultOptions, standalone: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const moduleContent = tree.readContent('/projects/bar/src/app/app-module.ts');
const componentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(componentContent).toContain('class FooComponent');
expect(moduleContent).not.toContain('FooComponent');
expect(componentContent).not.toContain('standalone');
});
it('should declare standalone components in the `imports` of a test', async () => {
const options = { ...defaultOptions, standalone: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(testContent).toContain('imports: [FooComponent]');
expect(testContent).not.toContain('declarations');
});
describe('standalone=false', () => {
const defaultNonStandaloneOptions: ComponentOptions = {
...defaultOptions,
standalone: false,
project: 'baz',
};
beforeEach(async () => {
appTree = await schematicRunner.runSchematic(
'application',
{ ...appOptions, standalone: false, name: 'baz' },
appTree,
);
});
it('should create a component', async () => {
const options = { ...defaultNonStandaloneOptions };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/baz/src/app/foo/foo.component.css',
'/projects/baz/src/app/foo/foo.component.html',
'/projects/baz/src/app/foo/foo.component.spec.ts',
'/projects/baz/src/app/foo/foo.component.ts',
]),
);
const moduleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
});
it('should use the module flag even if the module is a routing module', async () => {
const routingFileName = 'app-routing-module.ts';
const routingModulePath = `/projects/baz/src/app/${routingFileName}`;
const newTree = createAppModule(appTree, routingModulePath);
const options = { ...defaultNonStandaloneOptions, module: routingFileName };
const tree = await schematicRunner.runSchematic('component', options, newTree);
const content = tree.readContent(routingModulePath);
expect(content).toMatch(/import { FooComponent } from '.\/foo\/foo.component/);
});
it('should handle a path in the name option', async () => {
const options = { ...defaultNonStandaloneOptions, name: 'dir/test-component' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(content).toMatch(
/import { TestComponentComponent } from '\.\/dir\/test-component\/test-component.component'/,
);
});
it('should handle a path in the name and module options', async () => {
appTree = await schematicRunner.runSchematic(
'module',
{ name: 'admin/module', project: 'baz' },
appTree,
);
const options = {
...defaultNonStandaloneOptions,
name: 'other/test-component',
module: 'admin/module',
};
appTree = await schematicRunner.runSchematic('component', options, appTree);
const content = appTree.readContent('/projects/baz/src/app/admin/module/module-module.ts');
expect(content).toMatch(
/import { TestComponentComponent } from '..\/..\/other\/test-component\/test-component.component'/,
);
});
it('should find the closest module', async () => {
const options = { ...defaultNonStandaloneOptions };
const fooModule = '/projects/baz/src/app/foo/foo-module.ts';
appTree.create(
fooModule,
`
import { NgModule } from '@angular/core';
@NgModule({
imports: [],
declarations: []
})
export class FooModule { }
`,
);
const tree = await schematicRunner.runSchematic('component', options, appTree);
const fooModuleContent = tree.readContent(fooModule);
expect(fooModuleContent).toMatch(/import { FooComponent } from '.\/foo.component'/);
});
it('should export the component', async () => {
const options = { ...defaultNonStandaloneOptions, export: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const appModuleContent = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(appModuleContent).toMatch(/exports: \[\n(\s*) {2}FooComponent\n\1\]/);
});
it('should import into a specified module', async () => {
const options = { ...defaultNonStandaloneOptions, module: 'app-module.ts' };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');
expect(appModule).toMatch(/import { FooComponent } from '.\/foo\/foo.component'/);
});
it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.baz.sourceRoot = 'projects/baz/custom';
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
// should fail without a module in that dir
await expectAsync(
schematicRunner.runSchematic('component', defaultNonStandaloneOptions, appTree),
).toBeRejected();
// move the module
appTree.rename(
'/projects/baz/src/app/app-module.ts',
'/projects/baz/custom/app/app-module.ts',
);
appTree = await schematicRunner.runSchematic(
'component',
defaultNonStandaloneOptions,
appTree,
);
expect(appTree.files).toContain('/projects/baz/custom/app/foo/foo.component.ts');
});
it('should fail if specified module does not exist', async () => {
const options = {
...defaultNonStandaloneOptions,
module: '/projects/baz/src/app.moduleXXX.ts',
};
await expectAsync(schematicRunner.runSchematic('component', options, appTree)).toBeRejected();
});
});
it('should export the component as default when exportDefault is true', async () => {
const options = { ...defaultOptions, exportDefault: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toContain('export default class FooComponent');
const specContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(specContent).toContain("import FooComponent from './foo.component';");
});
it('should export the component as a named export when exportDefault is false', async () => {
const options = { ...defaultOptions, exportDefault: false };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toContain('export class FooComponent');
const specContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(specContent).toContain("import { FooComponent } from './foo.component';");
});
it('should add fixture.whenStable() in spec file when zoneless and standalone apps', async () => {
const tree = await schematicRunner.runSchematic('component', { ...defaultOptions }, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.spec.ts');
expect(tsContent).toContain('fixture.whenStable()');
expect(tsContent).not.toContain('fixture.detectChanges()');
});
describe('with zone.js application', () => {
const zoneAppOptions: ApplicationOptions = {
...appOptions,
name: 'baz',
zoneless: false,
};
it('should add not fixture.whenStable() in spec file for standalone', async () => {
appTree = await schematicRunner.runSchematic(
'application',
{ ...zoneAppOptions, standalone: true },
appTree,
);
const tree = await schematicRunner.runSchematic(
'component',
{ ...defaultOptions, standalone: true, project: 'baz' },
appTree,
);
const tsContent = tree.readContent('/projects/baz/src/app/foo/foo.component.spec.ts');
expect(tsContent).not.toContain('fixture.whenStable()');
expect(tsContent).toContain('fixture.detectChanges()');
});
});
});