diff --git a/schematics/scully/src/add-blog/index_spec.ts b/schematics/scully/src/add-blog/index_spec.ts new file mode 100644 index 000000000..e55735212 --- /dev/null +++ b/schematics/scully/src/add-blog/index_spec.ts @@ -0,0 +1,38 @@ +import {HostTree} from '@angular-devkit/schematics'; +import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; +import * as path from 'path'; + +import {setupProject} from '../utils/test-utils'; +import {Schema} from './schema'; + +const collectionPath = path.join(__dirname, '../collection.json'); + +describe('add-blog schematic', () => { + const schematicRunner = new SchematicTestRunner('scully-schematics', collectionPath); + const project = 'foo'; + const defaultOptions: Schema = {}; + let appTree: UnitTestTree; + + beforeEach(async () => { + appTree = new UnitTestTree(new HostTree()); + appTree = await setupProject(appTree, schematicRunner, project); + }); + + describe('when using the default options', () => { + beforeEach(async () => { + appTree = await schematicRunner.runSchematicAsync('blog', defaultOptions, appTree).toPromise(); + }); + + it('should have run the markdown schematic', () => { + expect( + schematicRunner.tasks.some( + task => + task.name === 'run-schematic' && + (task.options as any).name === 'create-markdown' && + (task.options as any).options.name === 'blog' && + (task.options as any).options.slug === 'slug' + ) + ).toBe(true); + }); + }); +}); diff --git a/schematics/scully/src/ng-add/index.ts b/schematics/scully/src/ng-add/index.ts index 85146af9b..761fbcb93 100644 --- a/schematics/scully/src/ng-add/index.ts +++ b/schematics/scully/src/ng-add/index.ts @@ -15,6 +15,7 @@ export default (options: Schema): Rule => { addHttpClientModule(options), addPolyfill(options), injectIdleService(options), + runBlogSchematic(options), runScullySchematic(options), ]); }; @@ -134,17 +135,21 @@ const injectIdleService = (options: Schema) => (tree: Tree, context: SchematicCo } }; -const runScullySchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => { +const runBlogSchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => { const nextRules: Rule[] = []; if (options.blog === true) { - // @ts-ignore - nextRules.push(context.addTask(new RunSchematicTask('blog', options), [])); + nextRules.push((host: Tree, ctx: SchematicContext) => { + ctx.addTask(new RunSchematicTask('blog', options), []); + }); } - // tslint:disable-next-line:no-shadowed-variable - nextRules.push((tree: Tree, context: SchematicContext) => { - const installTaskId = context.addTask(new NodePackageInstallTask()); - context.addTask(new RunSchematicTask('run', options), [installTaskId]); - }); + return chain(nextRules); +}; +const runScullySchematic = (options: Schema) => (tree: Tree, context: SchematicContext) => { + const nextRules: Rule[] = []; + nextRules.push((host: Tree, ctx: SchematicContext) => { + const installTaskId = ctx.addTask(new NodePackageInstallTask()); + ctx.addTask(new RunSchematicTask('run', options), [installTaskId]); + }); return chain(nextRules); }; diff --git a/schematics/scully/src/ng-add/index_spec.ts b/schematics/scully/src/ng-add/index_spec.ts index bdb3287e0..c9660d93b 100644 --- a/schematics/scully/src/ng-add/index_spec.ts +++ b/schematics/scully/src/ng-add/index_spec.ts @@ -59,5 +59,25 @@ describe('ng-add schematic', () => { const appModuleContent = getFileContent(appTree, 'src/app/app.component.ts'); expect(appModuleContent).toMatch(/constructor*.*.private idle: IdleMonitorService/s); }); + + it('should run NodePackageInstallTask', () => { + expect(schematicRunner.tasks.some(task => task.name === 'node-package')).toBe(true); + }); + + it('should have run the blog schematic', () => { + expect( + schematicRunner.tasks.some( + task => task.name === 'run-schematic' && (task.options as any).name === 'blog' + ) + ).toBe(true); + }); + + it('should have run the blog schematic', () => { + expect( + schematicRunner.tasks.some( + task => task.name === 'run-schematic' && (task.options as any).name === 'run' + ) + ).toBe(true); + }); }); }); diff --git a/schematics/scully/src/scully/index_spec.ts b/schematics/scully/src/scully/index_spec.ts index 2b91b5c9d..c6e6e4ebe 100644 --- a/schematics/scully/src/scully/index_spec.ts +++ b/schematics/scully/src/scully/index_spec.ts @@ -28,7 +28,7 @@ describe('scully schematic', () => { appTree = await schematicRunner.runSchematicAsync('scully', defaultOptions, appTree).toPromise(); }); - it('add config file', () => { + it('should create the scully config file when not exists', () => { expect(appTree.files).toContain(SCULLY_PATH); }); @@ -39,5 +39,14 @@ describe('scully schematic', () => { expect(scripts.scully).toEqual('scully'); expect(scripts['scully:serve']).toEqual('scully serve'); }); + + it(`should not override an existing scully config file'`, async () => { + appTree = new UnitTestTree(new HostTree()); + appTree = await setupProject(appTree, schematicRunner, project); + appTree.create(SCULLY_PATH, 'foo'); + appTree = await schematicRunner.runSchematicAsync('scully', defaultOptions, appTree).toPromise(); + expect(appTree.files).toContain(PACKAGE_JSON_PATH); + expect(getFileContent(appTree, SCULLY_PATH)).toEqual('foo'); + }); }); });