Skip to content

Commit

Permalink
fix(schematics): --routing should add RouterTestingModule in app.comp…
Browse files Browse the repository at this point in the history
…onent.spec.ts

Closes #95
  • Loading branch information
vsavkin committed Nov 17, 2017
1 parent 846d5e4 commit d7fc5b5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
6 changes: 4 additions & 2 deletions e2e/schematics/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ describe('Nrwl Workspace', () => {
ngNew('--collection=@nrwl/schematics --npmScope=nrwl');
copyMissingPackages();
newApp('myapp --routing');
newLib('mylib --ngmodule --routing --lazy --parentModule=apps/myapp/src/app/app.module.ts');
newLib('mylib --routing --lazy --parentModule=apps/myapp/src/app/app.module.ts');

runCLI('build --aot');
expect(runCLI('test --single-run')).toContain('Executed 2 of 2 SUCCESS');
},
100000
);
Expand All @@ -63,9 +64,10 @@ describe('Nrwl Workspace', () => {
ngNew('--collection=@nrwl/schematics --npmScope=nrwl');
copyMissingPackages();
newApp('myapp --routing');
newLib('mylib --ngmodule --routing --parentModule=apps/myapp/src/app/app.module.ts');
newLib('mylib --routing --parentModule=apps/myapp/src/app/app.module.ts');

runCLI('build --aot');
expect(runCLI('test --single-run')).toContain('Executed 2 of 2 SUCCESS');
},
100000
);
Expand Down
12 changes: 11 additions & 1 deletion packages/schematics/src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('app', () => {
});

it('should generate files', () => {
const tree = schematicRunner.runSchematic('app', { name: 'myAPp' }, appTree);
const tree = schematicRunner.runSchematic('app', { name: 'myApp' }, appTree);
expect(tree.exists('apps/my-app/src/main.ts')).toBeTruthy();
expect(tree.exists('apps/my-app/src/app/app.module.ts')).toBeTruthy();
expect(tree.exists('apps/my-app/src/app/app.component.ts')).toBeTruthy();
Expand All @@ -51,4 +51,14 @@ describe('app', () => {
const tree = schematicRunner.runSchematic('app', { name: 'myApp' }, appTree);
expect(getFileContent(tree, 'apps/my-app/src/app/app.module.ts')).toContain('NxModule.forRoot()');
});

describe('routing', () => {
it('should include RouterTestingModule', () => {
const tree = schematicRunner.runSchematic('app', { name: 'myApp', routing: true }, appTree);
expect(getFileContent(tree, 'apps/my-app/src/app/app.module.ts')).toContain('RouterModule.forRoot');
expect(getFileContent(tree, 'apps/my-app/src/app/app.component.spec.ts')).toContain(
'imports: [RouterTestingModule]'
);
});
});
});
15 changes: 14 additions & 1 deletion packages/schematics/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as ts from 'typescript';
import { addBootstrapToModule } from '@schematics/angular/utility/ast-utils';
import { insertImport } from '@schematics/angular/utility/route-utils';
import { serializeJson, addApp } from '../utility/fileutils';
import { addImportToTestBed } from '../utility/ast-utils';

function addBootstrap(path: string): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -91,8 +92,20 @@ function addRouterRootConfiguration(path: string): Rule {
insertImport(sourceFile, modulePath, 'RouterModule', '@angular/router'),
...addImportToModule(sourceFile, modulePath, `RouterModule.forRoot([], {initialNavigation: 'enabled'})`)
]);

const componentSpecPath = `${path}/app/app.component.spec.ts`;
const componentSpecSource = host.read(componentSpecPath)!.toString('utf-8');
const componentSpecSourceFile = ts.createSourceFile(
componentSpecPath,
componentSpecSource,
ts.ScriptTarget.Latest,
true
);
insert(host, componentSpecPath, [
insertImport(componentSpecSourceFile, componentSpecPath, 'RouterTestingModule', '@angular/router/testing'),
...addImportToTestBed(componentSpecSourceFile, componentSpecPath, `RouterTestingModule`)
]);
return host;
// add onSameUrlNavigation: 'reload'
};
}

Expand Down
2 changes: 0 additions & 2 deletions packages/schematics/src/lib/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,4 @@ describe('lib', () => {
});
});
});

// should throw when no --ngmodule
});
34 changes: 27 additions & 7 deletions packages/schematics/src/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ export function addImportToModule(source: ts.SourceFile, modulePath: string, sym
return _addSymbolToNgModuleMetadata(source, modulePath, 'imports', symbolName);
}

export function addImportToTestBed(source: ts.SourceFile, specPath: string, symbolName: string): Change[] {
const allCalls: ts.CallExpression[] = <any>findNodes(source, ts.SyntaxKind.CallExpression);

const configureTestingModuleObjectLiterals = allCalls
.filter(c => c.expression.kind === ts.SyntaxKind.PropertyAccessExpression)
.filter((c: any) => c.expression.name.getText(source) === 'configureTestingModule')
.map(c => (c.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression ? c.arguments[0] : null));

if (configureTestingModuleObjectLiterals.length > 0) {
const startPosition = configureTestingModuleObjectLiterals[0].getFirstToken(source).getEnd();
return [new InsertChange(specPath, startPosition, `imports: [${symbolName}], `)];
} else {
return [];
}
}

export function addReexport(
source: ts.SourceFile,
modulePath: string,
Expand Down Expand Up @@ -269,13 +285,7 @@ export function getBootstrapComponent(source: ts.SourceFile, moduleClassName: st
return bootstrapComponent.getText();
}

function getMatchingProperty(source: ts.SourceFile, property: string): ts.ObjectLiteralElement {
const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
let node: any = nodes[0]; // tslint:disable-line:no-any

if (!node) return null;

// Get all the children property assignment of object literals.
function getMatchingObjectLiteralElement(node: any, source: ts.SourceFile, property: string) {
return (
(node as ts.ObjectLiteralExpression).properties
.filter(prop => prop.kind == ts.SyntaxKind.PropertyAssignment)
Expand All @@ -294,6 +304,16 @@ function getMatchingProperty(source: ts.SourceFile, property: string): ts.Object
);
}

function getMatchingProperty(source: ts.SourceFile, property: string): ts.ObjectLiteralElement {
const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
let node: any = nodes[0]; // tslint:disable-line:no-any

if (!node) return null;

// Get all the children property assignment of object literals.
return getMatchingObjectLiteralElement(node, source, property);
}

export function addRoute(ngModulePath: string, source: ts.SourceFile, route: string): Change[] {
const routes = getListOfRoutes(source);
if (!routes) return [];
Expand Down

0 comments on commit d7fc5b5

Please sign in to comment.