jest-bdd-generator
Version:
Jest code generator empowered with Gherkin and BDD
217 lines (216 loc) • 8.1 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var TestGeneration_exports = {};
__export(TestGeneration_exports, {
TestGeneration: () => TestGeneration
});
module.exports = __toCommonJS(TestGeneration_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_JestToGherkin = require("../jest-to-gherkin/JestToGherkin");
var import_TestGeneratorFromSource = require("../gherkin-to-test/TestGeneratorFromSource");
var import_react2 = require("@monaco-editor/react");
const LineWrapper = (0, import_styled_components.default)("div")`
display: flex;
> * {
flex: 1 1 auto;
}
`;
const ColumnWrapper = (0, import_styled_components.default)("div")`
display: flex;
flex-direction: column;
`;
const SourceCodeEditor = ({ updateHandler, type, value }) => {
const [inJestSource, setEditor] = (0, import_react.useState)();
const fileJestSource = (0, import_react.useRef)(null);
const handleFile = (evt) => {
if (!evt.target.files) {
return;
}
const file = evt.target.files[0];
void file.text().then((val) => {
updateHandler(val);
if (inJestSource) {
inJestSource.setValue(val);
}
});
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ColumnWrapper, {
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
ref: fileJestSource,
onChange: handleFile,
type: "file"
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react2.Editor, {
value,
height: "40vh",
language: type !== null && type !== void 0 ? type : "typescript",
onMount: (editor) => setEditor(editor),
onChange: (v) => updateHandler(v !== null && v !== void 0 ? v : "")
})
]
});
};
const getUniqueStep = (steps) => {
const uniqueSteps = steps.filter((s, i) => steps.findIndex((f) => f.key === s.key && f.value === s.value) === i);
return uniqueSteps.map((s) => {
if (s.sourceCode) {
var _s_sourceCode_statements;
const head = "";
const tail = "";
const body = (_s_sourceCode_statements = s.sourceCode.statements) === null || _s_sourceCode_statements === void 0 ? void 0 : _s_sourceCode_statements.map(
(statement) => statement.getFullText()
//.replace(/\n\s+/g, '\n')
).join("");
return `${head}${body}${tail}`;
} else {
return "";
}
}).join("");
};
const TestGeneration = ({ testsSource = "", featureSource = "" }) => {
const [editor, setEditor] = (0, import_react.useState)();
const [type, setType] = (0, import_react.useState)("typescript");
const [gherkinSource, setGherkinSource] = (0, import_react.useState)(testsSource);
const [jestSource, setJestSource] = (0, import_react.useState)(featureSource);
const genComment = () => {
if (editor) {
const transpiler = new import_JestToGherkin.JestToGherkin();
transpiler.transpile(jestSource, {
fileName: "a.test.ts"
});
const ret = transpiler.generateComments();
setType("typescript");
editor.setValue(ret);
}
};
const genUniqueStep = () => {
if (editor) {
const transpiler = new import_JestToGherkin.JestToGherkin();
transpiler.transpile(jestSource, {
fileName: "a.test.ts"
});
setType("typescript");
editor.setValue(getUniqueStep(transpiler.output));
}
};
const genJest = () => {
if (editor) {
const generator = new import_TestGeneratorFromSource.TestGeneratorFromSource();
const steps = generator.compileKnownStepsFromSource(jestSource);
var _generator_generateGherkinFromSource;
const ret = (_generator_generateGherkinFromSource = generator.generateGherkinFromSource(steps, gherkinSource)) !== null && _generator_generateGherkinFromSource !== void 0 ? _generator_generateGherkinFromSource : "";
setType("typescript");
editor.setValue(ret);
}
};
const genGherkin = () => {
if (editor) {
const transpilor = new import_JestToGherkin.JestToGherkin();
const ret = transpilor.transpile(jestSource, {
fileName: "a.test.ts"
}).outputText;
setType("markdown");
editor.setValue(ret);
}
};
import_react.default.useEffect(() => {
if (featureSource) {
setGherkinSource(featureSource);
}
if (testsSource) {
setJestSource(testsSource);
}
}, [
testsSource,
featureSource
]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(LineWrapper, {
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SourceCodeEditor, {
updateHandler: setJestSource,
value: testsSource,
type: "typescript"
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SourceCodeEditor, {
updateHandler: setGherkinSource,
value: featureSource,
type: "markdown"
})
]
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(LineWrapper, {
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
onClick: genUniqueStep,
type: "button",
disabled: jestSource === "",
children: "View DEFINED STEPS"
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
onClick: genComment,
type: "button",
disabled: jestSource === "",
children: "Generate comments of test code from DEFINED STEPS"
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
onClick: genJest,
type: "button",
disabled: gherkinSource === "",
children: "Generate test code from Gherkin Document and DEFINED STEPS"
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
onClick: genGherkin,
type: "button",
disabled: jestSource === "",
children: "Generate Gherkin from DEFINED STEPS"
})
]
}),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(LineWrapper, {
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ColumnWrapper, {
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react2.Editor, {
height: "40vh",
language: type,
onMount: (editor2) => setEditor(editor2)
})
})
})
]
});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TestGeneration
});
//# sourceMappingURL=TestGeneration.js.map