Skip to content

Commit bd63d98

Browse files
d-koppenhagenjorgeucano
authored andcommitted
refactoring/tests ng add (#176)
* refactor(scully): split calling RunSchematicTask's * test(scully): provide additional tests this tests will verify that the other schematics (blog, scully) as well as the NodePackageInstallTask will be called * test(scully): add test for add-blog schematic * test(scully): verify existing config will kept * style(scully): fix typo
1 parent ea3f37c commit bd63d98

4 files changed

Lines changed: 81 additions & 9 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {HostTree} from '@angular-devkit/schematics';
2+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import * as path from 'path';
4+
5+
import {setupProject} from '../utils/test-utils';
6+
import {Schema} from './schema';
7+
8+
const collectionPath = path.join(__dirname, '../collection.json');
9+
10+
describe('add-blog schematic', () => {
11+
const schematicRunner = new SchematicTestRunner('scully-schematics', collectionPath);
12+
const project = 'foo';
13+
const defaultOptions: Schema = {};
14+
let appTree: UnitTestTree;
15+
16+
beforeEach(async () => {
17+
appTree = new UnitTestTree(new HostTree());
18+
appTree = await setupProject(appTree, schematicRunner, project);
19+
});
20+
21+
describe('when using the default options', () => {
22+
beforeEach(async () => {
23+
appTree = await schematicRunner.runSchematicAsync('blog', defaultOptions, appTree).toPromise();
24+
});
25+
26+
it('should have run the markdown schematic', () => {
27+
expect(
28+
schematicRunner.tasks.some(
29+
task =>
30+
task.name === 'run-schematic' &&
31+
(task.options as any).name === 'create-markdown' &&
32+
(task.options as any).options.name === 'blog' &&
33+
(task.options as any).options.slug === 'slug'
34+
)
35+
).toBe(true);
36+
});
37+
});
38+
});

schematics/scully/src/ng-add/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default (options: Schema): Rule => {
1515
addHttpClientModule(options),
1616
addPolyfill(options),
1717
injectIdleService(options),
18+
runBlogSchematic(options),
1819
runScullySchematic(options),
1920
]);
2021
};
@@ -134,17 +135,21 @@ const injectIdleService = (options: Schema) => (tree: Tree, context: SchematicCo
134135
}
135136
};
136137

137-
const runScullySchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => {
138+
const runBlogSchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => {
138139
const nextRules: Rule[] = [];
139140
if (options.blog === true) {
140-
// @ts-ignore
141-
nextRules.push(context.addTask(new RunSchematicTask('blog', options), []));
141+
nextRules.push((host: Tree, ctx: SchematicContext) => {
142+
ctx.addTask(new RunSchematicTask('blog', options), []);
143+
});
142144
}
143-
// tslint:disable-next-line:no-shadowed-variable
144-
nextRules.push((tree: Tree, context: SchematicContext) => {
145-
const installTaskId = context.addTask(new NodePackageInstallTask());
146-
context.addTask(new RunSchematicTask('run', options), [installTaskId]);
147-
});
145+
return chain(nextRules);
146+
};
148147

148+
const runScullySchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => {
149+
const nextRules: Rule[] = [];
150+
nextRules.push((host: Tree, ctx: SchematicContext) => {
151+
const installTaskId = ctx.addTask(new NodePackageInstallTask());
152+
ctx.addTask(new RunSchematicTask('run', options), [installTaskId]);
153+
});
149154
return chain(nextRules);
150155
};

schematics/scully/src/ng-add/index_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,25 @@ describe('ng-add schematic', () => {
5959
const appModuleContent = getFileContent(appTree, 'src/app/app.component.ts');
6060
expect(appModuleContent).toMatch(/constructor*.*.private idle: IdleMonitorService/s);
6161
});
62+
63+
it('should run NodePackageInstallTask', () => {
64+
expect(schematicRunner.tasks.some(task => task.name === 'node-package')).toBe(true);
65+
});
66+
67+
it('should have run the blog schematic', () => {
68+
expect(
69+
schematicRunner.tasks.some(
70+
task => task.name === 'run-schematic' && (task.options as any).name === 'blog'
71+
)
72+
).toBe(true);
73+
});
74+
75+
it('should have run the blog schematic', () => {
76+
expect(
77+
schematicRunner.tasks.some(
78+
task => task.name === 'run-schematic' && (task.options as any).name === 'run'
79+
)
80+
).toBe(true);
81+
});
6282
});
6383
});

schematics/scully/src/scully/index_spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('scully schematic', () => {
2828
appTree = await schematicRunner.runSchematicAsync('scully', defaultOptions, appTree).toPromise();
2929
});
3030

31-
it('add config file', () => {
31+
it('should create the scully config file when not exists', () => {
3232
expect(appTree.files).toContain(SCULLY_PATH);
3333
});
3434

@@ -39,5 +39,14 @@ describe('scully schematic', () => {
3939
expect(scripts.scully).toEqual('scully');
4040
expect(scripts['scully:serve']).toEqual('scully serve');
4141
});
42+
43+
it(`should not override an existing scully config file'`, async () => {
44+
appTree = new UnitTestTree(new HostTree());
45+
appTree = await setupProject(appTree, schematicRunner, project);
46+
appTree.create(SCULLY_PATH, 'foo');
47+
appTree = await schematicRunner.runSchematicAsync('scully', defaultOptions, appTree).toPromise();
48+
expect(appTree.files).toContain(PACKAGE_JSON_PATH);
49+
expect(getFileContent(appTree, SCULLY_PATH)).toEqual('foo');
50+
});
4251
});
4352
});

0 commit comments

Comments
 (0)