@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
54 lines (53 loc) • 2.19 kB
JavaScript
import { SyntaxKind } from 'ts-morph';
import { generateBackground } from '../../../scripts/generateFile';
import { StepAst } from './StepAst';
import { StepableAst } from './StepableAst';
export class BackgroundAst extends StepableAst {
constructor(options) {
super(options);
}
static fromOptions(options) {
return new BackgroundAst(options);
}
handleBackground() {
const backgroundCallExpression = this.getBackgroundCallExpression();
if (backgroundCallExpression === undefined &&
this.stepableParent.background !== null) {
this.stepableParentFunction.insertStatements(0, generateBackground(this.stepableParent.background, this.forRule));
}
if (backgroundCallExpression !== undefined &&
this.stepableParent.background === null) {
if (this.shouldComment) {
this.commentExpression(this.stepableParentFunction, backgroundCallExpression);
}
else {
this.removeChildFromParent(this.stepableParentFunction, backgroundCallExpression);
}
}
if (backgroundCallExpression !== undefined &&
this.stepableParent.background !== null) {
const arrowFunction = this.getBackgroundArrowFunction();
if (arrowFunction) {
StepAst.fromOptions({
...this.options,
stepParent: this.stepableParent.background,
stepParentFunction: arrowFunction,
}).handleSteps();
this.updateStepableArguments(this.stepableParent.background, arrowFunction);
}
}
}
getBackgroundArrowFunction() {
return this.getBackgroundCallExpression()
?.getArguments()
.find((arg) => arg.isKind(SyntaxKind.ArrowFunction));
}
getBackgroundCallExpression() {
const regex = this.forRule ? /\bRuleBackground\(/ : /\bBackground\(/;
return this.stepableParentFunction
.getDescendantsOfKind(SyntaxKind.CallExpression)
.find((call) => {
return regex.test(call.getText());
});
}
}