@@ -19,6 +19,7 @@ import {
1919 addSymbolToNgModuleMetadata ,
2020 findNodes ,
2121 insertAfterLastOccurrence ,
22+ insertImport ,
2223} from './ast-utils' ;
2324
2425function getTsSource ( path : string , content : string ) : ts . SourceFile {
@@ -685,4 +686,94 @@ describe('ast utils', () => {
685686 } ) ;
686687 } ) ;
687688 } ) ;
689+
690+ describe ( 'insertImport' , ( ) => {
691+ const filePath = './src/foo.ts' ;
692+
693+ it ( 'should insert a new import into a file' , ( ) => {
694+ const fileContent = '' ;
695+ const source = getTsSource ( filePath , fileContent ) ;
696+ const change = insertImport ( source , filePath , 'Component' , '@angular/core' ) ;
697+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
698+
699+ expect ( result ) . toBe ( `import { Component } from '@angular/core';` ) ;
700+ } ) ;
701+
702+ it ( 'should insert a new import under an alias into a file' , ( ) => {
703+ const fileContent = '' ;
704+ const source = getTsSource ( filePath , fileContent ) ;
705+ const change = insertImport (
706+ source ,
707+ filePath ,
708+ 'Component' ,
709+ '@angular/core' ,
710+ false ,
711+ 'NgComponent' ,
712+ ) ;
713+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
714+
715+ expect ( result ) . toBe ( `import { Component as NgComponent } from '@angular/core';` ) ;
716+ } ) ;
717+
718+ it ( 'should reuse imports from the same module without an alias' , ( ) => {
719+ const fileContent = `import { Pipe } from '@angular/core';` ;
720+ const source = getTsSource ( filePath , fileContent ) ;
721+ const change = insertImport ( source , filePath , 'Component' , '@angular/core' ) ;
722+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
723+
724+ expect ( result ) . toBe ( `import { Pipe, Component } from '@angular/core';` ) ;
725+ } ) ;
726+
727+ it ( 'should reuse imports from the same module with an alias' , ( ) => {
728+ const fileContent = `import { Pipe } from '@angular/core';` ;
729+ const source = getTsSource ( filePath , fileContent ) ;
730+ const change = insertImport (
731+ source ,
732+ filePath ,
733+ 'Component' ,
734+ '@angular/core' ,
735+ false ,
736+ 'NgComponent' ,
737+ ) ;
738+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
739+
740+ expect ( result ) . toBe ( `import { Pipe, Component as NgComponent } from '@angular/core';` ) ;
741+ } ) ;
742+
743+ it ( 'should reuse imports for the same symbol' , ( ) => {
744+ const fileContent = `import { Component } from '@angular/core';` ;
745+ const source = getTsSource ( filePath , fileContent ) ;
746+ const change = insertImport ( source , filePath , 'Component' , '@angular/core' ) ;
747+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
748+
749+ expect ( result ) . toBe ( fileContent ) ;
750+ } ) ;
751+
752+ it ( 'should not insert a new import if the symbol is imported under an alias' , ( ) => {
753+ const fileContent = `import { Component as NgComponent } from '@angular/core';` ;
754+ const source = getTsSource ( filePath , fileContent ) ;
755+ const change = insertImport ( source , filePath , 'Component' , '@angular/core' ) ;
756+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
757+
758+ expect ( result ) . toBe ( fileContent ) ;
759+ } ) ;
760+
761+ it ( 'should insert a new default import into a file' , ( ) => {
762+ const fileContent = '' ;
763+ const source = getTsSource ( filePath , fileContent ) ;
764+ const change = insertImport ( source , filePath , 'core' , '@angular/core' , true ) ;
765+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
766+
767+ expect ( result ) . toBe ( `import core from '@angular/core';` ) ;
768+ } ) ;
769+
770+ it ( 'should not insert an import if there is a namespace import' , ( ) => {
771+ const fileContent = `import * as foo from '@angular/core';` ;
772+ const source = getTsSource ( filePath , fileContent ) ;
773+ const change = insertImport ( source , filePath , 'Component' , '@angular/core' ) ;
774+ const result = applyChanges ( filePath , fileContent , [ change ] ) . trim ( ) ;
775+
776+ expect ( result ) . toBe ( fileContent ) ;
777+ } ) ;
778+ } ) ;
688779} ) ;
0 commit comments