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