Skip to content

Commit

Permalink
added assertNoReply to TestAdapter (#2002)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Ickman <[email protected]>
  • Loading branch information
mdrichardson and Stevenic authored Apr 21, 2020
1 parent 57ad046 commit ee0960d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libraries/botbuilder-core/src/testAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,40 @@ export class TestFlow {
this.adapter);
}

/**
* Generates an assertion that the turn processing logic did not generate a reply from the bot, as expected.
* @param description (Optional) Description of the test case. If not provided one will be generated.
* @param timeout (Optional) number of milliseconds to wait for a response from bot. Defaults to a value of `3000`.
*/
public assertNoReply(description?: string, timeout?: number): TestFlow {
return new TestFlow(
this.previous.then(() => {
// tslint:disable-next-line:promise-must-complete
return new Promise<void>((resolve: any, reject: any): void => {
if (!timeout) { timeout = 3000; }
const start: number = new Date().getTime();
const adapter: TestAdapter = this.adapter;

function waitForActivity(): void {
const current: number = new Date().getTime();
if ((current - start) > <number>timeout) {
// Operation timed out and received no reply
resolve();
} else if (adapter.activityBuffer.length > 0) {
// Activity received
const reply: Partial<Activity> = adapter.activityBuffer.shift() as Activity;
assert.strictEqual(reply, undefined, `${ JSON.stringify(reply) } is responded when waiting for no reply: '${ description }'`);
resolve();
} else {
setTimeout(waitForActivity, 5);
}
}
waitForActivity();
});
}),
this.adapter);
}

/**
* Generates an assertion if the bots response is not one of the candidate strings.
* @param candidates List of candidate responses.
Expand Down
38 changes: 38 additions & 0 deletions libraries/botbuilder-core/tests/testAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ describe(`TestAdapter`, function () {
.catch((err) => done());
});

it(`should timeout waiting for assertNoReply() when an Activity is not expected.`, function (done) {
const start = new Date().getTime();
const adapter = new TestAdapter((context) => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), 600);
});
});
adapter
.send('test')
.assertNoReply('no message received', 500)
.then(() => done());
});

it(`should validate using assertNoReply() that no reply was received, when reply Activity not expected.`, function (done) {
const start = new Date().getTime();
const adapter = new TestAdapter((context) => {
return context.sendActivity(receivedMessage);
});
adapter
.send('test')
.assertReply({ text: 'received' })
.assertNoReply('should be no additional replies')
.then(() => done());
});

it(`should throw an error with assertNoReply() when no reply is expected, but reply Activity was received.`, function (done) {
const start = new Date().getTime();
const adapter = new TestAdapter((context) => {
const activities = [receivedMessage, receivedMessage];
context.sendActivity(activities);
});
adapter
.send('test')
.assertReply({ text: 'received' })
.assertNoReply('should be no additional replies')
.catch((err) => done());
});

it(`should support calling assertReplyOneOf().`, function (done) {
const start = new Date().getTime();
const adapter = new TestAdapter((context) => {
Expand Down

0 comments on commit ee0960d

Please sign in to comment.