Skip to content

Commit 6ed6557

Browse files
author
monica.lopez-gris
committed
test: repository schematic
1 parent b73ac06 commit 6ed6557

2 files changed

Lines changed: 124 additions & 10 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import * as path from 'path';
3+
4+
describe('Repository Factory', () => {
5+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
6+
it('should manage name only', () => {
7+
const app: object = {
8+
name: '',
9+
};
10+
const options: object = {
11+
name: 'project',
12+
};
13+
runner.runSchematicAsync('application', app).subscribe(tree => {
14+
runner.runSchematicAsync('repository', options, tree).subscribe(tree => {
15+
const files: string[] = tree.files;
16+
expect(files.find(filename => filename === '/src/app/model/entities/project.entity.ts')).toBeDefined();
17+
expect(files.find(filename => filename === '/src/app/repositories/project.repository.ts')).toBeDefined();
18+
expect(tree.readContent('/src/app/model/entities/project.entity.ts')).toEqual(
19+
"import { Entity } from 'typeorm';\n" +
20+
"import { BaseEntity } from '../../../shared/model/entities/base-entity.entity';\n" +
21+
'\n' +
22+
'@Entity()\n' +
23+
'export class Project extends BaseEntity {}\n',
24+
);
25+
expect(tree.readContent('/src/app/repositories/project.repository.ts')).toEqual(
26+
"import { Project } from '../model/entities/project.entity';\n" +
27+
"import { Repository, EntityRepository } from 'typeorm';\n" +
28+
'\n' +
29+
'@EntityRepository(Project)\n' +
30+
'export class ProjectRepository extends Repository<Project> {}\n',
31+
);
32+
});
33+
});
34+
});
35+
it('should manage name and path', () => {
36+
const app: object = {
37+
name: 'app',
38+
};
39+
const options: object = {
40+
name: 'project',
41+
path: 'app',
42+
};
43+
runner.runSchematicAsync('application', app).subscribe(tree => {
44+
runner.runSchematicAsync('repository', options, tree).subscribe(tree => {
45+
const files: string[] = tree.files;
46+
expect(files.find(filename => filename === '/app/src/app/model/entities/project.entity.ts')).toBeDefined();
47+
expect(files.find(filename => filename === '/app/src/app/repositories/project.repository.ts')).toBeDefined();
48+
});
49+
});
50+
});
51+
it('should manage name to dasherize', () => {
52+
const app: object = {
53+
name: '',
54+
};
55+
const options: object = {
56+
name: 'fooBar',
57+
};
58+
runner.runSchematicAsync('application', app).subscribe(tree => {
59+
runner.runSchematicAsync('repository', options, tree).subscribe(tree => {
60+
const files: string[] = tree.files;
61+
expect(files.find(filename => filename === '/src/app/model/entities/foo-bar.entity.ts')).toBeDefined();
62+
expect(files.find(filename => filename === '/src/app/repositories/foo-bar.repository.ts')).toBeDefined();
63+
});
64+
});
65+
});
66+
});
Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,65 @@
1-
import { Tree } from '@angular-devkit/schematics';
21
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
32
import * as path from 'path';
43

5-
const collectionPath = path.join(__dirname, '../collection.json');
4+
describe('Security 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('security', options, tree).subscribe(tree => {
15+
const files: string[] = tree.files;
16+
expect(files.find(filename => filename === '/src/main.ts')).toBeDefined();
17+
expect(files.find(filename => filename === '/package.json')).toBeDefined();
618

7-
describe('schematics', () => {
8-
it('works', done => {
9-
const runner = new SchematicTestRunner('schematics', collectionPath);
10-
const obsTree = runner.runSchematicAsync('schematics', {}, Tree.empty());
11-
12-
obsTree.subscribe(tree => {
13-
expect(tree.files).toEqual([]);
14-
done();
19+
expect(tree.readContent('/package.json')).toContain('@types/helmet');
20+
expect(tree.readContent('/src/main.ts')).toEqual(
21+
"import { NestFactory } from '@nestjs/core';\n" +
22+
"import { AppModule } from './app/app.module';\n" +
23+
"import { WinstonLogger } from './app/shared/logger/winston.logger';\n" +
24+
"import { ValidationPipe } from '@nestjs/common';\n" +
25+
"import * as helmet from 'helmet';\n" +
26+
'\n' +
27+
'async function bootstrap(): Promise<void> {\n' +
28+
' const app = await NestFactory.create(AppModule, { logger: new WinstonLogger() });\n' +
29+
' app.useGlobalPipes(\n' +
30+
' new ValidationPipe({\n' +
31+
' transform: true,\n' +
32+
' }),\n' +
33+
' );\n' +
34+
" app.setGlobalPrefix('v1');\n" +
35+
' app.use(helmet());\n' +
36+
' app.enableCors({\n' +
37+
" origin: '*',\n" +
38+
' credentials: true,\n' +
39+
" exposedHeaders: 'Authorization',\n" +
40+
" allowedHeaders: 'authorization, content-type',\n" +
41+
' });\n' +
42+
' await app.listen(3000);\n' +
43+
'}\n' +
44+
'bootstrap();\n',
45+
);
46+
});
47+
});
48+
});
49+
it('should manage path', () => {
50+
const app: object = {
51+
name: 'foo',
52+
};
53+
const options: object = {
54+
path: 'foo',
55+
};
56+
runner.runSchematicAsync('application', app).subscribe(tree => {
57+
runner.runSchematicAsync('security', options, tree).subscribe(tree => {
58+
const files: string[] = tree.files;
59+
expect(files.find(filename => filename === '/foo/src/main.ts')).toBeDefined();
60+
expect(files.find(filename => filename === '/foo/package.json')).toBeDefined();
61+
expect(tree.readContent('/foo/package.json')).toContain('@types/helmet');
62+
});
1563
});
1664
});
1765
});

0 commit comments

Comments
 (0)