|
| 1 | +import {HostTree} from '@angular-devkit/schematics'; |
| 2 | +import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; |
| 3 | +import {getFileContent} from '@schematics/angular/utility/test'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +import {setupProject} from '../utils/test-utils'; |
| 7 | +import {Schema} from './schema'; |
| 8 | + |
| 9 | +const collectionPath = path.join(__dirname, '../collection.json'); |
| 10 | +const META_DATA_TEMPLATE_PATH = 'assets/meta-data-template.yml'; |
| 11 | + |
| 12 | +describe('add-post', () => { |
| 13 | + const schematicRunner = new SchematicTestRunner('scully-schematics', collectionPath); |
| 14 | + const project = 'foo'; |
| 15 | + const defaultOptions: Schema = { |
| 16 | + name: 'Foo barBaz', |
| 17 | + }; |
| 18 | + let appTree: UnitTestTree; |
| 19 | + const expectedFileName = '/blog/foo-bar-baz.md'; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + appTree = new UnitTestTree(new HostTree()); |
| 23 | + appTree = await setupProject(appTree, schematicRunner, project); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('when using the default options', () => { |
| 27 | + beforeEach(async () => { |
| 28 | + appTree = await schematicRunner.runSchematicAsync('post', defaultOptions, appTree).toPromise(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should create a new dasherized post', () => { |
| 32 | + expect(appTree.files).toContain(expectedFileName); |
| 33 | + const mdFileContent = getFileContent(appTree, expectedFileName); |
| 34 | + expect(mdFileContent).toMatch(/title: Foo barBaz/g); |
| 35 | + expect(mdFileContent).toMatch(/description: blog description/g); |
| 36 | + expect(mdFileContent).toMatch(/publish: false/g); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('when using a different `target`', () => { |
| 41 | + beforeEach(async () => { |
| 42 | + appTree = await schematicRunner |
| 43 | + .runSchematicAsync('post', {...defaultOptions, target: 'foo/bar'}, appTree) |
| 44 | + .toPromise(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should create a new dasherized post inside the target dir', () => { |
| 48 | + const expected = '/foo/bar/foo-bar-baz.md'; |
| 49 | + expect(appTree.files).toContain(expected); |
| 50 | + const mdFileContent = getFileContent(appTree, expected); |
| 51 | + expect(mdFileContent).toMatch(/title: Foo barBaz/g); |
| 52 | + expect(mdFileContent).toMatch(/description: blog description/g); |
| 53 | + expect(mdFileContent).toMatch(/publish: false/g); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('when using `metaDataFile` option', () => { |
| 58 | + beforeEach(async () => { |
| 59 | + appTree = await schematicRunner |
| 60 | + .runSchematicAsync('post', {...defaultOptions, metaDataFile: META_DATA_TEMPLATE_PATH}, appTree) |
| 61 | + .toPromise(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should add the meta data but keep title from options', () => { |
| 65 | + expect(appTree.files).toContain(expectedFileName); |
| 66 | + const mdFileContent = getFileContent(appTree, expectedFileName); |
| 67 | + expect(mdFileContent).toMatch(/title: Foo barBaz/g); |
| 68 | + expect(mdFileContent).toMatch(/thumbnail: assets\/images\/default\.jpg/g); |
| 69 | + expect(mdFileContent).toMatch(/author: John Doe/g); |
| 70 | + expect(mdFileContent).toMatch(/mail: John.Doe@example.com/g); |
| 71 | + expect(mdFileContent).toMatch(/keywords:\s+-\ angular\s+-\ scully/s); |
| 72 | + expect(mdFileContent).toMatch(/language: en/g); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments