@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
112 lines (111 loc) • 3.9 kB
JavaScript
import { BackgroundNotCalledError, BackgroundNotExistsError, FeatureUknowScenarioError, IsScenarioOutlineError, ItemAlreadyExistsError, NotScenarioOutlineError, ParentWithoutScenario, ScenarioNotCalledError, } from '../../errors/errors';
import { Taggable } from './Taggable';
import { Scenario, ScenarioOutline } from './scenario';
export class ScenarioParent extends Taggable {
name;
_scenarii = [];
background = null;
title;
constructor(name, title) {
super();
this.name = name;
this.title = title;
}
addScenario(newScenario) {
const duplicatedScenario = this._scenarii.find((scenario) => {
return scenario.getTitle() === newScenario.getTitle();
});
if (duplicatedScenario) {
throw new ItemAlreadyExistsError(this, newScenario);
}
this._scenarii.push(newScenario);
}
getScenarioByName(name) {
return this._scenarii.find((s) => {
return s.description === name;
});
}
getScenarioExample(name) {
const scenario = this.getScenarioByName(name);
if (scenario instanceof ScenarioOutline) {
return scenario.examples;
}
return null;
}
getFirstNotCalledScenario(options) {
return this._scenarii.find((scenario) => {
return (scenario.isCalled === false && scenario.shouldBeCalled(options));
});
}
haveAlreadyCalledScenario() {
return (this._scenarii.filter((scenario) => scenario.isCalled === true).length > 0);
}
getTitle() {
return `${this.title}: ${this.name}`;
}
checkUncalledScenario(options) {
const uncalled = this.getFirstNotCalledScenario(options);
if (uncalled) {
throw new ScenarioNotCalledError(uncalled);
}
return this;
}
checkUncalledBackground(options) {
if (this.background) {
if (this.background.isCalled === false &&
(options.includeTags.length <= 0 ||
this.background.matchTags(options.includeTags) === true) &&
this.background.matchTags(options.excludeTags) === false) {
throw new BackgroundNotCalledError(this.background);
}
}
return this;
}
getBackground() {
if (this.background) {
return this.background;
}
throw new BackgroundNotExistsError(this);
}
checkIfScenarioExists(scenarioDescription) {
const foundScenario = this.getScenarioByName(scenarioDescription);
if (!foundScenario) {
throw new FeatureUknowScenarioError(this, new Scenario(scenarioDescription));
}
return foundScenario;
}
scenarioShouldNotBeOutline(scenario) {
if (scenario instanceof ScenarioOutline) {
throw new IsScenarioOutlineError(scenario);
}
}
scenarioShouldBeOutline(scenario) {
if (!(scenario instanceof ScenarioOutline)) {
throw new NotScenarioOutlineError(scenario);
}
}
getScenario(description) {
const scenario = this.checkIfScenarioExists(description);
this.scenarioShouldNotBeOutline(scenario);
return scenario;
}
getScenarioOutline(description) {
const scenario = this.checkIfScenarioExists(description);
this.scenarioShouldBeOutline(scenario);
return scenario;
}
mustHaveScenario() {
if (this._scenarii.length === 0) {
throw new ParentWithoutScenario(this);
}
}
get hasScenarioOutline() {
return this._scenarii.some((scenario) => scenario.getTitle().includes('Outline'));
}
get hasScenario() {
return this._scenarii.some((scenario) => scenario.getTitle().includes('Outline') === false);
}
get scenarii() {
return this._scenarii;
}
}