@serenity-js/assertions
Version:
Serenity/JS universal assertion library supporting all types of functional tests, including both web and REST API scenarios
85 lines • 2.59 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.property = property;
const core_1 = require("@serenity-js/core");
/**
* Creates an [expectation](https://serenity-js.org/api/core/class/Expectation/) that is met when the value of
* the `actual[propertyName]` meets the `expectation`.
*
* ## Ensuring that an array has an item
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, property } from '@serenity-js/assertions'
*
* const list = [ 'hello', 'world' ]
*
* await actorCalled('Ester').attemptsTo(
* Ensure.that(list, property(0, isPresent())),
* )
* ```
*
* ## Ensuring that the property meets an expectation
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, property, equals } from '@serenity-js/assertions'
*
* const list = [ 'hello', 'world' ]
*
* await actorCalled('Ester').attemptsTo(
* Ensure.that(list, property('length', equals(2))),
* )
* ```
*
* ## Asserting on a list of objects
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, property, equals } from '@serenity-js/assertions'
*
* const developers = [{
* name: 'Jan',
* id: '@jan-molak',
* }, {
* name: 'John',
* id: '@wakaleo',
* }]
*
* await actorCalled('Ester').attemptsTo(
* Ensure.that(
* developers,
* containItemsWhereEachItem(
* property('id', startsWith('@'))
* ),
* ),
* )
* ```
*
* @param propertyName
* @param expectation
*
* @group Expectations
*/
function property(propertyName, expectation) {
return new HasProperty(propertyName, expectation);
}
/**
* @package
*/
class HasProperty extends core_1.Expectation {
propertyName;
constructor(propertyName, expectation) {
const subject = `have property ${String(propertyName)} that does ${expectation}`;
super('property', subject, async (actor, actual) => {
const actualValue = await actor.answer(actual);
const outcome = await actor.answer(expectation.isMetFor(actualValue[propertyName]));
const expectationDetails = core_1.ExpectationDetails.of('property', String(propertyName), outcome.expectation);
return outcome instanceof core_1.ExpectationMet
? new core_1.ExpectationMet(subject, expectationDetails, outcome.expected, outcome.actual)
: new core_1.ExpectationNotMet(subject, expectationDetails, outcome.expected, outcome.actual);
});
this.propertyName = propertyName;
}
}
//# sourceMappingURL=property.js.map
;