import { AnswersQuestions } from '@serenity-js/core'; import { match } from 'tiny-types'; import { Expectation } from '../Expectation'; import { ExpectationNotMet, Outcome } from '../outcomes'; export function and(...expectations: Array>): Expectation { return new And(expectations); } /** * @package */ class And extends Expectation { constructor(private readonly expectations: Array>) { super(); } answeredBy(actor: AnswersQuestions): (actual: Actual) => Promise> { return (actual: any) => this.expectations.reduce( (previous, current) => previous.then(outcome => match(outcome) .when(ExpectationNotMet, o => o) .else(_ => current.answeredBy(actor)(actual)), ), Promise.resolve(void 0), ); } toString(): string { return this.expectations.map(assertion => assertion.toString()).join(' and '); } }