Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed May 5, 2019
1 parent 543c82e commit a5cdaad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
29 changes: 13 additions & 16 deletions README.md
Expand Up @@ -268,27 +268,24 @@ controller.on('message_received', processWatsonResponse);
Events are messages having type different than `message`.

[Example](https://github.com/howdyai/botkit/blob/master/examples/facebook_bot.js) of handler:

```js
controller.on('facebook_postback', function(bot, message) {
bot.reply(message, 'Great Choice!!!! (' + message.payload + ')');
controller.on('facebook_postback', async (bot, message) => {
await bot.reply(message, `Great Choice. (${message.payload})`);
});
```

Since they usually have no text, events aren't processed by middleware and have no watsonData attribute.
If event handler wants to make use of some data from context, it has to read it first.
Example:

```js
controller.on('facebook_postback', (bot, message) => {
watsonMiddleware.readContext(message.user).
then((context = {}) =>
//do something useful here
myFunction(context.field1, context.field2)
.then(result => {
const newMessage = clone(message);
newMessage.text = 'postback result';
return watsonMiddleware.sendToWatson(bot, newMessage, { postbackResult: 'success' });
})
)
.catch(console.error);
controller.on('facebook_postback', async (bot, message) => {
const context = watsonMiddleware.readContext(message.user);
//do something useful here
const result = await myFunction(context.field1, context.field2);
const newMessage = {...message, text: 'postback result' };
await watsonMiddleware.sendToWatson(bot, newMessage, { postbackResult: 'success' });
});
```

Expand Down Expand Up @@ -316,8 +313,8 @@ Used globally:
```js
slackController.changeEars(watsonMiddleware.hear.bind(watsonMiddleware));

slackController.hears(['hello'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
bot.reply(message, message.watsonData.output.text.join('\n'));
slackController.hears(['hello'], ['direct_message', 'direct_mention', 'mention'], async (bot, message) => {
await bot.reply(message, message.watsonData.output.text.join('\n'));
// now do something special related to the hello intent
});
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "node ./node_modules/typescript/bin/tsc",
"pretest": "npm run build",
"test": "jest test --coverage",
"test": "jest test --coverage --forceExit",
"lint": "tsc --noEmit && eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"fix": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix ."
},
Expand Down
8 changes: 4 additions & 4 deletions test/context-store.test.ts
Expand Up @@ -16,9 +16,9 @@

import { readContext, updateContext } from '../lib/utils';
import { Botkit, BotkitMessage } from 'botkit';
import sinon from 'sinon';
import { MemoryStorage } from 'botbuilder';
import { WebAdapter } from 'botbuilder-adapter-web';
import sinon = require('sinon');

const message: BotkitMessage = {
type: 'message',
Expand Down Expand Up @@ -65,15 +65,15 @@ const controller = new Botkit({
const storage = controller.storage;

test('should read context correctly', function() {
return readContext(message, storage).then(function(context) {
return readContext(message.user, storage).then(function(context) {
expect(context).toEqual(null);
});
});

test('should suppress storage error', function() {
const storageStub = sinon.stub(storage, 'read').rejects('error message');

return readContext(message, storage)
return readContext(message.user, storage)
.then(function() {
storageStub.restore();
})
Expand Down Expand Up @@ -120,7 +120,7 @@ test('should ignore storage error on read when user is not saved yet', function(
test('should return storage error on write', function() {
const storageStub = sinon.stub(storage, 'write').rejects('error message');

updateContext(message.user, storage, conversation_response)
return updateContext(message.user, storage, conversation_response)
.then(function(err) {
expect(err).toEqual('error message');
storageStub.restore();
Expand Down

0 comments on commit a5cdaad

Please sign in to comment.