Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.

Commit a362662

Browse files
author
Roger Mattos
committed
created unit tests to support to new checkbox plugin on authentication event
1 parent 712003a commit a362662

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ jspm_packages
4040

4141
examples/config/development.json
4242
examples/config/production.json
43+
.vscode/settings.json

lib/BootBot.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class BootBot extends EventEmitter {
280280

281281
_handleEvent(type, event, data) {
282282
let chat = null;
283-
if (tpye === 'authentication') {
283+
if (type === 'authentication') {
284284
if (event.sender) {
285285
this.optinOrigin = 'send_to_messenger';
286286
chat = new Chat(this, event.sender.id);
@@ -291,6 +291,7 @@ class BootBot extends EventEmitter {
291291
}
292292
}
293293
else {
294+
this.optinOrigin = null;
294295
chat = new Chat(this, event.sender.id);
295296
}
296297
this.emit(type, event, chat, data);
@@ -373,7 +374,7 @@ class BootBot extends EventEmitter {
373374
break;
374375
}
375376
default:
376-
throw new Error('Invalid optin origin');
377+
objResult = { id: recipientId }
377378
}
378379
return objResult;
379380
}

test/BootBot.spec.js

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,19 @@ describe('BootBot', () => {
177177
const spy = sinon.spy(bot, 'sendRequest');
178178
const elements = [
179179
{
180-
"title":"Welcome to Peter\'s Hats",
181-
"image_url":"http://petersapparel.parseapp.com/img/item100-thumb.png",
182-
"subtitle":"We\'ve got the right hat for everyone.",
183-
"buttons":[
180+
"title": "Welcome to Peter\'s Hats",
181+
"image_url": "http://petersapparel.parseapp.com/img/item100-thumb.png",
182+
"subtitle": "We\'ve got the right hat for everyone.",
183+
"buttons": [
184184
{
185-
"type":"web_url",
186-
"url":"https://petersapparel.parseapp.com/view_item?item_id=100",
187-
"title":"View Website"
185+
"type": "web_url",
186+
"url": "https://petersapparel.parseapp.com/view_item?item_id=100",
187+
"title": "View Website"
188188
},
189189
{
190-
"type":"postback",
191-
"title":"Start Chatting",
192-
"payload":"USER_DEFINED_PAYLOAD"
190+
"type": "postback",
191+
"title": "Start Chatting",
192+
"payload": "USER_DEFINED_PAYLOAD"
193193
}
194194
]
195195
}
@@ -234,4 +234,83 @@ describe('BootBot', () => {
234234
bot.sendAttachment(userId, type, url);
235235
expect(spy.calledWith(expected)).to.equal(true);
236236
});
237-
});
237+
238+
describe('_handleEvent', () => {
239+
let pageId = 123456;
240+
241+
it('should set optin origin to send_to_messenger when sender exists', () => {
242+
const type = 'authentication';
243+
const event = {
244+
sender: {
245+
id: userId
246+
},
247+
recipient: {
248+
id: pageId
249+
},
250+
timestamp: 1234567890,
251+
optin: {
252+
ref: 'PASS_THROUGH_PARAM'
253+
}
254+
};
255+
256+
bot._handleEvent(type, event);
257+
expect(bot.optinOrigin).to.equal('send_to_messenger');
258+
});
259+
260+
it('should set optin origin to checkbox_plugin when sender not exists', () => {
261+
const type = 'authentication';
262+
const event = {
263+
recipient: {
264+
id: pageId
265+
},
266+
timestamp: 1234567890,
267+
optin: {
268+
ref: "PASS_THROUGH_PARAM",
269+
user_ref: userId
270+
}
271+
}
272+
273+
bot._handleEvent(type, event);
274+
expect(bot.optinOrigin).to.equal('checkbox_plugin');
275+
});
276+
277+
it('optin origin should be null when tpye is not authentication', () => {
278+
const type = 'read';
279+
const event = {
280+
sender: {
281+
id: userId
282+
},
283+
recipient: {
284+
id: pageId
285+
},
286+
timestamp: 1234567890,
287+
optin: {
288+
ref: 'PASS_THROUGH_PARAM'
289+
}
290+
};
291+
292+
bot._handleEvent(type, event);
293+
expect(bot.optinOrigin).to.be.null;
294+
});
295+
296+
it('should call emit once', () => {
297+
const spy = sinon.spy(bot, 'emit');
298+
const type = 'read';
299+
const event = {
300+
sender: {
301+
id: userId
302+
},
303+
recipient: {
304+
id: pageId
305+
},
306+
timestamp: 1234567890,
307+
optin: {
308+
ref: 'PASS_THROUGH_PARAM'
309+
}
310+
};
311+
312+
bot._handleEvent(type, event);
313+
expect(spy.calledOnce).to.equal(true);
314+
});
315+
});
316+
});

0 commit comments

Comments
 (0)