@@ -6,7 +6,8 @@ import { InsertChange, RemoveChange } from '../../addon/ng2/utilities/change';
6
6
import * as Promise from 'ember-cli/lib/ext/promise' ;
7
7
import {
8
8
findNodes ,
9
- insertAfterLastOccurrence
9
+ insertAfterLastOccurrence ,
10
+ addComponentToModule
10
11
} from '../../addon/ng2/utilities/ast-utils' ;
11
12
12
13
const readFile = Promise . denodeify ( fs . readFile ) ;
@@ -164,6 +165,122 @@ describe('ast-utils: insertAfterLastOccurrence', () => {
164
165
} ) ;
165
166
} ) ;
166
167
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
+
167
284
/**
168
285
* Gets node of kind kind from sourceFile
169
286
*/
0 commit comments