/** * @license * Copyright Google Inc. 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 { getFileContent } from '../utility/test'; import { SchematicTestRunner, UnitTestTree } from "@angular-devkit/schematics/testing"; import { Schema as WorkspaceOptions } from "@schematics/angular/workspace/schema"; import { Schema as ApplicationOptions } from "@schematics/angular/application/schema"; import { Schema as PipeOptions } from '@schematics/angular/pipe/schema'; describe('Pipe Schematic', () => { const schematicRunner = new SchematicTestRunner( 'schematics', require.resolve('../collection.json'), ); const defaultOptions: PipeOptions = { name: 'product', spec: true, module: undefined, export: false, flat: true, project: 'example', }; const workspaceOptions: WorkspaceOptions = { name: 'example', version: '6.0.0', }; const appOptions: ApplicationOptions = { name: 'example', inlineStyle: false, inlineTemplate: false, routing: false, skipTests: false, skipPackageJson: false, }; let appTree: UnitTestTree; beforeEach(() => { appTree = schematicRunner.runExternalSchematic("@schematics/angular", "workspace", workspaceOptions); appTree = schematicRunner.runExternalSchematic("@schematics/angular", "application",appOptions, appTree); }); it('should create a pipe', () => { const options = { ...defaultOptions }; // console.log(`options 52`, options); const tree = schematicRunner.runSchematic('pipe', { ...options, name: 'product/product' }, appTree); const files = tree.files; // expect(files).toContain('/example/src/app/product/product.pipe.spec.ts'); expect(files).toContain('/example/src/app/product/product.pipe.ts'); const moduleContent = getFileContent(tree, '/example/src/app/app.module.ts'); expect(moduleContent).toMatch("import { ProductPipe } from './product/product.pipe'"); expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+ProductPipe\r?\n/m); }); // it('should fail if specified module does not exist', () => { // const options = { ...defaultOptions, module: '/example/src/app/app.module.ts', project: "example" }; // let thrownError: Error | null = null; // try { // schematicRunner.runSchematic('pipe', options, appTree); // } catch (err) { // thrownError = err; // } // expect(thrownError).toBeDefined(); // }); // it('should handle a path in the name and module options', () => { // appTree = schematicRunner.runSchematic( // 'module', // { name: 'admin/module', project: 'bar' }, // appTree, // ); // const options = { ...defaultOptions, module: 'admin/module' }; // appTree = schematicRunner.runSchematic('pipe', options, appTree); // const content = appTree.readContent('/example/src/app/admin/module/module.module.ts'); // expect(content).toMatch(/import { FooPipe } from '\.\.\/\.\.\/foo.pipe'/); // }); it('should export the pipe', () => { const options = { ...defaultOptions, export: true }; const tree = schematicRunner.runSchematic('pipe', options, appTree); const appModuleContent = getFileContent(tree, '/example/src/app/app.module.ts'); expect(appModuleContent).toMatch(/exports: \[ProductPipe\]/); }); });