@@ -40,9 +40,26 @@ describe("getOrCreateConstructor", () => {
40
40
41
41
describe ( "addImportToClass" , ( ) => {
42
42
it ( "should add an import to a class" , ( ) => {
43
+ const sourceFileContent = `export class Foo { }` ;
44
+
45
+ const project = new Project ( { useInMemoryFileSystem : true } ) ;
46
+ const sourceFile = project . createSourceFile ( "foo.ts" , sourceFileContent ) ;
47
+
48
+ addImportToClass ( sourceFile , "Component" , "@angular/core" ) ;
49
+
50
+ expect ( dedent ( sourceFile . getText ( ) ) ) . toBe (
51
+ dedent ( `
52
+ import { Component } from "@angular/core";
53
+
54
+ export class Foo { }
55
+ ` ) ,
56
+ ) ;
57
+ } ) ;
58
+
59
+ it ( "should add an import to an existing import declaration" , ( ) => {
43
60
const sourceFileContent = `
44
- export class Foo {
45
- }
61
+ import { Injectable } from "@angular/core";
62
+ export class Foo { }
46
63
` ;
47
64
48
65
const project = new Project ( { useInMemoryFileSystem : true } ) ;
@@ -52,11 +69,42 @@ describe("addImportToClass", () => {
52
69
53
70
expect ( dedent ( sourceFile . getText ( ) ) ) . toBe (
54
71
dedent ( `
55
- import { Component } from "@angular/core";
72
+ import { Injectable, Component } from "@angular/core";
73
+ export class Foo { }
74
+ ` ) ,
75
+ ) ;
76
+ } ) ;
56
77
57
- export class Foo {
58
- }
78
+ it ( "should add multiple imports" , ( ) => {
79
+ const sourceFileContent = `export class Foo { }` ;
80
+
81
+ const project = new Project ( { useInMemoryFileSystem : true } ) ;
82
+ const sourceFile = project . createSourceFile ( "foo.ts" , sourceFileContent ) ;
83
+
84
+ addImportToClass ( sourceFile , [ "Component" , "Injectable" ] , "@angular/core" ) ;
85
+
86
+ expect ( dedent ( sourceFile . getText ( ) ) ) . toBe (
87
+ dedent ( `
88
+ import { Component, Injectable } from "@angular/core";
89
+
90
+ export class Foo { }
59
91
` ) ,
60
92
) ;
61
93
} ) ;
94
+
95
+ it ( 'should do nothing if the source file does not have a class' , ( ) => {
96
+ const sourceFileContent = `
97
+ export function foo() { }
98
+ ` ;
99
+
100
+ const project = new Project ( { useInMemoryFileSystem : true } ) ;
101
+ const sourceFile = project . createSourceFile ( "foo.ts" , sourceFileContent ) ;
102
+
103
+ addImportToClass ( sourceFile , "Component" , "@angular/core" ) ;
104
+
105
+ expect ( dedent ( sourceFile . getText ( ) ) ) . toBe (
106
+ dedent ( `
107
+ export function foo() { }
108
+ ` ) ) ;
109
+ } ) ;
62
110
} ) ;
0 commit comments