Skip to content
Permalink
Browse files
fix(assertions): auto-generated description of an Expectation can be …
…overridden via describedAs
  • Loading branch information
jan-molak committed Nov 4, 2020
1 parent e138478 commit f5d02fafdd1c4fcee76d0011e2c916915adc86a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
@@ -68,4 +68,29 @@ describe('Expectation', () => {
)).to.be.rejectedWith(AssertionError, `Expected 9 to have value that's less than 8 or equal 8`);
});
});

describe('allows to override the description of an expectation, so that the new version', () => {

function isWithin(lowerBound: number, upperBound: number) {
return and(
or(isGreaterThan(lowerBound), equals(lowerBound)),
or(isLessThan(upperBound), equals(upperBound)),
).describedAs(`have value within ${ lowerBound } and ${ upperBound }`);
}

/** @test {Expectation.to} */
it('replaces the old description', () => {
expect(Ensure.that(5, isWithin(3, 6)).toString())
.to.equal(`#actor ensures that 5 does have value within 3 and 6`);
});

/** @test {Expectation.to} */
it('provides a precise failure message when the expectation is not met', () => {
return expect(actorCalled('Astrid').attemptsTo(
Ensure.that(9, isWithin(7, 8)),
)).to.be.rejectedWith(AssertionError, `Expected 9 to have value that's less than 8 or equal 8`);
});
});


});
@@ -52,16 +52,6 @@ describe('Question', () => {
.to.equal('first name');
});

/** @test {Question.about} */
/** @test {Question#describedAs} */
it('allows for a custom description to override the default one without affecting the original question', () => {
const Name = Question.about('a name', (actor: Actor) => actor.name);
const FirstName = Name.describedAs('first name');

expect(Name.toString()).to.equal('a name');
expect(FirstName.toString()).to.equal('first name');
});

describe('when mapping the answer', () => {

/** @test {Question.about} */
@@ -112,7 +112,7 @@ export abstract class Question<T> {
* @param {string} subject
* @returns {Question<T>}
*/
describedAs(subject: string): Question<T> {
describedAs(subject: string): this {
this.subject = subject;

return this;
@@ -233,7 +233,9 @@ class AnonymousQuestion<T> extends Question<T> {
* @param {string} subject
* @returns {Question<T>}
*/
describedAs(subject: string): Question<T> {
return new AnonymousQuestion(subject, this.body);
describedAs(subject: string): this {
this.subject = subject;

return this;
}
}
@@ -37,8 +37,10 @@ export class Transform<Answer_Type extends any, Output_Type> extends Question<Pr
*
* @param subject
*/
describedAs(subject: string): Transform<Answer_Type, Output_Type> {
return new Transform(this.questions, this.transformation, subject);
describedAs(subject: string): this {
this.subject = subject;

return this;
}

answeredBy(actor: AnswersQuestions & UsesAbilities): Promise<Output_Type> {

0 comments on commit f5d02fa

Please sign in to comment.