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

port dotnet DialogTestClient DialogContext changes #1914

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 27 additions & 11 deletions libraries/botbuilder-testing/src/dialogTestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@
* Licensed under the MIT License.
*/


import { Activity, TestAdapter, Middleware, ConversationState, MemoryStorage, AutoSaveStateMiddleware, TurnContext } from 'botbuilder-core';
import { Dialog, DialogSet, DialogTurnStatus, DialogTurnResult } from 'botbuilder-dialogs';
import { Dialog, DialogContext, DialogSet, DialogTurnStatus, DialogTurnResult } from 'botbuilder-dialogs';

/**
* A client for testing dialogs in isolation.
*/
export class DialogTestClient {

private readonly _callback: (turnContext: TurnContext) => Promise<void>;
private _dialogContext: DialogContext = null;
private readonly _testAdapter: TestAdapter;

/**
* A DialogTurnResult instance with the result of the last turn.
*/
public dialogTurnResult: DialogTurnResult;

/**
* A ConversationState instance for the current test client.
*/
public conversationState: ConversationState;

/**
Expand All @@ -40,7 +48,7 @@ export class DialogTestClient {
*/
public constructor(channelId: string, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState);
public constructor(testAdapter: TestAdapter, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState)
constructor(channelOrAdapter: string|TestAdapter, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState) {
public constructor(channelOrAdapter: string|TestAdapter, targetDialog: Dialog, initialDialogOptions?: any, middlewares?: Middleware[], conversationState?: ConversationState) {
this.conversationState = conversationState || new ConversationState(new MemoryStorage());

let dialogState = this.conversationState.createProperty('DialogState');
Expand All @@ -58,6 +66,15 @@ export class DialogTestClient {
this.addUserMiddlewares(middlewares);
}

/**
* Gets a reference for the DialogContext.
* @remarks
* This property will be null until at least one activity is sent to DialogTestClient.
*/
public get dialogContext(): DialogContext {
return this._dialogContext;
}

/**
* Send an activity into the dialog.
* @returns a TestFlow that can be used to assert replies etc
Expand All @@ -75,31 +92,30 @@ export class DialogTestClient {
/**
* Get the next reply waiting to be delivered (if one exists)
*/
public getNextReply() {
public getNextReply(): Partial<Activity> {
return this._testAdapter.activityBuffer.shift();
}

private getDefaultCallback(targetDialog: Dialog, initialDialogOptions: any, dialogState: any): (turnContext: TurnContext) => Promise<void> {

return async (turnContext: TurnContext) => {
return async (turnContext: TurnContext): Promise<void> => {

const dialogSet = new DialogSet(dialogState);
dialogSet.add(targetDialog);

const dialogContext = await dialogSet.createContext(turnContext);
this.dialogTurnResult = await dialogContext.continueDialog();
this._dialogContext = await dialogSet.createContext(turnContext);
this.dialogTurnResult = await this._dialogContext.continueDialog();
if (this.dialogTurnResult.status === DialogTurnStatus.empty) {
this.dialogTurnResult = await dialogContext.beginDialog(targetDialog.id, initialDialogOptions);
this.dialogTurnResult = await this._dialogContext.beginDialog(targetDialog.id, initialDialogOptions);
}
};
}

private addUserMiddlewares(middlewares: Middleware[]): void {
if (middlewares != null) {
middlewares.forEach((middleware) => {
middlewares.forEach((middleware): void => {
this._testAdapter.use(middleware);
});
}
}

}
}
19 changes: 18 additions & 1 deletion libraries/botbuilder-testing/tests/dialogTestClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const { DialogTestClient, DialogTestLogger } = require('../');
const { ComponentDialog, TextPrompt, WaterfallDialog, DialogTurnStatus, DialogSet } = require('botbuilder-dialogs');
const assert = require('assert');
const { ok: assert, strictEqual } = require('assert');


describe('DialogTestClient', function() {
Expand All @@ -20,6 +20,23 @@ describe('DialogTestClient', function() {
assert(client._testAdapter.template.channelId == 'custom', 'Created with wrong channel id');
});

it('should set a dialogContext after an activity is received', async function() {
let dialog = new WaterfallDialog('waterfall', [
async step => {
await step.context.sendActivity('hello');
return step.endDialog();
}
]);

let client = new DialogTestClient('test', dialog);
strictEqual(client.dialogContext, null);
let reply = await client.sendActivity('hello');
assert(client.dialogContext, 'client.dialogContext not found');
assert(reply.text == 'hello', 'dialog responded with incorrect message');
assert(reply.channelId == 'test', 'test channel id didnt get set');
assert(client.dialogTurnResult.status == DialogTurnStatus.complete, 'dialog did not end properly');
});

it('should process a single turn waterfall dialog', async function() {

let dialog = new WaterfallDialog('waterfall', [
Expand Down