Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(core): q supports having multiple Answerables in a template
  • Loading branch information
jan-molak committed Feb 15, 2021
1 parent 15a9015 commit 05f0d9d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/core/spec/screenplay/questions/q.spec.ts
Expand Up @@ -60,6 +60,16 @@ describe('q', () => {
return expect(Quentin.answer(question)).to.eventually.equal('The answer is: 42!')
});

it(`should inject answers to multiple Answerables into the template`, () => {
const
baseUrl = Question.about('url', actor => 'http://127.0.0.1:8000'),
itemId = Question.about('itemId', actor => 5);

const question = q `${ baseUrl }/api/items/${ itemId }`;

return expect(Quentin.answer(question)).to.eventually.equal('http://127.0.0.1:8000/api/items/5');
});

/** @test {q} */
it('provides a sensible description of the question being asked', () => {
const question = q `/products/${ 1 }/attributes/${ Promise.resolve(2) }`;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/screenplay/questions/q.ts
Expand Up @@ -38,10 +38,13 @@ export function q(templates: TemplateStringsArray, ...parameters: Array<Answerab
Promise.all(parameters.map(parameter => actor.answer(parameter)))
.then(answers =>
templates
.map((template, i) => i < answers.length
? [ template, answers ]
: [ template ])
.reduce((acc, tuple) => acc.concat(tuple))
.map((template, i) =>
i < answers.length
? [ template, answers[i] ]
: [ template ])
.reduce((acc, tuple) =>
acc.concat(tuple)
)
.join('')
)
);
Expand Down

0 comments on commit 05f0d9d

Please sign in to comment.