Skip to content

Commit b73ac06

Browse files
author
monica.lopez-gris
committed
test: module schematic
1 parent 43b4c4a commit b73ac06

2 files changed

Lines changed: 246 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import * as path from 'path';
3+
4+
describe('Pipe Factory', () => {
5+
const runner: SchematicTestRunner = new SchematicTestRunner('.', path.join(process.cwd(), 'src/collection.json'));
6+
it('should manage name only', () => {
7+
const options: object = {
8+
name: 'foo',
9+
flat: false,
10+
};
11+
runner.runSchematicAsync('pipe', options).subscribe(tree => {
12+
const files: string[] = tree.files;
13+
expect(files.find(filename => filename === '/app/foo/foo.pipe.ts')).toBeDefined();
14+
expect(tree.readContent('/app/foo/foo.pipe.ts')).toEqual(
15+
"import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';\n" +
16+
'\n' +
17+
'@Injectable()\n' +
18+
'export class FooPipe implements PipeTransform {\n' +
19+
' transform(value: any, metadata: ArgumentMetadata) {\n' +
20+
' return value;\n' +
21+
' }\n' +
22+
'}\n',
23+
);
24+
});
25+
});
26+
it('should manage name as a path', () => {
27+
const options: object = {
28+
name: 'bar/foo',
29+
flat: false,
30+
};
31+
runner.runSchematicAsync('pipe', options).subscribe(tree => {
32+
const files: string[] = tree.files;
33+
expect(files.find(filename => filename === '/app/bar/foo/foo.pipe.ts')).toBeDefined();
34+
expect(tree.readContent('/app/bar/foo/foo.pipe.ts')).toEqual(
35+
"import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';\n" +
36+
'\n' +
37+
'@Injectable()\n' +
38+
'export class FooPipe implements PipeTransform {\n' +
39+
' transform(value: any, metadata: ArgumentMetadata) {\n' +
40+
' return value;\n' +
41+
' }\n' +
42+
'}\n',
43+
);
44+
});
45+
});
46+
it('should manage name and path', () => {
47+
const options: object = {
48+
name: 'foo',
49+
path: 'baz',
50+
flat: false,
51+
};
52+
runner.runSchematicAsync('pipe', options).subscribe(tree => {
53+
const files: string[] = tree.files;
54+
expect(files.find(filename => filename === '/baz/src/app/foo/foo.pipe.ts')).toBeDefined();
55+
expect(tree.readContent('/baz/src/app/foo/foo.pipe.ts')).toEqual(
56+
"import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';\n" +
57+
'\n' +
58+
'@Injectable()\n' +
59+
'export class FooPipe implements PipeTransform {\n' +
60+
' transform(value: any, metadata: ArgumentMetadata) {\n' +
61+
' return value;\n' +
62+
' }\n' +
63+
'}\n',
64+
);
65+
});
66+
});
67+
it('should manage name to dasherize', () => {
68+
const options: object = {
69+
name: 'fooBar',
70+
flat: false,
71+
};
72+
runner.runSchematicAsync('pipe', options).subscribe(tree => {
73+
const files: string[] = tree.files;
74+
expect(files.find(filename => filename === '/app/foo-bar/foo-bar.pipe.ts')).toBeDefined();
75+
expect(tree.readContent('/app/foo-bar/foo-bar.pipe.ts')).toEqual(
76+
"import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';\n" +
77+
'\n' +
78+
'@Injectable()\n' +
79+
'export class FooBarPipe implements PipeTransform {\n' +
80+
' transform(value: any, metadata: ArgumentMetadata) {\n' +
81+
' return value;\n' +
82+
' }\n' +
83+
'}\n',
84+
);
85+
});
86+
});
87+
it('should manage path to dasherize', () => {
88+
const options: object = {
89+
name: 'barBaz/foo',
90+
flat: false,
91+
};
92+
runner.runSchematicAsync('pipe', options).subscribe(tree => {
93+
const files: string[] = tree.files;
94+
expect(files.find(filename => filename === '/app/bar-baz/foo/foo.pipe.ts')).toBeDefined();
95+
expect(tree.readContent('/app/bar-baz/foo/foo.pipe.ts')).toEqual(
96+
"import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';\n" +
97+
'\n' +
98+
'@Injectable()\n' +
99+
'export class FooPipe implements PipeTransform {\n' +
100+
' transform(value: any, metadata: ArgumentMetadata) {\n' +
101+
' return value;\n' +
102+
' }\n' +
103+
'}\n',
104+
);
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)