jest-bdd-generator
Version:
Jest code generator empowered with Gherkin and BDD
183 lines (182 loc) • 5.77 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useRef } from "react";
import styled from "styled-components";
import { JestToGherkin } from "../jest-to-gherkin/JestToGherkin";
import { TestGeneratorFromSource } from "../gherkin-to-test/TestGeneratorFromSource";
import { Editor } from "@monaco-editor/react";
const LineWrapper = styled("div")`
display: flex;
> * {
flex: 1 1 auto;
}
`;
const ColumnWrapper = styled("div")`
display: flex;
flex-direction: column;
`;
const SourceCodeEditor = ({ updateHandler, type, value }) => {
const [inJestSource, setEditor] = useState();
const fileJestSource = 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__ */ _jsxs(ColumnWrapper, {
children: [
/* @__PURE__ */ _jsx("input", {
ref: fileJestSource,
onChange: handleFile,
type: "file"
}),
/* @__PURE__ */ _jsx(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] = useState();
const [type, setType] = useState("typescript");
const [gherkinSource, setGherkinSource] = useState(testsSource);
const [jestSource, setJestSource] = useState(featureSource);
const genComment = () => {
if (editor) {
const transpiler = new JestToGherkin();
transpiler.transpile(jestSource, {
fileName: "a.test.ts"
});
const ret = transpiler.generateComments();
setType("typescript");
editor.setValue(ret);
}
};
const genUniqueStep = () => {
if (editor) {
const transpiler = new JestToGherkin();
transpiler.transpile(jestSource, {
fileName: "a.test.ts"
});
setType("typescript");
editor.setValue(getUniqueStep(transpiler.output));
}
};
const genJest = () => {
if (editor) {
const generator = new 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 JestToGherkin();
const ret = transpilor.transpile(jestSource, {
fileName: "a.test.ts"
}).outputText;
setType("markdown");
editor.setValue(ret);
}
};
React.useEffect(() => {
if (featureSource) {
setGherkinSource(featureSource);
}
if (testsSource) {
setJestSource(testsSource);
}
}, [
testsSource,
featureSource
]);
return /* @__PURE__ */ _jsxs(_Fragment, {
children: [
/* @__PURE__ */ _jsxs(LineWrapper, {
children: [
/* @__PURE__ */ _jsx(SourceCodeEditor, {
updateHandler: setJestSource,
value: testsSource,
type: "typescript"
}),
/* @__PURE__ */ _jsx(SourceCodeEditor, {
updateHandler: setGherkinSource,
value: featureSource,
type: "markdown"
})
]
}),
/* @__PURE__ */ _jsxs(LineWrapper, {
children: [
/* @__PURE__ */ _jsx("button", {
onClick: genUniqueStep,
type: "button",
disabled: jestSource === "",
children: "View DEFINED STEPS"
}),
/* @__PURE__ */ _jsx("button", {
onClick: genComment,
type: "button",
disabled: jestSource === "",
children: "Generate comments of test code from DEFINED STEPS"
}),
/* @__PURE__ */ _jsx("button", {
onClick: genJest,
type: "button",
disabled: gherkinSource === "",
children: "Generate test code from Gherkin Document and DEFINED STEPS"
}),
/* @__PURE__ */ _jsx("button", {
onClick: genGherkin,
type: "button",
disabled: jestSource === "",
children: "Generate Gherkin from DEFINED STEPS"
})
]
}),
/* @__PURE__ */ _jsx(LineWrapper, {
children: /* @__PURE__ */ _jsx(ColumnWrapper, {
children: /* @__PURE__ */ _jsx(Editor, {
height: "40vh",
language: type,
onMount: (editor2) => setEditor(editor2)
})
})
})
]
});
};
export {
TestGeneration
};
//# sourceMappingURL=TestGeneration.js.map