Skip to content

Commit

Permalink
refactor(guard.factory): dasherize to normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlatin Stanimirov committed Mar 5, 2022
1 parent b5d3c3c commit 9df6205
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
66 changes: 52 additions & 14 deletions src/lib/guard/guard.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ describe('Guard Factory', () => {
const options: GuardOptions = {
name: 'foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo.guard.ts'),
files.find((filename) => filename === '/foo.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/foo.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
Expand All @@ -39,10 +41,12 @@ describe('Guard Factory', () => {
const options: GuardOptions = {
name: 'bar/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar/foo.guard.ts'),
files.find((filename) => filename === '/bar/foo.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/bar/foo.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
Expand All @@ -64,10 +68,12 @@ describe('Guard Factory', () => {
name: 'foo',
path: 'baz',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/baz/foo.guard.ts'),
files.find((filename) => filename === '/baz/foo.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/baz/foo.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
Expand All @@ -84,14 +90,16 @@ describe('Guard Factory', () => {
);
});

it('should manage name to dasherize', async () => {
it('should manage name to normalize', async () => {
const options: GuardOptions = {
name: 'fooBar',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo-bar.guard.ts'),
files.find((filename) => filename === '/foo-bar.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/foo-bar.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
Expand All @@ -108,14 +116,16 @@ describe('Guard Factory', () => {
);
});

it('should manage path to dasherize', async () => {
it('should manage path to normalize', async () => {
const options: GuardOptions = {
name: 'barBaz/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar-baz/foo.guard.ts'),
files.find((filename) => filename === '/bar-baz/foo.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/bar-baz/foo.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
Expand All @@ -132,15 +142,43 @@ describe('Guard Factory', () => {
);
});

it('should keep underscore in path and name', async () => {
const options: GuardOptions = {
name: '_foo/_bar',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find((filename) => filename === '/_foo/_bar.guard.ts'),
).not.toBeUndefined();
expect(tree.readContent('/_foo/_bar.guard.ts')).toEqual(
"import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';\n" +
"import { Observable } from 'rxjs';\n" +
'\n' +
'@Injectable()\n' +
'export class BarGuard implements CanActivate {\n' +
' canActivate(\n' +
' context: ExecutionContext,\n' +
' ): boolean | Promise<boolean> | Observable<boolean> {\n' +
' return true;\n' +
' }\n' +
'}\n',
);
});

it('should manage javascript file', async () => {
const options: GuardOptions = {
name: 'foo',
language: 'js',
};
const tree: UnitTestTree = await runner.runSchematicAsync('guard', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('guard', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo.guard.js'),
files.find((filename) => filename === '/foo.guard.js'),
).not.toBeUndefined();
expect(tree.readContent('/foo.guard.js')).toEqual(
"import { Injectable } from '@nestjs/common';\n" +
Expand Down
5 changes: 3 additions & 2 deletions src/lib/guard/guard.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { GuardOptions } from './guard.schema';
Expand All @@ -28,8 +29,8 @@ function transform(options: GuardOptions): GuardOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = strings.dasherize(location.name);
target.path = strings.dasherize(location.path);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.language = target.language !== undefined ? target.language : 'ts';

target.path = target.flat
Expand Down

0 comments on commit 9df6205

Please sign in to comment.