UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

121 lines (119 loc) 6.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const ts = require("typescript"); const testHelpers_1 = require("./../testHelpers"); describe("ModifierableNode", () => { describe("getFirstModifierByKind", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); it("should return the modifier when it exists", () => { chai_1.expect(firstChild.getFirstModifierByKind(ts.SyntaxKind.ExportKeyword)).to.not.be.undefined; }); it("should return undefined when the modifier doesn't exist", () => { chai_1.expect(firstChild.getFirstModifierByKind(ts.SyntaxKind.AbstractKeyword)).to.be.undefined; }); }); describe("getFirstModifierByKindOrThrow", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); it("should return the modifier when it exists", () => { chai_1.expect(firstChild.getFirstModifierByKindOrThrow(ts.SyntaxKind.ExportKeyword)).to.not.be.undefined; }); it("should return undefined when the modifier doesn't exist", () => { chai_1.expect(() => firstChild.getFirstModifierByKindOrThrow(ts.SyntaxKind.AbstractKeyword)).to.throw(); }); }); describe("hasModifier", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); describe("providing string", () => { it("should be true when it does", () => { chai_1.expect(firstChild.hasModifier("export")).to.be.true; }); it("should be false when it doesn't", () => { chai_1.expect(firstChild.hasModifier("abstract")).to.be.false; }); }); describe("providing syntax kind", () => { it("should be true when it does", () => { chai_1.expect(firstChild.hasModifier(ts.SyntaxKind.ExportKeyword)).to.be.true; }); it("should be false when it doesn't", () => { chai_1.expect(firstChild.hasModifier(ts.SyntaxKind.AbstractKeyword)).to.be.false; }); }); }); describe("getModifiers", () => { const { firstChild } = testHelpers_1.getInfoFromText("export abstract class Identifier {}"); const modifiers = firstChild.getModifiers(); it("should get all the modifiers", () => { chai_1.expect(modifiers.length).to.equal(2); }); it("should get the right modifiers", () => { chai_1.expect(modifiers[0].getKind()).to.equal(ts.SyntaxKind.ExportKeyword); chai_1.expect(modifiers[1].getKind()).to.equal(ts.SyntaxKind.AbstractKeyword); }); }); describe("addModifier", () => { it("should add a modifier in the correct order in a simple scenario", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.addModifier("abstract"); firstChild.addModifier("export"); chai_1.expect(firstChild.getText()).to.equal("export abstract class Identifier {}"); }); it("should add a modifier in the correct order in an advanced scenario", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.addModifier("export"); firstChild.addModifier("abstract"); firstChild.addModifier("declare"); chai_1.expect(firstChild.getText()).to.equal("export declare abstract class Identifier {}"); }); it("should not add the same modifier twice", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.addModifier("export"); firstChild.addModifier("export"); chai_1.expect(firstChild.getText()).to.equal("export class Identifier {}"); }); it("should add when there's a decorator", () => { const { firstChild } = testHelpers_1.getInfoFromText("@dec class Identifier {}"); firstChild.addModifier("export"); chai_1.expect(firstChild.getText()).to.equal("@dec export class Identifier {}"); }); it("should add when there's a js doc", () => { const { firstChild } = testHelpers_1.getInfoFromText("/** Test */ class Identifier {}"); firstChild.addModifier("export"); chai_1.expect(firstChild.getFullText()).to.equal("/** Test */ export class Identifier {}"); }); }); describe("toggleModifier", () => { it("should add a modifier when toggling on", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.toggleModifier("export"); chai_1.expect(firstChild.getText()).to.equal("export class Identifier {}"); }); it("should remove the modifier when toggling off", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); firstChild.toggleModifier("export"); chai_1.expect(firstChild.getText()).to.equal("class Identifier {}"); }); it("should use the value for toggling that's provided when toggling on and on", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); firstChild.toggleModifier("export", true); chai_1.expect(firstChild.getText()).to.equal("export class Identifier {}"); }); it("should use the value for toggling that's provided when toggling on and off", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.toggleModifier("export", true); chai_1.expect(firstChild.getText()).to.equal("export class Identifier {}"); }); it("should use the value for toggling that's provided when toggling off and on", () => { const { firstChild } = testHelpers_1.getInfoFromText("export class Identifier {}"); firstChild.toggleModifier("export", false); chai_1.expect(firstChild.getText()).to.equal("class Identifier {}"); }); it("should use the value for toggling that's provided when toggling off and off", () => { const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}"); firstChild.toggleModifier("export", false); chai_1.expect(firstChild.getText()).to.equal("class Identifier {}"); }); }); }); //# sourceMappingURL=modifierableNodeTests.js.map