Skip to content

Commit

Permalink
add addStep, fix this pointer for steps and fix unit tests which were…
Browse files Browse the repository at this point in the history
…n't awaiting adapter
  • Loading branch information
Tom Laird-McConnell committed Sep 11, 2018
1 parent ee2e943 commit 00c212a
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 336 deletions.
6 changes: 5 additions & 1 deletion libraries/botbuilder-core/src/testAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ export class TestFlow {
} else if (adapter.activityBuffer.length > 0) {
// Activity received
const reply: Partial<Activity> = adapter.activityBuffer.shift() as Activity;
inspector(reply, description as string);
try {
inspector(reply, description as string);
} catch (err) {
reject(err);
}
resolve();
} else {
setTimeout(waitForActivity, 5);
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@types/mocha": "^2.2.47",
"codelyzer": "^4.1.0",
"mocha": "^5.0.0",
"mocha": "^5.2.0",
"nyc": "^11.4.1",
"source-map-support": "^0.5.3",
"ts-node": "^4.1.0"
Expand Down
19 changes: 16 additions & 3 deletions libraries/botbuilder-dialogs/src/waterfallDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,22 @@ export class WaterfallDialog<O extends object = {}> extends Dialog<O> {
* Creates a new waterfall dialog containing the given array of steps.
* @param steps Array of waterfall steps.
*/
constructor(dialogId: string, steps: WaterfallStep<O>[]) {
constructor(dialogId: string, steps?: WaterfallStep<O>[]) {
super(dialogId);
this.steps = steps.slice(0);
this.steps = [];
if (steps) {
this.steps = steps.slice(0);
}
}

/**
* add a new step to the waterfall
* @param step method to call
* @returns WaterfallDialog
*/
public addStep(step: WaterfallStep<O>): WaterfallDialog<O> {
this.steps.push(step);
return this;
}

public async dialogBegin(dc: DialogContext, options?: O): Promise<DialogTurnResult> {
Expand Down Expand Up @@ -103,7 +116,7 @@ export class WaterfallDialog<O extends object = {}> extends Dialog<O> {
}

protected async onStep(dc: DialogContext, step: WaterfallStepContext<O>): Promise<DialogTurnResult> {
return await this.steps[step.index](dc, step);
return await this.steps[step.index].call(this, dc, step);
}

private async runStep(dc: DialogContext, index: number, reason: DialogReason, result?: any): Promise<DialogTurnResult> {
Expand Down
5 changes: 2 additions & 3 deletions libraries/botbuilder-dialogs/tests/attachmentPrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const invalidMessage = { text: `what?`, type: 'message' };
describe('AttachmentPrompt', function() {
this.timeout(5000);

it('should call AttachmentPrompt using dc.prompt().', function (done) {
it('should call AttachmentPrompt using dc.prompt().', async function () {
// Initialize TestAdapter.
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);
Expand All @@ -32,11 +32,10 @@ describe('AttachmentPrompt', function() {
const dialogs = new DialogSet(dialogState);
dialogs.add(new AttachmentPrompt('prompt'));

adapter.send('Hello')
await adapter.send('Hello')
.assertReply('Please send an attachment.')
.send(answerMessage)
.assertReply('test1');
done();
});

it('should call AttachmentPrompt with custom validator.', function (done) {
Expand Down
108 changes: 50 additions & 58 deletions libraries/botbuilder-dialogs/tests/choicePrompt.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { ActivityTypes, ConversationState, MemoryStorage, TestAdapter } = require('botbuilder-core');
const { ChoicePrompt, DialogSet,ListStyle, DialogTurnStatus } = require('../');
const { ChoicePrompt, DialogSet, ListStyle, DialogTurnStatus } = require('../');
const assert = require('assert');

const answerMessage = { text: `red`, type: 'message' };
const invalidMessage = { text: `purple`, type: 'message' };

const stringChoices = ['red', 'green', 'blue'];

describe('ChoicePrompt', function() {
describe('ChoicePrompt', function () {
this.timeout(5000);

it('should call ChoicePrompt using dc.prompt().', function (done) {
it('should call ChoicePrompt using dc.prompt().', async function () {
// Initialize TestAdapter.
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);
Expand All @@ -36,14 +36,13 @@ describe('ChoicePrompt', function() {

dialogs.add(choicePrompt);

adapter.send('Hello')
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
});

it('should send a prompt and choices if they are passed in via PromptOptions.', function (done) {
it('should send a prompt and choices if they are passed in via PromptOptions.', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand All @@ -65,14 +64,13 @@ describe('ChoicePrompt', function() {

dialogs.add(choicePrompt);

adapter.send('Hello')
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
});

it('should call ChoicePrompt with custom validator.', function (done) {
it('should call ChoicePrompt with custom validator.', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand All @@ -99,16 +97,15 @@ describe('ChoicePrompt', function() {
choicePrompt.style = ListStyle.none;
dialogs.add(choicePrompt);

adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('Please choose a color.')
.send(answerMessage)
.assertReply('red');
});

it('should send custom retryPrompt.', function (done) {
it('should send custom retryPrompt.', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand All @@ -129,16 +126,15 @@ describe('ChoicePrompt', function() {
choicePrompt.style = ListStyle.none;
dialogs.add(choicePrompt);

adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('Please choose red, blue, or green.')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('Please choose red, blue, or green.')
.send(answerMessage)
.assertReply('red');
});

it('should send ignore retryPrompt if validator replies.', function (done) {
it('should send ignore retryPrompt if validator replies.', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand Down Expand Up @@ -167,16 +163,15 @@ describe('ChoicePrompt', function() {
choicePrompt.style = ListStyle.none;
dialogs.add(choicePrompt);

adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('bad input.')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send('Hello')
.assertReply('Please choose a color.')
.send(invalidMessage)
.assertReply('bad input.')
.send(answerMessage)
.assertReply('red');
});

it('should use defaultLocale when rendering choices', function (done) {
it('should use defaultLocale when rendering choices', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand Down Expand Up @@ -204,16 +199,15 @@ describe('ChoicePrompt', function() {
}, 'es-es');
dialogs.add(choicePrompt);

adapter.send({ text: 'Hello', type: ActivityTypes.Message })
.assertReply('Please choose a color. (1) red, (2) green, o (3) blue')
.send(invalidMessage)
.assertReply('bad input.')
.send({ text: 'red', type: ActivityTypes.Message })
.assertReply('red');
done();
await adapter.send({ text: 'Hello', type: ActivityTypes.Message })
.assertReply('Please choose a color. (1) red, (2) green, o (3) blue')
.send(invalidMessage)
.assertReply('bad input.')
.send({ text: 'red', type: ActivityTypes.Message })
.assertReply('red');
});

it('should use context.activity.locale when rendering choices', function (done) {
it('should use context.activity.locale when rendering choices', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand Down Expand Up @@ -241,14 +235,13 @@ describe('ChoicePrompt', function() {
});
dialogs.add(choicePrompt);

adapter.send({ text: 'Hello', type: ActivityTypes.Message, locale: 'es-es' })
.assertReply('Please choose a color. (1) red, (2) green, o (3) blue')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send({ text: 'Hello', type: ActivityTypes.Message, locale: 'es-es' })
.assertReply('Please choose a color. (1) red, (2) green, o (3) blue')
.send(answerMessage)
.assertReply('red');
});

it('should use context.activity.locale over defaultLocale when rendering choices', function (done) {
it('should use context.activity.locale over defaultLocale when rendering choices', async function () {
const adapter = new TestAdapter(async (turnContext) => {
const dc = await dialogs.createContext(turnContext);

Expand Down Expand Up @@ -276,10 +269,9 @@ describe('ChoicePrompt', function() {
}, 'es-es');
dialogs.add(choicePrompt);

adapter.send({ text: 'Hello', type: ActivityTypes.Message, locale: 'en-us' })
.assertReply('Please choose a color. (1) red, (2) green, or (3) blue')
.send(answerMessage)
.assertReply('red');
done();
await adapter.send({ text: 'Hello', type: ActivityTypes.Message, locale: 'en-us' })
.assertReply('Please choose a color. (1) red, (2) green, or (3) blue')
.send(answerMessage)
.assertReply('red');
});
});

0 comments on commit 00c212a

Please sign in to comment.