Skip to content

Commit

Permalink
test: initial tests for typing indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Mar 16, 2019
1 parent cff1baa commit a640176
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 28 deletions.
10 changes: 4 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"extends": ["airbnb-base", "prettier"],
"plugins": ["mocha"],
"rules": {
"mocha/no-exclusive-tests": "error"
},
"env": { "mocha": true, "node": true },
"extends": ["airbnb-base", "prettier", "plugin:jest/recommended"],
"plugins": ["jest"],
"rules": {},
"env": { "jest/globals": true, "node": true },
"globals": {
"expect": true,
"sinon": true
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^3.3.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-mocha": "^5.2.0",
"husky": "^1.3.1",
"jest": "^24.5.0",
Expand All @@ -37,7 +38,7 @@
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "yarn lint && lint-staged",
"pre-push": "yarn test"
"pre-push": "yarn test:coverage"
}
},
"lint-staged": {
Expand Down
80 changes: 62 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,77 @@ const calculateTypingDelay = response => {
);
};

const typingIndicatorMessage = (bot, message) => ({
const typingIndicatorMessage = message => ({
recipient: { id: message.to },
channel: message.channel,
sender_action: 'typing_on',
});

const botSupportsTyping = bot => bot.startTyping;

// Currently only supports Facebook response
const isTypingMessage = message =>
message.sender_action && message.sender_action === 'typing_on';

const botkitMiddlewareTyping = (bot, message, next) => {
const send = () => {
console.log('Bot typing middleware');
if (botSupportsTyping(bot) && !isTypingMessage(message)) {
const typingIndicator = typingIndicatorMessage(bot, message);
const typingDelay = calculateTypingDelay(message);
bot.send(typingIndicator, () => {
setTimeout(() => next(), typingDelay);
});
} else {
next();
}
};

return {
send,
};
const botkitMiddlewareTyping = (config = {}) => (bot, message, next) => {
if (botSupportsTyping(bot) && !isTypingMessage(message)) {
// const typingIndicator = typingIndicatorMessage(message);
// const typingDelay = calculateTypingDelay(message);
bot.send(typingIndicatorMessage(message), () => {
setTimeout(() => next(), typingDelay);
});
} else {
next();
}
};

// const config = require('../config');

// const AVERAGE_WORDS_PER_MINUTE = 85;
// const AVERAGE_CHARACTERS_PER_MINUTE = AVERAGE_WORDS_PER_MINUTE * 7;
// const DEFAULT_ATTACHMENT_TEXT_LENGTH = 80;

// const MAXIMUM_TYPING_DELAY =
// config.get('application.maximumTypingDelaySeconds') * 1000;

// const calculateTypingDelay = response => {
// let textLength;
// if (typeof response === 'string') {
// textLength = response.length;
// } else if (response.text) {
// textLength = response.text.length;
// } else {
// textLength = DEFAULT_ATTACHMENT_TEXT_LENGTH;
// }

// const textSpeed =
// Math.floor(textLength / (AVERAGE_CHARACTERS_PER_MINUTE / 60)) * 1000;

// return Math.min(textSpeed, MAXIMUM_TYPING_DELAY);
// };

// const typingIndicatorMessage = message => ({
// recipient: { id: message.to },
// channel: message.channel,
// sender_action: 'typing_on',
// });

// const botSupportsTyping = bot => bot.startTyping;

// const isTypingMessage = message =>
// message.sender_action && message.sender_action === 'typing_on';

// const typingMiddleware = (bot, message, next) => {
// if (botSupportsTyping(bot) && !isTypingMessage(message)) {
// const typingIndicator = typingIndicatorMessage(message);
// const typingDelay = calculateTypingDelay(message);

// bot.send(typingIndicator, () => {
// setTimeout(() => next(), typingDelay);
// });
// } else {
// next();
// }
// };

module.exports = botkitMiddlewareTyping;
89 changes: 86 additions & 3 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
// const bokitMiddlewareTyping = require('../../src/index');
const typingMiddleware = require('../../src/index');

test('testing Jest', () => {
expect(3).toBe(3);
test('should return a function', () => {
expect(typeof typingMiddleware).toBe('function');
});

describe('when typing is not supported by the bot platform', () => {
let spyNext;
const fakeBot = {
startTyping: false,
};

beforeEach(() => {
spyNext = jest.fn();
});

test('should call next', () => {
const fakeMessage = {};
const sendMiddleware = typingMiddleware();

sendMiddleware(fakeBot, fakeMessage, spyNext);

expect(spyNext.mock.calls.length).toBe(1);
});
});

describe('when typing is supported by the bot platform', () => {
let spyNext;
let spyBotSend;
let fakeBot;

beforeEach(() => {
spyNext = jest.fn();
spyBotSend = jest.fn();

fakeBot = {
startTyping: true,
send: spyBotSend,
};
});

test('should call next if message contains typing indicator', () => {
const fakeMessage = {
sender_action: 'typing_on',
};

const sendMiddleware = typingMiddleware();

sendMiddleware(fakeBot, fakeMessage, spyNext);

expect(spyNext).toHaveBeenCalledTimes(1);
});

test('should call bot.send', () => {
const fakeMessage = {};

const sendMiddleware = typingMiddleware();

sendMiddleware(fakeBot, fakeMessage, spyNext);

expect(spyBotSend).toHaveBeenCalledTimes(1);
});

test('should call bot.send with expected message', () => {
const expectedUserId = 'expectedUserId';
const expectedChannel = 'expectedChannel';

const fakeMessage = {
to: expectedUserId,
channel: expectedChannel,
};

const expectedMessage = {
recipient: { id: expectedUserId },
channel: expectedChannel,
sender_action: 'typing_on',
};

const sendMiddleware = typingMiddleware();

sendMiddleware(fakeBot, fakeMessage, spyNext);

expect(spyBotSend).toHaveBeenCalledWith(
expectedMessage,
expect.any(Function)
);
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,11 @@ eslint-plugin-import@^2.14.0:
read-pkg-up "^2.0.0"
resolve "^1.6.0"

eslint-plugin-jest@^22.4.1:
version "22.4.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-22.4.1.tgz#a5fd6f7a2a41388d16f527073b778013c5189a9c"
integrity sha512-gcLfn6P2PrFAVx3AobaOzlIEevpAEf9chTpFZz7bYfc7pz8XRv7vuKTIE4hxPKZSha6XWKKplDQ0x9Pq8xX2mg==

eslint-plugin-mocha@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-mocha/-/eslint-plugin-mocha-5.2.0.tgz#d8786d9fff8cb8b5f6e4b61e40395d6568a5c4e2"
Expand Down

0 comments on commit a640176

Please sign in to comment.