UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

54 lines (52 loc) 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("PropertyDeclaration", () => { function getFirstPropertyWithInfo(code) { const opts = testHelpers_1.getInfoFromText(code); return Object.assign({}, opts, { firstProperty: opts.firstChild.getInstanceProperties()[0] }); } describe("fill", () => { function doTest(code, structure, expectedCode) { const { firstProperty, sourceFile } = getFirstPropertyWithInfo(code); firstProperty.fill(structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should not change the property when nothing is set", () => { doTest("class Identifier { prop: string; }", {}, "class Identifier { prop: string; }"); }); it("should change the property when setting", () => { doTest("class Identifier { prop: string; }", { type: "number" }, "class Identifier { prop: number; }"); }); }); describe("remove", () => { function doTest(code, nameToRemove, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(code); firstChild.getInstanceProperty(nameToRemove).remove(); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should remove when it's the only property", () => { doTest("class Identifier {\n prop: string;\n}", "prop", "class Identifier {\n}"); }); it("should remove when it's the first property", () => { doTest("class Identifier {\n prop: string;\n prop2: string;\n}", "prop", "class Identifier {\n prop2: string;\n}"); }); it("should remove when it's the middle property", () => { doTest("class Identifier {\n prop: string;\n prop2: string;\n prop3: string;\n}", "prop2", "class Identifier {\n prop: string;\n prop3: string;\n}"); }); it("should remove when it's the last property", () => { doTest("class Identifier {\n prop: string;\n prop2: string;\n}", "prop2", "class Identifier {\n prop: string;\n}"); }); it("should remove when it's beside a method with a body", () => { doTest("class Identifier {\n method(){}\n\n prop: string;\n}", "prop", "class Identifier {\n method(){}\n}"); }); it("should remove when it's inside two methods", () => { doTest("class Identifier {\n method(){}\n\n prop: string;\n\n method2() {}\n}", "prop", "class Identifier {\n method(){}\n\n method2() {}\n}"); }); it("should remove when it's in an ambient class", () => { doTest("declare class Identifier {\n method(): void;\n\n prop: string;\n\n method2(): void;\n}", "prop", "declare class Identifier {\n method(): void;\n method2(): void;\n}"); }); }); }); //# sourceMappingURL=propertyDeclarationTests.js.map