Skip to content

Commit 226ecce

Browse files
author
monica.lopez-gris
committed
test: service schematic
1 parent 6ed6557 commit 226ecce

2 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
});
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import * as path from 'path';
3+
4+
describe('Service Factory', () => {
5+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
6+
it('should work', () => {
7+
const app: object = {
8+
name: '',
9+
};
10+
const options: object = {
11+
path: '',
12+
};
13+
runner.runSchematicAsync('application', app).subscribe(tree => {
14+
runner.runSchematicAsync('swagger', options, tree).subscribe(tree => {
15+
const files: string[] = tree.files;
16+
expect(files.find(filename => filename === '/package.json')).toBeDefined();
17+
expect(tree.readContent('/package.json')).toContain('@nestjs/swagger');
18+
expect(tree.readContent('/package.json')).toContain('swagger-ui-express');
19+
expect(tree.readContent('/src/main.ts')).toEqual(
20+
"import { NestFactory } from '@nestjs/core';\n" +
21+
"import { AppModule } from './app/app.module';\n" +
22+
"import { WinstonLogger } from './app/shared/logger/winston.logger';\n" +
23+
"import { ValidationPipe } from '@nestjs/common';\n" +
24+
"import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';\n" +
25+
'\n' +
26+
'async function bootstrap(): Promise<void> {\n' +
27+
' const app = await NestFactory.create(AppModule, { logger: new WinstonLogger() });\n' +
28+
' app.useGlobalPipes(\n' +
29+
' new ValidationPipe({\n' +
30+
' transform: true,\n' +
31+
' }),\n' +
32+
' );\n' +
33+
" app.setGlobalPrefix('v1');\n" +
34+
" if (process.env.NODE_ENV === 'develop') {\n" +
35+
' const options = new DocumentBuilder()\n' +
36+
" .setTitle('NestJS application')\n" +
37+
" .setDescription('')\n" +
38+
" .setVersion('0.0.1')\n" +
39+
' .addBearerAuth()\n' +
40+
' .build();\n' +
41+
'\n' +
42+
' const swaggerDoc = SwaggerModule.createDocument(app, options);\n' +
43+
" SwaggerModule.setup('api', app, swaggerDoc);\n" +
44+
' }\n' +
45+
' await app.listen(3000);\n' +
46+
'}\n' +
47+
'bootstrap();\n',
48+
);
49+
expect(tree.readContent('/nest-cli.json')).toContain('@nestjs/swagger/plugin');
50+
});
51+
});
52+
});
53+
it('should set path', () => {
54+
const app: object = {
55+
name: 'app',
56+
};
57+
const options: object = {
58+
path: 'app',
59+
};
60+
runner.runSchematicAsync('application', app).subscribe(tree => {
61+
runner.runSchematicAsync('swagger', options, tree).subscribe(tree => {
62+
const files: string[] = tree.files;
63+
expect(files.find(filename => filename === '/app/package.json')).toBeDefined();
64+
expect(tree.readContent('/app/package.json')).toContain('@nestjs/swagger');
65+
expect(tree.readContent('/app/package.json')).toContain('swagger-ui-express');
66+
expect(tree.readContent('/app/src/main.ts')).toEqual(
67+
"import { NestFactory } from '@nestjs/core';\n" +
68+
"import { AppModule } from './app/app.module';\n" +
69+
"import { WinstonLogger } from './app/shared/logger/winston.logger';\n" +
70+
"import { ValidationPipe } from '@nestjs/common';\n" +
71+
"import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';\n" +
72+
'\n' +
73+
'async function bootstrap(): Promise<void> {\n' +
74+
' const app = await NestFactory.create(AppModule, { logger: new WinstonLogger() });\n' +
75+
' app.useGlobalPipes(\n' +
76+
' new ValidationPipe({\n' +
77+
' transform: true,\n' +
78+
' }),\n' +
79+
' );\n' +
80+
" app.setGlobalPrefix('v1');\n" +
81+
" if (process.env.NODE_ENV === 'develop') {\n" +
82+
' const options = new DocumentBuilder()\n' +
83+
" .setTitle('NestJS application')\n" +
84+
" .setDescription('')\n" +
85+
" .setVersion('0.0.1')\n" +
86+
' .addBearerAuth()\n' +
87+
' .build();\n' +
88+
'\n' +
89+
' const swaggerDoc = SwaggerModule.createDocument(app, options);\n' +
90+
" SwaggerModule.setup('api', app, swaggerDoc);\n" +
91+
' }\n' +
92+
' await app.listen(3000);\n' +
93+
'}\n' +
94+
'bootstrap();\n',
95+
);
96+
expect(tree.readContent('/app/nest-cli.json')).toContain('@nestjs/swagger/plugin');
97+
});
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)