Skip to content
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) %>Interceptor } from './<%= dasherize(name) %>.interceptor';
import { <%= classify(name) %>Interceptor } from './<%= dasherize(name) %><%= typeSeparator %>interceptor';

describe('<%= classify(name) %>Interceptor', () => {
beforeEach(() => TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { HttpInterceptorFn } from '@angular/common/http';

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

describe('<%= camelize(name) %>Interceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) =>
Expand Down
32 changes: 27 additions & 5 deletions packages/schematics/angular/interceptor/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,31 @@ describe('Interceptor Schematic', () => {

const tree = await schematicRunner.runSchematic('interceptor', options, appTree);

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

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

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

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

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

it('should respect the skipTests flag', async () => {
Expand All @@ -57,8 +79,8 @@ describe('Interceptor Schematic', () => {
const tree = await schematicRunner.runSchematic('interceptor', options, appTree);

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

it('should respect the sourceRoot value', async () => {
Expand All @@ -67,7 +89,7 @@ describe('Interceptor Schematic', () => {
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
appTree = await schematicRunner.runSchematic('interceptor', defaultOptions, appTree);

expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.interceptor.ts');
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo-interceptor.ts');
});

it('should create a functional interceptor', async () => {
Expand All @@ -77,7 +99,7 @@ describe('Interceptor Schematic', () => {
appTree,
);

const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo/foo-interceptor.ts');
expect(fileString).toContain(
'export const fooInterceptor: HttpInterceptorFn = (req, next) => {',
);
Expand All @@ -90,7 +112,7 @@ describe('Interceptor Schematic', () => {
appTree,
);

const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
const fileString = tree.readContent('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
expect(fileString).toContain('const interceptor: HttpInterceptorFn = (req, next) => ');
expect(fileString).toContain('TestBed.runInInjectionContext(() => fooInterceptor(req, next));');
});
Expand Down
6 changes: 6 additions & 0 deletions packages/schematics/angular/interceptor/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"type": "boolean",
"description": "Creates the interceptor as a function `HttpInterceptorFn` instead of a class. Functional interceptors can be simpler for basic scenarios.",
"default": true
},
"typeSeparator": {
"type": "string",
"default": "-",
"enum": ["-", "."],
"description": "The separator character to use before the type within the generated file's name. For example, if you set the option to `.`, the file will be named `example.interceptor.ts`."
}
},
"required": ["name", "project"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default function () {
const interceptorDir = join('src', 'app');

return (
ng('generate', 'interceptor', 'test-interceptor')
ng('generate', 'interceptor', 'test')
.then(() => expectFileToExist(interceptorDir))
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.interceptor.ts')))
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.interceptor.spec.ts')))
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.ts')))
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.spec.ts')))

// Try to run the unit tests.
.then(() => ng('test', '--watch=false'))
Expand Down