Skip to content

Commit

Permalink
feat(controller): implement spec file suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
garritfra committed Jun 19, 2022
1 parent 3cc1d89 commit 8e4a812
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/lib/controller/controller.factory.test.ts
Expand Up @@ -230,4 +230,38 @@ describe('Controller Factory', () => {
'export class FooModule {}\n',
);
});
it('should create a spec file', async () => {
const options: ControllerOptions = {
name: 'foo.controller',
spec: true,
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('class', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.controller.spec.ts'),
).not.toBeUndefined();
});
it('should create a spec file with custom file suffix', async () => {
const options: ControllerOptions = {
name: 'foo.controller',
spec: true,
specFileSuffix: 'test',
flat: true,
};
const tree: UnitTestTree = await runner
.runSchematicAsync('class', options)
.toPromise();
const files: string[] = tree.files;

expect(
files.find((filename) => filename === '/foo.controller.spec.ts'),
).toBeUndefined();
expect(
files.find((filename) => filename === '/foo.controller.test.ts'),
).not.toBeUndefined();
});
});
12 changes: 11 additions & 1 deletion src/lib/controller/controller.factory.ts
Expand Up @@ -51,6 +51,10 @@ function transform(source: ControllerOptions): ControllerOptions {
target.language =
target.language !== undefined ? target.language : DEFAULT_LANGUAGE;

target.specFileSuffix = normalizeToKebabOrSnakeCase(
source.specFileSuffix || 'spec',
);

target.path = target.flat
? target.path
: join(target.path as Path, target.name);
Expand All @@ -60,7 +64,13 @@ function transform(source: ControllerOptions): ControllerOptions {
function generate(options: ControllerOptions) {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/controller/controller.schema.d.ts
Expand Up @@ -37,6 +37,10 @@ export interface ControllerOptions {
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Specifies the file suffix of spec files.
*/
specFileSuffix?: string;
/**
* Flag to indicate if a directory is created.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/controller/schema.json
Expand Up @@ -44,6 +44,11 @@
"type": "boolean",
"default": true,
"description": "Specifies if a spec file is generated."
},
"specFileSuffix": {
"type": "string",
"default": "spec",
"description": "Specifies the file suffix of spec files."
}
},
"required": ["name"]
Expand Down

0 comments on commit 8e4a812

Please sign in to comment.