Skip to content

Commit 8295368

Browse files
committed
test: typescript utils
1 parent bbb3ce9 commit 8295368

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

packages/cli/src/angular/utils/typescript-utils.test.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,26 @@ describe("getOrCreateConstructor", () => {
4040

4141
describe("addImportToClass", () => {
4242
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", () => {
4360
const sourceFileContent = `
44-
export class Foo {
45-
}
61+
import { Injectable } from "@angular/core";
62+
export class Foo { }
4663
`;
4764

4865
const project = new Project({ useInMemoryFileSystem: true });
@@ -52,11 +69,42 @@ describe("addImportToClass", () => {
5269

5370
expect(dedent(sourceFile.getText())).toBe(
5471
dedent(`
55-
import { Component } from "@angular/core";
72+
import { Injectable, Component } from "@angular/core";
73+
export class Foo { }
74+
`),
75+
);
76+
});
5677

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 { }
5991
`),
6092
);
6193
});
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+
});
62110
});

0 commit comments

Comments
 (0)