/
index_spec.ts
120 lines (105 loc) · 4.35 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
/**
* @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.io/license
*/
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as ClassOptions } from './schema';
describe('Class Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const defaultOptions: ClassOptions = {
name: 'foo',
type: '',
skipTests: true,
project: 'bar',
};
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const appOptions: ApplicationOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
routing: false,
skipTests: false,
skipPackageJson: false,
};
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await schematicRunner.runSchematicAsync('workspace', workspaceOptions).toPromise();
appTree = await schematicRunner
.runSchematicAsync('application', appOptions, appTree)
.toPromise();
});
it('should create just the class file', async () => {
const tree = await schematicRunner
.runSchematicAsync('class', defaultOptions, appTree)
.toPromise();
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
expect(tree.files).not.toContain('/projects/bar/src/app/foo.spec.ts');
});
it('should create the class and spec file', async () => {
const options = {
...defaultOptions,
skipTests: false,
};
const tree = await schematicRunner.runSchematicAsync('class', options, appTree).toPromise();
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
expect(tree.files).toContain('/projects/bar/src/app/foo.spec.ts');
});
it('should create an class named "Foo"', async () => {
const tree = await schematicRunner
.runSchematicAsync('class', defaultOptions, appTree)
.toPromise();
const fileContent = tree.readContent('/projects/bar/src/app/foo.ts');
expect(fileContent).toMatch(/export class Foo/);
});
it('should put type in the file name', async () => {
const options = { ...defaultOptions, type: 'model' };
const tree = await schematicRunner.runSchematicAsync('class', options, appTree).toPromise();
expect(tree.files).toContain('/projects/bar/src/app/foo.model.ts');
});
it('should split the name to name & type with split on "."', async () => {
const options = { ...defaultOptions, name: 'foo.model' };
const tree = await schematicRunner.runSchematicAsync('class', options, appTree).toPromise();
const classPath = '/projects/bar/src/app/foo.model.ts';
const content = tree.readContent(classPath);
expect(content).toMatch(/export class FooModel/);
});
it('should respect the path option', async () => {
const options = { ...defaultOptions, path: 'zzz' };
const tree = await schematicRunner.runSchematicAsync('class', options, appTree).toPromise();
expect(tree.files).toContain('/zzz/foo.ts');
});
it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.bar.sourceRoot = 'projects/bar/custom';
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
appTree = await schematicRunner.runSchematicAsync('class', defaultOptions, appTree).toPromise();
expect(appTree.files).toContain('/projects/bar/custom/app/foo.ts');
});
it('should respect the skipTests flag', async () => {
const options = {
...defaultOptions,
skipTests: true,
};
const tree = await schematicRunner.runSchematicAsync('class', options, appTree).toPromise();
expect(tree.files).toContain('/projects/bar/src/app/foo.ts');
expect(tree.files).not.toContain('/projects/bar/src/app/foo.spec.ts');
});
it('should error when class name contains invalid characters', async () => {
const options = { ...defaultOptions, name: '1Clazz' };
await expectAsync(
schematicRunner.runSchematicAsync('class', options, appTree).toPromise(),
).toBeRejectedWithError('Class name "1Clazz" is invalid.');
});
});