Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added assertNoReply to TestFlow/TestAdapter #2002

Merged
merged 3 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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