|
| 1 | +import { normalize } from '@angular-devkit/core'; |
| 2 | +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +describe('Module Factory', () => { |
| 6 | + const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json')); |
| 7 | + it('should manage name only', () => { |
| 8 | + const options: object = { |
| 9 | + name: 'foo', |
| 10 | + skipImport: true, |
| 11 | + }; |
| 12 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 13 | + const files: string[] = tree.files; |
| 14 | + expect(files.find(filename => filename === '/app/foo/foo.module.ts')).toBeDefined(); |
| 15 | + expect(tree.readContent('/app/foo/foo.module.ts')).toEqual( |
| 16 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooModule {}\n', |
| 17 | + ); |
| 18 | + }); |
| 19 | + }); |
| 20 | + it('should manage name as a path', () => { |
| 21 | + const options: object = { |
| 22 | + name: 'bar/foo', |
| 23 | + skipImport: true, |
| 24 | + }; |
| 25 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 26 | + const files: string[] = tree.files; |
| 27 | + expect(files.find(filename => filename === '/app/bar/foo/foo.module.ts')).toBeDefined(); |
| 28 | + expect(tree.readContent('/app/bar/foo/foo.module.ts')).toEqual( |
| 29 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooModule {}\n', |
| 30 | + ); |
| 31 | + }); |
| 32 | + }); |
| 33 | + it('should manage name and path', () => { |
| 34 | + const options: object = { |
| 35 | + name: 'foo', |
| 36 | + path: 'bar', |
| 37 | + skipImport: true, |
| 38 | + }; |
| 39 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 40 | + const files: string[] = tree.files; |
| 41 | + expect(files.find(filename => filename === '/bar/app/foo/foo.module.ts')).toBeDefined(); |
| 42 | + expect(tree.readContent('/bar/app/foo/foo.module.ts')).toEqual( |
| 43 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooModule {}\n', |
| 44 | + ); |
| 45 | + }); |
| 46 | + }); |
| 47 | + it('should manage name to dasherize', () => { |
| 48 | + const options: object = { |
| 49 | + name: 'fooBar', |
| 50 | + skipImport: true, |
| 51 | + }; |
| 52 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 53 | + const files: string[] = tree.files; |
| 54 | + expect(files.find(filename => filename === '/app/foo-bar/foo-bar.module.ts')).toBeDefined(); |
| 55 | + expect(tree.readContent('/app/foo-bar/foo-bar.module.ts')).toEqual( |
| 56 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooBarModule {}\n', |
| 57 | + ); |
| 58 | + }); |
| 59 | + }); |
| 60 | + it('should manage name and path', () => { |
| 61 | + const options: object = { |
| 62 | + name: 'foo', |
| 63 | + path: 'bar', |
| 64 | + skipImport: true, |
| 65 | + }; |
| 66 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 67 | + const files: string[] = tree.files; |
| 68 | + expect(files.find(filename => filename === '/bar/app/foo/foo.module.ts')).toBeDefined(); |
| 69 | + expect(tree.readContent('/bar/app/foo/foo.module.ts')).toEqual( |
| 70 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooModule {}\n', |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | + it('should manage path to dasherize', () => { |
| 75 | + const options: object = { |
| 76 | + name: 'barBaz/foo', |
| 77 | + skipImport: true, |
| 78 | + }; |
| 79 | + runner.runSchematicAsync('module', options).subscribe(tree => { |
| 80 | + const files: string[] = tree.files; |
| 81 | + expect(files.find(filename => filename === '/app/bar-baz/foo/foo.module.ts')).toBeDefined(); |
| 82 | + expect(tree.readContent('/app/bar-baz/foo/foo.module.ts')).toEqual( |
| 83 | + "import { Module } from '@nestjs/common';\n" + '\n' + '@Module({})\n' + 'export class FooModule {}\n', |
| 84 | + ); |
| 85 | + }); |
| 86 | + }); |
| 87 | + it('should manage declaration in app module', () => { |
| 88 | + const app: object = { |
| 89 | + name: '', |
| 90 | + }; |
| 91 | + const options: object = { |
| 92 | + name: 'foo', |
| 93 | + }; |
| 94 | + runner.runSchematicAsync('application', app).subscribe(tree => { |
| 95 | + runner.runSchematicAsync('module', options, tree).subscribe(tree => { |
| 96 | + expect(tree.readContent(normalize('/src/app/app.module.ts'))).toEqual( |
| 97 | + "import { Module } from '@nestjs/common';\n" + |
| 98 | + "import { AppController } from './app.controller';\n" + |
| 99 | + "import { AppService } from './app.service';\n" + |
| 100 | + "import { CoreModule } from './core/core.module';\n" + |
| 101 | + "import { FooModule } from './foo/foo.module';\n" + |
| 102 | + '\n' + |
| 103 | + '@Module({\n' + |
| 104 | + ' imports: [CoreModule, FooModule],\n' + |
| 105 | + ' controllers: [AppController],\n' + |
| 106 | + ' providers: [AppService],\n' + |
| 107 | + '})\n' + |
| 108 | + 'export class AppModule {}\n', |
| 109 | + ); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + it('should manage declaration in bar module', () => { |
| 114 | + const app: object = { |
| 115 | + name: '', |
| 116 | + }; |
| 117 | + const optionsModule: object = { |
| 118 | + name: 'bar', |
| 119 | + }; |
| 120 | + const options: object = { |
| 121 | + name: 'bar/foo', |
| 122 | + }; |
| 123 | + runner.runSchematicAsync('application', app).subscribe(tree => { |
| 124 | + runner.runSchematicAsync('module', optionsModule, tree).subscribe(tree => { |
| 125 | + runner.runSchematicAsync('module', options, tree).subscribe(tree => { |
| 126 | + expect(tree.readContent(normalize('/src/app/bar/bar.module.ts'))).toEqual( |
| 127 | + "import { Module } from '@nestjs/common';\n" + |
| 128 | + "import { FooModule } from './foo/foo.module';\n" + |
| 129 | + '\n' + |
| 130 | + '@Module({\n' + |
| 131 | + ' imports: [FooModule]\n' + |
| 132 | + '})\n' + |
| 133 | + 'export class BarModule {}\n', |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments