Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@schematics/angular): generate guards with a dash type separator #30029

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { <%= classify(name) %>Guard } from './<%= dasherize(name) %>.guard';
import { <%= classify(name) %>Guard } from './<%= dasherize(name) %><%= typeSeparator %>guard';

describe('<%= classify(name) %>Guard', () => {
let guard: <%= classify(name) %>Guard;
Expand Down
52 changes: 37 additions & 15 deletions packages/schematics/angular/guard/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,62 @@ describe('Guard Schematic', () => {
);

const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo.guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo.guard.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
});

it('should respect the skipTests flag', async () => {
const options = { ...defaultOptions, skipTests: true };

const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).not.toContain('/projects/bar/src/app/foo.guard.spec.ts');
expect(files).not.toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
});

it('should use a `.` type separator when specified', async () => {
const options = { ...defaultOptions, typeSeparator: '.' };

const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo.guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo.guard.ts');
const specContent = tree.readContent('/projects/bar/src/app/foo.guard.spec.ts');
expect(specContent).toContain(`'./foo.guard'`);
});

it('should use a `-` type separator when specified', async () => {
const options = { ...defaultOptions, typeSeparator: '-' };

const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-guard.ts');
const specContent = tree.readContent('/projects/bar/src/app/foo-guard.spec.ts');
expect(specContent).toContain(`'./foo-guard'`);
});

it('should respect the flat flag', async () => {
const options = { ...defaultOptions, flat: false };

const tree = await schematicRunner.runSchematic('guard', options, appTree);
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo/foo.guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo.guard.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo-guard.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo-guard.ts');
});

it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.bar.sourceRoot = 'projects/bar/custom';
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
appTree = await schematicRunner.runSchematic('guard', defaultOptions, appTree);
expect(appTree.files).toContain('/projects/bar/custom/app/foo.guard.ts');
expect(appTree.files).toContain('/projects/bar/custom/app/foo-guard.ts');
});

