@serenity-js/assertions
Version:
Serenity/JS universal assertion library supporting all types of functional tests, including both web and REST API scenarios
56 lines • 2.57 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.containAtLeastOneItemThat = containAtLeastOneItemThat;
const core_1 = require("@serenity-js/core");
/**
* Produces an [expectation](https://serenity-js.org/api/core/class/Expectation/) that is met when the actual array of `Item[]` contains
* at least one `Item` for which the `expectation` is met.
*
* ## Ensuring that at least one item in an array meets the expectation
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, containAtLeastOneItemThat, isGreaterThan } from '@serenity-js/assertions'
*
* const items = [ 10, 15, 20 ]
*
* await actorCalled('Ester').attemptsTo(
* Ensure.that(items, containAtLeastOneItemThat(isGreaterThan(18))),
* )
* ```
*
* @param expectation
*
* @group Expectations
*/
function containAtLeastOneItemThat(expectation) {
return new ContainAtLeastOneItemThatMeetsExpectation(expectation);
}
/**
* @package
*/
class ContainAtLeastOneItemThatMeetsExpectation extends core_1.Expectation {
expectation;
static descriptionFor(expectation) {
return (0, core_1.d) `contain at least one item that does ${expectation}`;
}
constructor(expectation) {
super('containAtLeastOneItemThat', ContainAtLeastOneItemThatMeetsExpectation.descriptionFor(expectation), async (actor, actual) => {
const items = await actor.answer(actual);
if (!items || items.length === 0) {
const unanswered = new core_1.Unanswered();
return new core_1.ExpectationNotMet(ContainAtLeastOneItemThatMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containAtLeastOneItemThat', unanswered), unanswered, items);
}
let outcome;
for (const item of items) {
outcome = await actor.answer(expectation.isMetFor(item));
if (outcome instanceof core_1.ExpectationMet) {
return new core_1.ExpectationMet(ContainAtLeastOneItemThatMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containAtLeastOneItemThat', outcome.expectation), outcome.expected, items);
}
}
return new core_1.ExpectationNotMet(ContainAtLeastOneItemThatMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containAtLeastOneItemThat', outcome.expectation), outcome.expected, items);
});
this.expectation = expectation;
}
}
//# sourceMappingURL=containAtLeastOneItemThat.js.map
;