Skip to content
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Passing in the config overrides any default values or values set by bot-tester.j
defaultAddress: botbuilder.IAddress,
timeout: number // in milliseconds
// each filter returns false for messages that the BotTester should ignore
messageFilters: ((message:IMessage) => boolean)[]
messageFilters: ((message:IMessage) => boolean)[],
ignoreTypingEvent: boolean,
ignoreEndOfConversationEvent: boolean
}
```
if timeout is defined, then a particular ```runTest()``` call will fail if it does not receive each expected message within the timeout period of time set in the options.
Expand Down Expand Up @@ -411,5 +413,18 @@ describe('BotTester', () => {
.runTest()
).to.be.rejected.notify(done);
});

it('can ignore typing events', () => {
bot.dialog('/', (session) => {
session.send('hello');
session.sendTyping();
session.send('goodbye');
});

return new BotTester(bot)
.ignoreTypingEvent()
.sendMessageToBot('hey', 'hello', 'goodbye')
.runTest();
});
});
```
31 changes: 31 additions & 0 deletions src/BotTester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Promise from 'bluebird';
import { IAddress, IMessage, Message, Session, UniversalBot } from 'botbuilder';
import { ignoreEndOfConversationEventFilter, ignoreTypingEventFilter } from './builtInMessageFilters';
import { config, IConfig, MessageFilter } from './config';
import { ExpectedMessage, PossibleExpectedMessageCollections, PossibleExpectedMessageType } from './ExpectedMessage';
import { botToUserMessageCheckerFunction, MessageService } from './MessageService';
Expand All @@ -24,6 +25,16 @@ export interface IOptionsModifier {
* sets the timeout time that the BotTester will wait for any particular message before failing
*/
setTimeout(milliseconds: number): BotTester;

/**
* adds prebuilt filter to ignore typing event
*/
ignoreTypingEvent(): BotTester;

/**
* adds prebuilt filter to ignore endOfConversationEvent
*/
ignoreEndOfConversationEvent(): BotTester;
}

/**
Expand Down Expand Up @@ -119,10 +130,30 @@ export class BotTester implements IBotTester, IOptionsModifier {
return this;
}

public ignoreEndOfConversationEvent(): BotTester {
this.config.ignoreEndOfConversationEvent = true;

return this;
}

public ignoreTypingEvent(): BotTester {
this.config.ignoreTypingEvent = true;

return this;
}

/**
* Initializes the MessegeService here to allow config changes to accumulate
*/
public runTest(): Promise<{}> {
if (this.config.ignoreTypingEvent) {
this.config.messageFilters.push(ignoreTypingEventFilter);
}

if (this.config.ignoreEndOfConversationEvent) {
this.config.messageFilters.push(ignoreEndOfConversationEventFilter);
}

this.messageService = new MessageService(this.bot, this.config);

return Promise.mapSeries(this.testSteps, (fn: TestStep) => fn());
Expand Down
4 changes: 2 additions & 2 deletions src/ExpectedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMessage } from 'botbuilder';
import { IMessage, IEvent } from 'botbuilder';
import { expect } from './assertionLibraries/IExpectation';

export enum ExpectedMessageType {
Expand All @@ -10,7 +10,7 @@ export enum ExpectedMessageType {
/**
* Types accepted for responses checkers
*/
export type PossibleExpectedMessageType = string | IMessage | RegExp;
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent;

/**
* Response expectations area always collections. The collection is the set of possible responses, chosen at random. If the collection size
Expand Down
1 change: 0 additions & 1 deletion src/MessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export class MessageService {
} catch (e) {
return rej(e);
}

});

if (!outgoingMessageComparator.expectsAdditionalMessages()) {
Expand Down
13 changes: 13 additions & 0 deletions src/builtInMessageFilters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IMessage } from 'botbuilder';

export function ignoreInternalSaveMessageFilter(message: IMessage): boolean {
return message.type !== '__save__';
}

export function ignoreEndOfConversationEventFilter(message: IMessage): boolean {
return message.type !== 'endOfConversation';
}

export function ignoreTypingEventFilter(message: IMessage): boolean {
return message.type !== 'typing';
}
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export interface IConfig {
*/
defaultAddress?: IAddress;

/**
* ignores typing event messages
*/
ignoreTypingEvent?: boolean;

/**
* ignores end of conversation event messages
*/
ignoreEndOfConversationEvent?: boolean;

/**
* filters for messages that the BotTester framework should use
*/
Expand Down
13 changes: 13 additions & 0 deletions test/BotTester.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ describe('BotTester', () => {
.runTest()
).to.be.rejected.notify(done);
});

it('can ignore typing events', () => {
bot.dialog('/', (session) => {
session.send('hello');
session.sendTyping();
session.send('goodbye');
});

return new BotTester(bot)
.ignoreTypingEvent()
.sendMessageToBot('hey', 'hello', 'goodbye')
.runTest();
});
});
//```

Expand Down
35 changes: 35 additions & 0 deletions test/BotTesterFailure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,39 @@ describe('BotTester', () => {
.sendMessageToBot('intro', 'hello', 'how', 'are', 'you?')
.runTest()).to.be.rejected.notify(done);
});

it('will fail if a endOfConversationEvent is seen with an ingoreEndOfConversationEventFilter', (done: Function) => {
bot.dialog('/', (session: Session) => {
session.send('hello');
session.endConversation();
});

expect(new BotTester(bot)
// need to timeout before the tests do to catch the error
.setTimeout(500)
.ignoreEndOfConversationEvent()
//tslint:disable
.sendMessageToBot('hey', 'hello', {type: 'endOfConversation'} as any)
//tslint:enable
.runTest())
.to.be.rejected.notify(done);
});

it('will fail if a typing event is seen with an ignoreTypingEventFilter', (done: Function) => {
bot.dialog('/', (session: Session) => {
session.send('hello');
session.sendTyping();
session.send('hey');
});

expect(new BotTester(bot)
// need to timeout before the tests do to catch the error
.setTimeout(500)
.ignoreTypingEvent()
//tslint:disable
.sendMessageToBot('hey', 'hello', {type: 'typing'} as any, 'hey')
//tslint:enable
.runTest())
.to.be.rejected.notify(done);
});
});