ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
164 lines (162 loc) • 8.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("ExportDeclaration", () => {
describe("isNamespaceExport", () => {
function doTest(text, expected) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.isNamespaceExport()).to.equal(expected);
}
it("should be a namespace export when is one", () => {
doTest("export * from './test'", true);
});
it("should not be a namespace export when is a named export", () => {
doTest(`export {name} from "./test"`, false);
});
});
describe("hasNamedExports", () => {
function doTest(text, expected) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.hasNamedExports()).to.equal(expected);
}
it("should not have any named exports when is a namespace export", () => {
doTest("export * from './test'", false);
});
it("should have named exports when has one", () => {
doTest(`export {name} from "./test"`, true);
});
});
describe("setModuleSpecifier", () => {
function doTest(text, newModuleSpecifier, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.setModuleSpecifier(newModuleSpecifier);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should set the module specifier when using single quotes", () => {
doTest(`export * from './test';`, "./new-test", `export * from './new-test';`);
});
it("should set the module specifier when using double quotes", () => {
doTest(`export * from "./test";`, "./new-test", `export * from "./new-test";`);
});
it("should set the module specifier when it's empty", () => {
doTest(`export * from "";`, "./new-test", `export * from "./new-test";`);
});
it("should set the module specifier when it doesn't exist", () => {
doTest(`export {test};`, "./new-test", `export {test} from "./new-test";`);
});
it("should set the module specifier when it doesn't exist and there's no semi-colon", () => {
doTest(`export {test}`, "./new-test", `export {test} from "./new-test"`);
});
});
describe("getModuleSpecifier", () => {
function doTest(text, expected) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.getModuleSpecifier()).to.equal(expected);
}
it("should get the module specifier when using single quotes", () => {
doTest("export * from './test'", "./test");
});
it("should get the module specifier when using double quotes", () => {
doTest(`export {name} from "./test"`, "./test");
});
it("should return undefined when it doesn't exist", () => {
doTest(`export {name}`, undefined);
});
});
describe("hasModuleSpecifier", () => {
function doTest(text, expected) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.hasModuleSpecifier()).to.equal(expected);
}
it("should have a module specifier when using single quotes", () => {
doTest("export * from './test'", true);
});
it("should have a module specifier when using double quotes", () => {
doTest(`export {name} from "./test"`, true);
});
it("should not have a module specifier when one doesn't exist", () => {
doTest(`export {name}`, false);
});
});
describe("getNamedExports", () => {
function doTest(text, expected) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
const namedExports = firstChild.getNamedExports();
chai_1.expect(namedExports.length).to.equal(expected.length);
for (let i = 0; i < namedExports.length; i++) {
chai_1.expect(namedExports[i].getName().getText()).to.equal(expected[i].name);
if (expected[i].alias == null)
chai_1.expect(namedExports[i].getAlias()).to.equal(undefined);
else
chai_1.expect(namedExports[i].getAlias().getText()).to.equal(expected[i].alias);
}
}
it("should get the named exports", () => {
doTest(`export {name, name2, name3 as name4} from "./test";`, [{ name: "name" }, { name: "name2" }, { name: "name3", alias: "name4" }]);
});
it("should not get anything when only a namespace export exists", () => {
doTest(`export * from "./test";`, []);
});
});
describe("insertNamedExports", () => {
function doTest(text, index, structures, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.insertNamedExports(index, structures);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should insert named exports when doing a namespace export", () => {
doTest(`export * from "./test";`, 0, [{ name: "name", alias: "alias" }], `export {name as alias} from "./test";`);
});
it("should insert named exports at the start", () => {
doTest(`export {name3} from "./test";`, 0, [{ name: "name1" }, { name: "name2" }], `export {name1, name2, name3} from "./test";`);
});
it("should insert named exports at the end", () => {
doTest(`export {name1} from "./test";`, 1, [{ name: "name2" }, { name: "name3" }], `export {name1, name2, name3} from "./test";`);
});
it("should insert named exports in the middle", () => {
doTest(`export {name1, name4} from "./test";`, 1, [{ name: "name2" }, { name: "name3" }], `export {name1, name2, name3, name4} from "./test";`);
});
});
describe("insertNamedExport", () => {
function doTest(text, index, structure, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.insertNamedExport(index, structure);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should insert at the specified index", () => {
doTest(`export {name1, name3} from "./test";`, 1, { name: "name2" }, `export {name1, name2, name3} from "./test";`);
});
});
describe("addNamedExport", () => {
function doTest(text, structure, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.addNamedExport(structure);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should add at the end", () => {
doTest(`export {name1, name2} from "./test";`, { name: "name3" }, `export {name1, name2, name3} from "./test";`);
});
});
describe("addNamedExports", () => {
function doTest(text, structures, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
firstChild.addNamedExports(structures);
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should add named exports at the end", () => {
doTest(`export {name1} from "./test";`, [{ name: "name2" }, { name: "name3" }], `export {name1, name2, name3} from "./test";`);
});
});
describe("remove", () => {
function doTest(text, index, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getExports()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove the export declaration", () => {
doTest("export * from 'i';\nexport * from 'j';\nexport * from 'k';\n", 1, "export * from 'i';\nexport * from 'k';\n");
});
});
});
//# sourceMappingURL=exportDeclarationTests.js.map