import { AnswersQuestions } from '@serenity-js/core'; import { formatted } from '@serenity-js/core/lib/io'; import { Expectation } from '../Expectation'; import { ExpectationMet, ExpectationNotMet, Outcome } from '../outcomes'; export function property( propertyName: Property, expectation: Expectation, ): Expectation { return new HasProperty(propertyName, expectation); } /** * @package */ class HasProperty extends Expectation { constructor( private readonly propertyName: Property, private readonly expectation: Expectation, ) { super(); } answeredBy(actor: AnswersQuestions): (actual: Actual) => Promise> { return (actual: Actual) => this.expectation.answeredBy(actor)(actual[this.propertyName]) .then((outcome: Outcome) => { return outcome instanceof ExpectationMet ? new ExpectationMet(this.toString(), outcome.expected, actual) : new ExpectationNotMet(this.toString(), outcome.expected, actual); }); } toString(): string { return formatted `have property ${ this.propertyName } that does ${ this.expectation }`; } }