@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.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.containItemsWhereEachItem = containItemsWhereEachItem;
const core_1 = require("@serenity-js/core");
/**
* Produces an [expectation](https://serenity-js.org/api/core/class/Expectation/) that is met when all the items of the actual array of `Item[]`
* meet the `expectation`.
*
* ## Ensuring that all the items in an array meet the expectation
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, containItemsWhereEachItem, endsWith } from '@serenity-js/assertions'
*
* const items = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' ]
*
* await actorCalled('Ester').attemptsTo(
* Ensure.that(items, containItemsWhereEachItem(endsWith('day'))),
* )
* ```
*
* @param expectation
*
* @group Expectations
*/
function containItemsWhereEachItem(expectation) {
return new ContainItemsWhereEachItemMeetsExpectation(expectation);
}
/**
* @package
*/
class ContainItemsWhereEachItemMeetsExpectation extends core_1.Expectation {
expectation;
static descriptionFor(expectation) {
return (0, core_1.d) `contain items where each item does ${expectation}`;
}
constructor(expectation) {
super('containItemsWhereEachItem', ContainItemsWhereEachItemMeetsExpectation.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(ContainItemsWhereEachItemMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containItemsWhereEachItem', unanswered), unanswered, items);
}
let outcome;
for (const item of items) {
outcome = await actor.answer(expectation.isMetFor(item));
if (outcome instanceof core_1.ExpectationNotMet) {
return new core_1.ExpectationNotMet(ContainItemsWhereEachItemMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containItemsWhereEachItem', outcome.expectation), outcome.expected, items);
}
}
return new core_1.ExpectationMet(ContainItemsWhereEachItemMeetsExpectation.descriptionFor(expectation), core_1.ExpectationDetails.of('containItemsWhereEachItem', outcome.expectation), outcome.expected, items);
});
this.expectation = expectation;
}
}
//# sourceMappingURL=containItemsWhereEachItem.js.map
;