UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

71 lines (69 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const utils_1 = require("./../../../utils"); const testHelpers_1 = require("./../testHelpers"); describe("StaticableNode", () => { const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("class MyClass { static prop: string; prop2: string; }"); const classDec = mainSourceFile.getClasses()[0]; const staticProp = classDec.getStaticProperties()[0]; const instanceProp = classDec.getInstanceProperties()[0]; describe("isStatic", () => { it("should be static when static", () => { chai_1.expect(staticProp.isStatic()).to.be.true; }); it("should not be static when not static", () => { chai_1.expect(instanceProp.isStatic()).to.be.false; }); }); describe("getStaticKeyword", () => { it("should have a static keyword when static", () => { chai_1.expect(staticProp.getStaticKeyword().getText()).to.equal("static"); }); it("should not have a static keyword when not static", () => { chai_1.expect(instanceProp.getStaticKeyword()).to.be.undefined; }); }); describe("getStaticKeywordOrThrow", () => { it("should have a static keyword when static", () => { chai_1.expect(staticProp.getStaticKeywordOrThrow().getText()).to.equal("static"); }); it("should throw when not static", () => { chai_1.expect(() => instanceProp.getStaticKeywordOrThrow()).to.throw(); }); }); describe("setIsStatic", () => { it("should set as static when not static", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class MyClass { prop: string; }"); firstChild.getInstanceProperties()[0].setIsStatic(true); chai_1.expect(sourceFile.getText()).to.equal("class MyClass { static prop: string; }"); }); it("should set as not static when static", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class MyClass { static prop: string; }"); firstChild.getStaticProperties()[0].setIsStatic(false); chai_1.expect(sourceFile.getText()).to.equal("class MyClass { prop: string; }"); }); }); describe("fill", () => { function getFirstMethod(code) { const result = testHelpers_1.getInfoFromText(code); const firstMethod = result.firstChild.getAllMembers().filter(m => utils_1.TypeGuards.isMethodDeclaration(m))[0]; return Object.assign({ firstMethod }, result); } function doTest(startCode, structure, expectedCode) { const { firstMethod, sourceFile } = getFirstMethod(startCode); firstMethod.fill(structure); chai_1.expect(sourceFile.getText()).to.equal(expectedCode); } it("should not modify anything if the structure doesn't change anything", () => { doTest("class MyClass { method() {} }", {}, "class MyClass { method() {} }"); }); it("should not modify anything if the structure doesn't change anything and the node has everything set", () => { doTest("class MyClass { static method() {} }", {}, "class MyClass { static method() {} }"); }); it("should modify when setting as has declare keyword", () => { doTest("class MyClass { method() {} }", { isStatic: true }, "class MyClass { static method() {} }"); }); }); }); //# sourceMappingURL=staticableNodeTests.js.map