Skip to content

Commit 39c0713

Browse files
committed
Adding tests for addComponentToModule
1 parent f238c83 commit 39c0713

File tree

4 files changed

+133
-11
lines changed

4 files changed

+133
-11
lines changed

addon/ng2/blueprints/service/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = {
5757
const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts');
5858
const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath);
5959
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
60-
const className = stringUtils.classify(`${options.entity.name}`);
60+
const className = stringUtils.classify(`${options.entity.name}Service`);
6161

6262
if (!options.flat) {
6363
returns.push(addBarrelRegistration(this, this.generatePath));

addon/ng2/utilities/ast-utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,19 @@ function _addSymbolToNgModuleMetadata(ngModulePath: string, metadataField: strin
221221
// We haven't found the field in the metadata declaration. Insert a new
222222
// field.
223223
let expr = <ts.ObjectLiteralExpression>node;
224-
node = expr.properties[expr.properties.length - 1];
225-
position = node.getEnd();
226-
// Get the indentation of the last element, if any.
227-
const text = node.getFullText(source);
228-
if (text.startsWith('\n')) {
229-
toInsert = `,${text.match(/^\n(\r?)\s+/)[0]}${metadataField}: [${symbolName}]`;
224+
if (expr.properties.length == 0) {
225+
position = expr.getEnd() - 1;
226+
toInsert = ` ${metadataField}: [${symbolName}]\n`;
230227
} else {
231-
toInsert = `, ${metadataField}: [${symbolName}]`;
228+
node = expr.properties[expr.properties.length - 1];
229+
position = node.getEnd();
230+
// Get the indentation of the last element, if any.
231+
const text = node.getFullText(source);
232+
if (text.startsWith('\n')) {
233+
toInsert = `,${text.match(/^\n(\r?)\s+/)[0]}${metadataField}: [${symbolName}]`;
234+
} else {
235+
toInsert = `, ${metadataField}: [${symbolName}]`;
236+
}
232237
}
233238
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
234239
// We found the field but it's empty. Insert it just before the `]`.

tests/acceptance/ast-utils.spec.ts

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { InsertChange, RemoveChange } from '../../addon/ng2/utilities/change';
66
import * as Promise from 'ember-cli/lib/ext/promise';
77
import {
88
findNodes,
9-
insertAfterLastOccurrence
9+
insertAfterLastOccurrence,
10+
addComponentToModule
1011
} from '../../addon/ng2/utilities/ast-utils';
1112

1213
const readFile = Promise.denodeify(fs.readFile);
@@ -164,6 +165,122 @@ describe('ast-utils: insertAfterLastOccurrence', () => {
164165
});
165166
});
166167

168+
169+
describe('addComponentToModule', () => {
170+
beforeEach(() => {
171+
mockFs( {
172+
'1.ts': `
173+
import {NgModule} from '@angular/core';
174+
175+
@NgModule({
176+
declarations: []
177+
})
178+
class Module {}`,
179+
'2.ts': `
180+
import {NgModule} from '@angular/core';
181+
182+
@NgModule({
183+
declarations: [
184+
Other
185+
]
186+
})
187+
class Module {}`,
188+
'3.ts': `
189+
import {NgModule} from '@angular/core';
190+
191+
@NgModule({
192+
})
193+
class Module {}`,
194+
'4.ts': `
195+
import {NgModule} from '@angular/core';
196+
197+
@NgModule({
198+
field1: [],
199+
field2: {}
200+
})
201+
class Module {}`
202+
});
203+
});
204+
afterEach(() => mockFs.restore());
205+
206+
it('works with empty array', () => {
207+
return addComponentToModule('1.ts', 'MyClass', 'MyImportPath')
208+
.then(change => change.apply())
209+
.then(() => readFile('1.ts', 'utf-8'))
210+
.then(content => {
211+
expect(content).to.equal(
212+
'\n' +
213+
'import {NgModule} from \'@angular/core\';\n' +
214+
'import { MyClass } from \'MyImportPath\';\n' +
215+
'\n' +
216+
'@NgModule({\n' +
217+
' declarations: [MyClass]\n' +
218+
'})\n' +
219+
'class Module {}'
220+
);
221+
})
222+
});
223+
224+
it('works with array with declarations', () => {
225+
return addComponentToModule('2.ts', 'MyClass', 'MyImportPath')
226+
.then(change => change.apply())
227+
.then(() => readFile('2.ts', 'utf-8'))
228+
.then(content => {
229+
expect(content).to.equal(
230+
'\n' +
231+
'import {NgModule} from \'@angular/core\';\n' +
232+
'import { MyClass } from \'MyImportPath\';\n' +
233+
'\n' +
234+
'@NgModule({\n' +
235+
' declarations: [\n' +
236+
' Other,\n' +
237+
' MyClass\n' +
238+
' ]\n' +
239+
'})\n' +
240+
'class Module {}'
241+
);
242+
})
243+
});
244+
245+
it('works without any declarations', () => {
246+
return addComponentToModule('3.ts', 'MyClass', 'MyImportPath')
247+
.then(change => change.apply())
248+
.then(() => readFile('3.ts', 'utf-8'))
249+
.then(content => {
250+
expect(content).to.equal(
251+
'\n' +
252+
'import {NgModule} from \'@angular/core\';\n' +
253+
'import { MyClass } from \'MyImportPath\';\n' +
254+
'\n' +
255+
'@NgModule({\n' +
256+
' declarations: [MyClass]\n' +
257+
'})\n' +
258+
'class Module {}'
259+
);
260+
})
261+
});
262+
263+
it('works without a declaration field', () => {
264+
return addComponentToModule('4.ts', 'MyClass', 'MyImportPath')
265+
.then(change => change.apply())
266+
.then(() => readFile('4.ts', 'utf-8'))
267+
.then(content => {
268+
expect(content).to.equal(
269+
'\n' +
270+
'import {NgModule} from \'@angular/core\';\n' +
271+
'import { MyClass } from \'MyImportPath\';\n' +
272+
'\n' +
273+
'@NgModule({\n' +
274+
' field1: [],\n' +
275+
' field2: {},\n' +
276+
' declarations: [MyClass]\n' +
277+
'})\n' +
278+
'class Module {}'
279+
);
280+
})
281+
});
282+
});
283+
167284
/**
168285
* Gets node of kind kind from sourceFile
169286
*/

tests/acceptance/generate-service.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ describe('Acceptance: ng generate service', function () {
4343
.then(() => expect(existsSync(testPath)).to.equal(true))
4444
.then(() => readFile(appModulePath, 'utf-8'))
4545
.then(content => {
46-
expect(content).matches(/import.*\bMySvc\b.*from '.\/my-svc.service';/);
47-
expect(content).matches(/providers:\s*\[MySvc\]/m);
46+
expect(content).matches(/import.*\MySvcService\b.*from '.\/my-svc.service';/);
47+
expect(content).matches(/providers:\s*\[MySvcService\]/m);
4848
});
4949
});
5050

0 commit comments

Comments
 (0)