|
| 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 | +}); |
0 commit comments