Skip to content

Commit 5d34cb7

Browse files
devversionmmalerba
authored andcommitted
chore(schematics): add tests for address-form and tree schematic (#13178)
* Adds spec files for the `address-form` and `tree` schematic. All other schematics already have tests.
1 parent 060123f commit 5d34cb7

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
2+
import {collectionPath, createTestApp} from '../test-setup/test-app';
3+
import {getFileContent} from '@schematics/angular/utility/test';
4+
import {Schema} from './schema';
5+
6+
describe('Material address-form schematic', () => {
7+
let runner: SchematicTestRunner;
8+
9+
const baseOptions: Schema = {
10+
name: 'foo',
11+
project: 'material',
12+
};
13+
14+
beforeEach(() => {
15+
runner = new SchematicTestRunner('schematics', collectionPath);
16+
});
17+
18+
it('should create address-form files and add them to module', () => {
19+
const tree = runner.runSchematic('address-form', baseOptions, createTestApp());
20+
const files = tree.files;
21+
22+
expect(files).toContain('/projects/material/src/app/foo/foo.component.css');
23+
expect(files).toContain('/projects/material/src/app/foo/foo.component.html');
24+
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
25+
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
26+
27+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
28+
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
29+
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
30+
});
31+
32+
it('should add address-form imports to module', () => {
33+
const tree = runner.runSchematic('address-form', baseOptions, createTestApp());
34+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
35+
36+
expect(moduleContent).toContain('MatInputModule');
37+
expect(moduleContent).toContain('MatButtonModule');
38+
expect(moduleContent).toContain('MatSelectModule');
39+
expect(moduleContent).toContain('MatRadioModule');
40+
expect(moduleContent).toContain('ReactiveFormsModule');
41+
});
42+
43+
describe('styleext option', () => {
44+
it('should respect the option value', () => {
45+
const tree = runner.runSchematic(
46+
'address-form', {styleext: 'scss', ...baseOptions}, createTestApp());
47+
48+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
49+
});
50+
51+
it('should fallback to the @schematics/angular:component option value', () => {
52+
const tree = runner.runSchematic('address-form', baseOptions, createTestApp({style: 'less'}));
53+
54+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
55+
});
56+
});
57+
58+
describe('inlineStyle option', () => {
59+
it('should respect the option value', () => {
60+
const tree = runner.runSchematic(
61+
'address-form', {inlineStyle: true, ...baseOptions}, createTestApp());
62+
63+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
64+
});
65+
66+
it('should fallback to the @schematics/angular:component option value', () => {
67+
const tree = runner.runSchematic(
68+
'address-form', baseOptions, createTestApp({inlineStyle: true}));
69+
70+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
71+
});
72+
});
73+
74+
describe('inlineTemplate option', () => {
75+
it('should respect the option value', () => {
76+
const tree = runner.runSchematic(
77+
'address-form', {inlineTemplate: true, ...baseOptions}, createTestApp());
78+
79+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
80+
});
81+
82+
it('should fallback to the @schematics/angular:component option value', () => {
83+
const tree = runner.runSchematic(
84+
'address-form', baseOptions, createTestApp({inlineTemplate: true}));
85+
86+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
87+
});
88+
});
89+
90+
describe('spec option', () => {
91+
it('should respect the option value', () => {
92+
const tree = runner.runSchematic(
93+
'address-form', {spec: false, ...baseOptions}, createTestApp());
94+
95+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
96+
});
97+
98+
it('should fallback to the @schematics/angular:component option value', () => {
99+
const tree = runner.runSchematic(
100+
'address-form', baseOptions, createTestApp({skipTests: true}));
101+
102+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
103+
});
104+
});
105+
});

src/lib/schematics/tree/index.spec.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
2+
import {collectionPath, createTestApp} from '../test-setup/test-app';
3+
import {getFileContent} from '@schematics/angular/utility/test';
4+
import {Schema} from './schema';
5+
6+
describe('Material tree schematic', () => {
7+
let runner: SchematicTestRunner;
8+
9+
const baseOptions: Schema = {
10+
name: 'foo',
11+
project: 'material',
12+
};
13+
14+
beforeEach(() => {
15+
runner = new SchematicTestRunner('schematics', collectionPath);
16+
});
17+
18+
it('should create tree component files and add them to module', () => {
19+
const tree = runner.runSchematic('tree', baseOptions, createTestApp());
20+
const files = tree.files;
21+
22+
expect(files).toContain('/projects/material/src/app/foo/foo.component.css');
23+
expect(files).toContain('/projects/material/src/app/foo/foo.component.html');
24+
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
25+
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
26+
27+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
28+
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
29+
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
30+
});
31+
32+
it('should add tree imports to module', () => {
33+
const tree = runner.runSchematic('tree', baseOptions, createTestApp());
34+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
35+
36+
expect(moduleContent).toContain('MatTreeModule');
37+
expect(moduleContent).toContain('MatIconModule');
38+
expect(moduleContent).toContain('MatButtonModule');
39+
});
40+
41+
describe('styleext option', () => {
42+
it('should respect the option value', () => {
43+
const tree = runner.runSchematic(
44+
'tree', {styleext: 'scss', ...baseOptions}, createTestApp());
45+
46+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
47+
});
48+
49+
it('should fallback to the @schematics/angular:component option value', () => {
50+
const tree = runner.runSchematic('tree', baseOptions, createTestApp({style: 'less'}));
51+
52+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
53+
});
54+
});
55+
56+
describe('inlineStyle option', () => {
57+
it('should respect the option value', () => {
58+
const tree = runner.runSchematic(
59+
'tree', {inlineStyle: true, ...baseOptions}, createTestApp());
60+
61+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
62+
});
63+
64+
it('should fallback to the @schematics/angular:component option value', () => {
65+
const tree = runner.runSchematic('tree', baseOptions, createTestApp({inlineStyle: true}));
66+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
67+
});
68+
});
69+
70+
describe('inlineTemplate option', () => {
71+
it('should respect the option value', () => {
72+
const tree = runner.runSchematic(
73+
'tree', {inlineTemplate: true, ...baseOptions}, createTestApp());
74+
75+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
76+
});
77+
78+
it('should fallback to the @schematics/angular:component option value', () => {
79+
const tree = runner.runSchematic(
80+
'tree', baseOptions, createTestApp({inlineTemplate: true}));
81+
82+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
83+
});
84+
});
85+
86+
describe('spec option', () => {
87+
it('should respect the option value', () => {
88+
const tree = runner.runSchematic('tree', {spec: false, ...baseOptions}, createTestApp());
89+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
90+
});
91+
92+
it('should fallback to the @schematics/angular:component option value', () => {
93+
const tree = runner.runSchematic('tree', baseOptions, createTestApp({skipTests: true}));
94+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
95+
});
96+
});
97+
});

0 commit comments

Comments
 (0)