ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
76 lines (74 loc) • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("AsyncableNode", () => {
describe("navigation", () => {
const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("async function Identifier() {}\nfunction Identifier2() {}");
const asyncFunc = mainSourceFile.getFunctions()[0];
const nonAsyncFunc = mainSourceFile.getFunctions()[1];
describe("isAsync", () => {
it("should be async when async", () => {
chai_1.expect(asyncFunc.isAsync()).to.be.true;
});
it("should not be async when not async", () => {
chai_1.expect(nonAsyncFunc.isAsync()).to.be.false;
});
});
describe("getAsyncKeyword", () => {
it("should have a async keyword when async", () => {
chai_1.expect(asyncFunc.getAsyncKeyword().getText()).to.equal("async");
});
it("should not have a async keyword when not async", () => {
chai_1.expect(nonAsyncFunc.getAsyncKeyword()).to.be.undefined;
});
});
describe("getAsyncKeywordOrThrow", () => {
it("should have a async keyword when async", () => {
chai_1.expect(asyncFunc.getAsyncKeywordOrThrow().getText()).to.equal("async");
});
it("should not have a async keyword when not async", () => {
chai_1.expect(() => nonAsyncFunc.getAsyncKeywordOrThrow()).to.throw();
});
});
});
describe("setIsAsync", () => {
function doTest(text, value, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.setIsAsync(value);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should set as async when not async", () => {
doTest("function Identifier() {}", true, "async function Identifier() {}");
});
it("should set as not async when async", () => {
doTest("async function Identifier() {}", false, "function Identifier() {}");
});
});
describe("fill", () => {
function doTest(startCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode);
firstChild.fill(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
}
it("should modify when false and setting true", () => {
doTest("function myFunction() {}", { isAsync: true }, "async function myFunction() {}");
});
it("should modify when true and setting false", () => {
doTest("async function myFunction() {}", { isAsync: false }, "function myFunction() {}");
});
it("should not modify when false and setting false", () => {
doTest("function myFunction() {}", { isAsync: false }, "function myFunction() {}");
});
it("should not modify when true and setting true", () => {
doTest("async function myFunction() {}", { isAsync: true }, "async function myFunction() {}");
});
it("should not modify when false and no property provided", () => {
doTest("function myFunction() {}", {}, "function myFunction() {}");
});
it("should not modify when true and no property provided", () => {
doTest("async function myFunction() {}", {}, "async function myFunction() {}");
});
});
});
//# sourceMappingURL=asyncableNodeTests.js.map