Skip to content

Commit a5cdaad

Browse files
chore: update tests
1 parent 543c82e commit a5cdaad

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,27 +268,24 @@ controller.on('message_received', processWatsonResponse);
268268
Events are messages having type different than `message`.
269269

270270
[Example](https://github.com/howdyai/botkit/blob/master/examples/facebook_bot.js) of handler:
271+
271272
```js
272-
controller.on('facebook_postback', function(bot, message) {
273-
bot.reply(message, 'Great Choice!!!! (' + message.payload + ')');
273+
controller.on('facebook_postback', async (bot, message) => {
274+
await bot.reply(message, `Great Choice. (${message.payload})`);
274275
});
275276
```
277+
276278
Since they usually have no text, events aren't processed by middleware and have no watsonData attribute.
277279
If event handler wants to make use of some data from context, it has to read it first.
278280
Example:
281+
279282
```js
280-
controller.on('facebook_postback', (bot, message) => {
281-
watsonMiddleware.readContext(message.user).
282-
then((context = {}) =>
283-
//do something useful here
284-
myFunction(context.field1, context.field2)
285-
.then(result => {
286-
const newMessage = clone(message);
287-
newMessage.text = 'postback result';
288-
return watsonMiddleware.sendToWatson(bot, newMessage, { postbackResult: 'success' });
289-
})
290-
)
291-
.catch(console.error);
283+
controller.on('facebook_postback', async (bot, message) => {
284+
const context = watsonMiddleware.readContext(message.user);
285+
//do something useful here
286+
const result = await myFunction(context.field1, context.field2);
287+
const newMessage = {...message, text: 'postback result' };
288+
await watsonMiddleware.sendToWatson(bot, newMessage, { postbackResult: 'success' });
292289
});
293290
```
294291

@@ -316,8 +313,8 @@ Used globally:
316313
```js
317314
slackController.changeEars(watsonMiddleware.hear.bind(watsonMiddleware));
318315

319-
slackController.hears(['hello'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
320-
bot.reply(message, message.watsonData.output.text.join('\n'));
316+
slackController.hears(['hello'], ['direct_message', 'direct_mention', 'mention'], async (bot, message) => {
317+
await bot.reply(message, message.watsonData.output.text.join('\n'));
321318
// now do something special related to the hello intent
322319
});
323320
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"build": "node ./node_modules/typescript/bin/tsc",
99
"pretest": "npm run build",
10-
"test": "jest test --coverage",
10+
"test": "jest test --coverage --forceExit",
1111
"lint": "tsc --noEmit && eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
1212
"fix": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix ."
1313
},

test/context-store.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import { readContext, updateContext } from '../lib/utils';
1818
import { Botkit, BotkitMessage } from 'botkit';
19-
import sinon from 'sinon';
2019
import { MemoryStorage } from 'botbuilder';
2120
import { WebAdapter } from 'botbuilder-adapter-web';
21+
import sinon = require('sinon');
2222

2323
const message: BotkitMessage = {
2424
type: 'message',
@@ -65,15 +65,15 @@ const controller = new Botkit({
6565
const storage = controller.storage;
6666

6767
test('should read context correctly', function() {
68-
return readContext(message, storage).then(function(context) {
68+
return readContext(message.user, storage).then(function(context) {
6969
expect(context).toEqual(null);
7070
});
7171
});
7272

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

76-
return readContext(message, storage)
76+
return readContext(message.user, storage)
7777
.then(function() {
7878
storageStub.restore();
7979
})
@@ -120,7 +120,7 @@ test('should ignore storage error on read when user is not saved yet', function(
120120
test('should return storage error on write', function() {
121121
const storageStub = sinon.stub(storage, 'write').rejects('error message');
122122

123-
updateContext(message.user, storage, conversation_response)
123+
return updateContext(message.user, storage, conversation_response)
124124
.then(function(err) {
125125
expect(err).toEqual('error message');
126126
storageStub.restore();

0 commit comments

Comments
 (0)