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
38 changes: 38 additions & 0 deletions src/cdk/schematics/utils/ast/ng-module-imports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {HostTree} from '@angular-devkit/schematics';
import {UnitTestTree} from '@angular-devkit/schematics/testing';
import {hasNgModuleImport} from './ng-module-imports';

describe('NgModule import utils', () => {

let host: UnitTestTree;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
});

describe('hasNgModuleImport', () => {
it('should properly detect imports', () => {
host.create('/test.ts', `
@NgModule({
imports: [TestModule],
})
export class MyModule {}
`);

expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true);
expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false);
});

it(`should detect imports for NgModule's using a namespaced identifier`, () => {
host.create('/test.ts', `
@myImport.NgModule({
imports: [TestModule],
})
export class MyModule {}
`);

expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true);
expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion src/cdk/schematics/utils/ast/ng-module-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function resolveIdentifierOfExpression(expression: ts.Expression): ts.Identifier
if (ts.isIdentifier(expression)) {
return expression;
} else if (ts.isPropertyAccessExpression(expression)) {
return resolveIdentifierOfExpression(expression.expression);
return expression.name;
}
return null;
}
Expand Down