@@ -16,6 +16,8 @@ import {
1616 addExportToModule ,
1717 addProviderToModule ,
1818 addSymbolToNgModuleMetadata ,
19+ insertAfterLastOccurrence ,
20+ findNodes ,
1921} from './ast-utils' ;
2022
2123
@@ -205,4 +207,45 @@ describe('ast utils', () => {
205207 expect ( output ) . toMatch ( / i m p o r t { L o g S e r v i c e } f r o m ' .\/ l o g .s e r v i c e ' ; / ) ;
206208 expect ( output ) . toMatch ( / \} , \r ? \n \s * L o g S e r v i c e \r ? \n \s * \] / ) ;
207209 } ) ;
210+
211+ describe ( 'insertAfterLastOccurrence' , ( ) => {
212+ const filePath : string = './src/foo.ts' ;
213+
214+ it ( 'should work for the default scenario' , ( ) => {
215+ const fileContent = `const arr = ['foo'];` ;
216+ const source = getTsSource ( filePath , fileContent ) ;
217+ const arrayNode = findNodes ( source . getChildren ( ) . shift ( ) as ts . Node , ts . SyntaxKind . ArrayLiteralExpression ) ;
218+ const elements = ( arrayNode . pop ( ) as ts . ArrayLiteralExpression ) . elements ;
219+
220+ const change = insertAfterLastOccurrence (
221+ elements as any as ts . Node [ ] ,
222+ `, 'bar'` ,
223+ filePath ,
224+ elements . pos ,
225+ ts . SyntaxKind . StringLiteral
226+ ) ;
227+ const output = applyChanges ( filePath , fileContent , [ change ] ) ;
228+
229+ expect ( output ) . toMatch ( / c o n s t a r r = \[ ' f o o ' , ' b a r ' \] ; / ) ;
230+ } ) ;
231+
232+
233+ it ( 'should work without occurrences' , ( ) => {
234+ const fileContent = `const arr = [];` ;
235+ const source = getTsSource ( filePath , fileContent ) ;
236+ const arrayNode = findNodes ( source . getChildren ( ) . shift ( ) as ts . Node , ts . SyntaxKind . ArrayLiteralExpression ) ;
237+ const elements = ( arrayNode . pop ( ) as ts . ArrayLiteralExpression ) . elements ;
238+
239+ const change = insertAfterLastOccurrence (
240+ elements as any as ts . Node [ ] ,
241+ `'bar'` ,
242+ filePath ,
243+ elements . pos ,
244+ ts . SyntaxKind . StringLiteral
245+ ) ;
246+ const output = applyChanges ( filePath , fileContent , [ change ] ) ;
247+
248+ expect ( output ) . toMatch ( / c o n s t a r r = \[ ' b a r ' \] ; / ) ;
249+ } ) ;
250+ } ) ;
208251} ) ;
0 commit comments