Skip to content

Commit

Permalink
Adding tests for addComponentToModule
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Aug 10, 2016
1 parent f238c83 commit a566c1c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
2 changes: 1 addition & 1 deletion addon/ng2/blueprints/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts');
const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath);
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
const className = stringUtils.classify(`${options.entity.name}`);
const className = stringUtils.classify(`${options.entity.name}Service`);

if (!options.flat) {
returns.push(addBarrelRegistration(this, this.generatePath));
Expand Down
19 changes: 12 additions & 7 deletions addon/ng2/utilities/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,19 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
// We haven't found the field in the metadata declaration. Insert a new
// field.
let expr = <ts.ObjectLiteralExpression>node;
node = expr.properties[expr.properties.length - 1];
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.startsWith('\n')) {
toInsert = `,${text.match(/^\n(\r?)\s+/)[0]}${metadataField}: [${symbolName}]`;
if (expr.properties.length == 0) {
position = expr.getEnd() - 1;
toInsert = ` ${metadataField}: [${symbolName}]\n`;
} else {
toInsert = `, ${metadataField}: [${symbolName}]`;
node = expr.properties[expr.properties.length - 1];
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.startsWith('\n')) {
toInsert = `,${text.match(/^\n(\r?)\s+/)[0]}${metadataField}: [${symbolName}]`;
} else {
toInsert = `, ${metadataField}: [${symbolName}]`;
}
}
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
// We found the field but it's empty. Insert it just before the `]`.
Expand Down
119 changes: 118 additions & 1 deletion tests/acceptance/ast-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { InsertChange, RemoveChange } from '../../addon/ng2/utilities/change';
import * as Promise from 'ember-cli/lib/ext/promise';
import {
findNodes,
insertAfterLastOccurrence
insertAfterLastOccurrence,
addComponentToModule
} from '../../addon/ng2/utilities/ast-utils';

const readFile = Promise.denodeify(fs.readFile);
Expand Down Expand Up @@ -164,6 +165,122 @@ describe('ast-utils: insertAfterLastOccurrence', () => {
});
});


describe('addComponentToModule', () => {
beforeEach(() => {
mockFs( {
'1.ts': `
import {NgModule} from '@angular/core';
@NgModule({
declarations: []
})
class Module {}`,
'2.ts': `
import {NgModule} from '@angular/core';
@NgModule({
declarations: [
Other
]
})
class Module {}`,
'3.ts': `
import {NgModule} from '@angular/core';
@NgModule({
})
class Module {}`,
'4.ts': `
import {NgModule} from '@angular/core';
@NgModule({
field1: [],
field2: {}
})
class Module {}`
});
});
afterEach(() => mockFs.restore());

it('works with empty array', () => {
return addComponentToModule('1.ts', 'MyClass', 'MyImportPath')
.then(change => change.apply())
.then(() => readFile('1.ts', 'utf-8'))
.then(content => {
expect(content).to.equal(
'\n' +
'import {NgModule} from \'@angular/core\';\n' +
'import { MyClass } from \'MyImportPath\';\n' +
'\n' +
'@NgModule({\n' +
' declarations: [MyClass]\n' +
'})\n' +
'class Module {}'
);
})
});

it('works with array with declarations', () => {
return addComponentToModule('2.ts', 'MyClass', 'MyImportPath')
.then(change => change.apply())
.then(() => readFile('2.ts', 'utf-8'))
.then(content => {
expect(content).to.equal(
'\n' +
'import {NgModule} from \'@angular/core\';\n' +
'import { MyClass } from \'MyImportPath\';\n' +
'\n' +
'@NgModule({\n' +
' declarations: [\n' +
' Other,\n' +
' MyClass\n' +
' ]\n' +
'})\n' +
'class Module {}'
);
})
});

it('works without any declarations', () => {
return addComponentToModule('3.ts', 'MyClass', 'MyImportPath')
.then(change => change.apply())
.then(() => readFile('3.ts', 'utf-8'))
.then(content => {
expect(content).to.equal(
'\n' +
'import {NgModule} from \'@angular/core\';\n' +
'import { MyClass } from \'MyImportPath\';\n' +
'\n' +
'@NgModule({\n' +
' declarations: [MyClass]\n' +
'})\n' +
'class Module {}'
);
})
});

it('works without a declaration field', () => {
return addComponentToModule('4.ts', 'MyClass', 'MyImportPath')
.then(change => change.apply())
.then(() => readFile('4.ts', 'utf-8'))
.then(content => {
expect(content).to.equal(
'\n' +
'import {NgModule} from \'@angular/core\';\n' +
'import { MyClass } from \'MyImportPath\';\n' +
'\n' +
'@NgModule({\n' +
' field1: [],\n' +
' field2: {},\n' +
' declarations: [MyClass]\n' +
'})\n' +
'class Module {}'
);
})
});
});

/**
* Gets node of kind kind from sourceFile
*/
Expand Down

0 comments on commit a566c1c

Please sign in to comment.