UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

71 lines (69 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const testHelpers_1 = require("./../testHelpers"); describe("ReadonlyableNode", () => { function getInfoWithFirstPropertyFromText(text) { const result = testHelpers_1.getInfoFromText(text); return Object.assign({}, result, { firstProperty: result.firstChild.getInstanceProperties()[0] }); } describe("isReadonly", () => { it("should be readonly when readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nreadonly prop: string;}\n"); chai_1.expect(firstProperty.isReadonly()).to.be.true; }); it("should not be readonly when not readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n"); chai_1.expect(firstProperty.isReadonly()).to.be.false; }); }); describe("getReadonlyKeyword", () => { it("should be get the readonly keyword when readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nreadonly prop: string;}\n"); chai_1.expect(firstProperty.getReadonlyKeyword().getText()).to.equal("readonly"); }); it("should return undefined when not readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n"); chai_1.expect(firstProperty.getReadonlyKeyword()).to.be.undefined; }); }); describe("getReadonlyKeywordOrThrow", () => { it("should be get the readonly keyword when readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nreadonly prop: string;}\n"); chai_1.expect(firstProperty.getReadonlyKeywordOrThrow().getText()).to.equal("readonly"); }); it("should throw when not readonly", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class MyClass {\nprop: string;}\n"); chai_1.expect(() => firstProperty.getReadonlyKeywordOrThrow()).to.throw(); }); }); describe("setIsReadonly", () => { it("should set as readonly when not readonly", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class MyClass { prop: string; }"); firstChild.getInstanceProperties()[0].setIsReadonly(true); chai_1.expect(sourceFile.getText()).to.equal("class MyClass { readonly prop: string; }"); }); it("should set as not readonly when readonly", () => { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("class MyClass { readonly prop: string; }"); firstChild.getInstanceProperties()[0].setIsReadonly(false); chai_1.expect(sourceFile.getText()).to.equal("class MyClass { prop: string; }"); }); }); describe("fill", () => { function doTest(startCode, structure, expectedCode) { const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText(startCode); firstProperty.fill(structure); chai_1.expect(sourceFile.getText()).to.equal(expectedCode); } it("should not modify when not set and structure empty", () => { doTest("class MyClass { prop: string; }", {}, "class MyClass { prop: string; }"); }); it("should not modify when set and structure empty", () => { doTest("class MyClass { readonly prop: string; }", {}, "class MyClass { readonly prop: string; }"); }); it("should modify when setting true", () => { doTest("class MyClass { prop: string; }", { isReadonly: true }, "class MyClass { readonly prop: string; }"); }); }); }); //# sourceMappingURL=readonlyableNodeTests.js.map