@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
77 lines • 2.6 kB
JavaScript
import { d } from '../../io/index.js';
import { Task } from '../Task.js';
import { ExpectationMet } from './expectations/index.js';
/**
* A [flow control statement](https://en.wikipedia.org/wiki/Control_flow)
* that enables an [`Actor`](https://serenity-js.org/api/core/class/Actor/) to decide between two alternate series of [activities](https://serenity-js.org/api/core/class/Activity/).
*
* Think of it as a Screenplay Pattern equivalent of the traditional `if` statement.
*
* ## Choose between two alternative sequences of activities
*
* ```ts
* import { equals } from '@serenity-js/assertions'
* import { actorCalled, Check } from '@serenity-js/core'
*
* await actorCalled('Chuck').attemptsTo(
* Check.whether(process.env.MODE, equals('prod'))
* .andIfSo(
* LogInAsProdUser(),
* )
* .otherwise(
* LogInAsTestUser(),
* )
* )
* ```
*
* ## Perform a sequence of activities when a condition is met
*
* ```ts
* import { actorCalled, Check } from '@serenity-js/core'
* import { isVisible } from '@serenity-js/web'
*
* await actorCalled('Chuck').attemptsTo(
* Check.whether(CookieConsentBanner(), isVisible())
* .andIfSo(
* AcceptNecessaryCookies(),
* )
* )
* ```
*
* @group Activities
*/
export class Check extends Task {
actual;
expectation;
activities;
alternativeActivities;
static whether(actual, expectation) {
return {
andIfSo: (...activities) => new Check(actual, expectation, activities),
};
}
constructor(actual, expectation, activities, alternativeActivities = []) {
super(d `#actor checks whether ${actual} does ${expectation}`);
this.actual = actual;
this.expectation = expectation;
this.activities = activities;
this.alternativeActivities = alternativeActivities;
}
/**
* @param alternativeActivities
* A sequence of [activities](https://serenity-js.org/api/core/class/Activity/) to perform when the [`Expectation`](https://serenity-js.org/api/core/class/Expectation/) is not met.
*/
otherwise(...alternativeActivities) {
return new Check(this.actual, this.expectation, this.activities, alternativeActivities);
}
/**
* @inheritDoc
*/
async performAs(actor) {
const outcome = await actor.answer(this.expectation.isMetFor(this.actual));
return outcome instanceof ExpectationMet
? actor.attemptsTo(...this.activities)
: actor.attemptsTo(...this.alternativeActivities);
}
}
//# sourceMappingURL=Check.js.map