it('should respect the implements value', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'], functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain('CanActivate');
expect(fileString).toContain('canActivate');
expect(fileString).not.toContain('CanActivateChild');
Expand All @@ -96,7 +118,7 @@ describe('Guard Schematic', () => {
it('should generate a functional guard by default', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain('export const fooGuard: CanActivateFn = (route, state) => {');
expect(fileString).not.toContain('CanActivateChild');
expect(fileString).not.toContain('canActivateChild');
Expand All @@ -107,7 +129,7 @@ describe('Guard Schematic', () => {
it('should generate a helper function to execute the guard in a test', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.spec.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.spec.ts');
expect(fileString).toContain('const executeGuard: CanActivateFn = (...guardParameters) => ');
expect(fileString).toContain(
'TestBed.runInInjectionContext(() => fooGuard(...guardParameters));',
Expand All @@ -117,7 +139,7 @@ describe('Guard Schematic', () => {
it('should generate CanDeactivateFn with unknown functional guard', async () => {
const options = { ...defaultOptions, implements: ['CanDeactivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
expect(fileString).toContain(
'export const fooGuard: CanDeactivateFn<unknown> = ' +
'(component, currentRoute, currentState, nextState) => {',
Expand All @@ -128,7 +150,7 @@ describe('Guard Schematic', () => {
const implementationOptions = ['CanActivate', 'CanDeactivate', 'CanActivateChild'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');

// Should contain all implementations
implementationOptions.forEach((implementation: string) => {
Expand All @@ -142,7 +164,7 @@ describe('Guard Schematic', () => {
const implementationOptions = ['CanMatch'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports = `import { CanMatch, GuardResult, MaybeAsync, Route, subPath } from '@angular/router';`;

expect(fileString).toContain(expectedImports);
Expand All @@ -152,7 +174,7 @@ describe('Guard Schematic', () => {
const implementationOptions = ['CanActivate'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports =
`import { ActivatedRouteSnapshot, CanActivate, GuardResult, ` +
`MaybeAsync, RouterStateSnapshot } from '@angular/router';`;
Expand All @@ -163,7 +185,7 @@ describe('Guard Schematic', () => {
it('should add correct imports based on canActivate functional guard', async () => {
const options = { ...defaultOptions, implements: ['CanActivate'] };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports = `import { CanActivateFn } from '@angular/router';`;

expect(fileString).toContain(expectedImports);
Expand All @@ -173,7 +195,7 @@ describe('Guard Schematic', () => {
const implementationOptions = ['CanActivate', 'CanMatch', 'CanActivateChild'];
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
const tree = await schematicRunner.runSchematic('guard', options, appTree);
const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
const expectedImports =
`import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanMatch, GuardResult, ` +
`MaybeAsync, Route, RouterStateSnapshot, subPath } from '@angular/router';`;
Expand Down
5 changes: 5 additions & 0 deletions packages/schematics/angular/guard/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
},
"default": ["CanActivate"],
"x-prompt": "Which type of guard would you like to create?"
},
"typeSeparator": {
"type": "string",
"default": "-",
"enum": ["-", "."]
}
},
"required": ["name", "project"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { <%= guardType %> } from '@angular/router';

import { <%= camelize(name) %>Guard } from './<%= dasherize(name) %>.guard';
import { <%= camelize(name) %>Guard } from './<%= dasherize(name) %><%= typeSeparator %>guard';

describe('<%= camelize(name) %>Guard', () => {
const executeGuard: <%= guardType %><% if (guardType === 'CanDeactivateFn') { %><unknown><% } %> = (...guardParameters) =>
Expand Down
11 changes: 4 additions & 7 deletions tests/legacy-cli/e2e/tests/generate/guard/guard-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ export default async function () {
// Does not create a sub directory.
const guardDir = join('src', 'app');

await ng('generate', 'guard', 'test-guard');
await ng('generate', 'guard', 'test');
await expectFileToExist(guardDir);
await expectFileToExist(join(guardDir, 'test-guard.guard.ts'));
await expectFileToMatch(
join(guardDir, 'test-guard.guard.ts'),
/export const testGuardGuard: CanActivateFn/,
);
await expectFileToExist(join(guardDir, 'test-guard.guard.spec.ts'));
await expectFileToExist(join(guardDir, 'test-guard.ts'));
await expectFileToMatch(join(guardDir, 'test-guard.ts'), /export const testGuard: CanActivateFn/);
await expectFileToExist(join(guardDir, 'test-guard.spec.ts'));
await ng('test', '--watch=false');
}
6 changes: 3 additions & 3 deletions tests/legacy-cli/e2e/tests/generate/guard/guard-implements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default async function () {

await ng('generate', 'guard', 'match', '--implements=CanMatch');
await expectFileToExist(guardDir);
await expectFileToExist(join(guardDir, 'match.guard.ts'));
await expectFileToMatch(join(guardDir, 'match.guard.ts'), /export const matchGuard: CanMatch/);
await expectFileToExist(join(guardDir, 'match.guard.spec.ts'));
await expectFileToExist(join(guardDir, 'match-guard.ts'));
await expectFileToMatch(join(guardDir, 'match-guard.ts'), /export const matchGuard: CanMatch/);
await expectFileToExist(join(guardDir, 'match-guard.spec.ts'));
await ng('test', '--watch=false');
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export default async function () {
'--no-functional',
);
await expectFileToExist(guardDir);
await expectFileToExist(join(guardDir, 'multiple.guard.ts'));
await expectFileToExist(join(guardDir, 'multiple-guard.ts'));
await expectFileToMatch(
join(guardDir, 'multiple.guard.ts'),
join(guardDir, 'multiple-guard.ts'),
/implements CanActivate, CanDeactivate<unknown>/,
);
await expectFileToExist(join(guardDir, 'multiple.guard.spec.ts'));
await expectFileToExist(join(guardDir, 'multiple-guard.spec.ts'));
await ng('test', '--watch=false');
}