Commit cdc350f5 authored by JC Brand's avatar JC Brand

Add more tests for events emitting. Updates #48

parent 986c1788
...@@ -2327,7 +2327,6 @@ ...@@ -2327,7 +2327,6 @@
// not broadcasted // not broadcasted
return true; return true;
} }
converse.emit('onMessage', message);
var $forwarded = $message.children('forwarded'); var $forwarded = $message.children('forwarded');
if ($forwarded.length) { if ($forwarded.length) {
$message = $forwarded.children('message'); $message = $forwarded.children('message');
...@@ -2367,6 +2366,7 @@ ...@@ -2367,6 +2366,7 @@
} }
chatbox.messageReceived(message); chatbox.messageReceived(message);
converse.roster.addResource(buddy_jid, resource); converse.roster.addResource(buddy_jid, resource);
converse.emit('onMessage', message);
return true; return true;
} }
}); });
......
...@@ -39,7 +39,10 @@ ...@@ -39,7 +39,10 @@
}, converse)); }, converse));
it("can be saved to, and retrieved from, localStorage", $.proxy(function () { it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
spyOn(converse, 'emit');
utils.closeControlBox(); utils.closeControlBox();
expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
// First, we open 6 more chatboxes (controlbox is already open) // First, we open 6 more chatboxes (controlbox is already open)
utils.openChatBoxes(6); utils.openChatBoxes(6);
// We instantiate a new ChatBoxes collection, which by default // We instantiate a new ChatBoxes collection, which by default
...@@ -62,6 +65,7 @@ ...@@ -62,6 +65,7 @@
}, converse)); }, converse));
it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () { it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
spyOn(converse, 'emit');
var chatbox, view, $el, var chatbox, view, $el,
num_open_chats = this.chatboxes.length; num_open_chats = this.chatboxes.length;
for (i=0; i<num_open_chats; i++) { for (i=0; i<num_open_chats; i++) {
...@@ -71,17 +75,21 @@ ...@@ -71,17 +75,21 @@
view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
view.$el.find('.close-chatbox-button').click(); view.$el.find('.close-chatbox-button').click();
expect(view.closeChat).toHaveBeenCalled(); expect(view.closeChat).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
} }
}, converse)); }, converse));
it("will be removed from localStorage when closed", $.proxy(function () { it("will be removed from localStorage when closed", $.proxy(function () {
spyOn(converse, 'emit');
this.chatboxes.localStorage._clear(); this.chatboxes.localStorage._clear();
utils.closeControlBox(); utils.closeControlBox();
expect(converse.chatboxes.length).toEqual(0); expect(converse.chatboxes.length).toEqual(0);
utils.openChatBoxes(6); utils.openChatBoxes(6);
expect(converse.chatboxes.length).toEqual(6); expect(converse.chatboxes.length).toEqual(6);
expect(converse.emit).toHaveBeenCalledWith('onChatBoxOpened', jasmine.any(Object));
utils.closeAllChatBoxes(); utils.closeAllChatBoxes();
expect(converse.chatboxes.length).toEqual(0); expect(converse.chatboxes.length).toEqual(0);
expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
var newchatboxes = new this.ChatBoxes(); var newchatboxes = new this.ChatBoxes();
expect(newchatboxes.length).toEqual(0); expect(newchatboxes.length).toEqual(0);
// onConnected will fetch chatboxes in localStorage, but // onConnected will fetch chatboxes in localStorage, but
...@@ -140,7 +148,6 @@ ...@@ -140,7 +148,6 @@
expect($($items[10]).children('a').data('emoticon')).toBe(':O'); expect($($items[10]).children('a').data('emoticon')).toBe(':O');
expect($($items[11]).children('a').data('emoticon')).toBe('(^.^)b'); expect($($items[11]).children('a').data('emoticon')).toBe('(^.^)b');
expect($($items[12]).children('a').data('emoticon')).toBe('<3'); expect($($items[12]).children('a').data('emoticon')).toBe('<3');
$items[0].click(); $items[0].click();
}); });
waits(250); waits(250);
...@@ -192,6 +199,7 @@ ...@@ -192,6 +199,7 @@
describe("A Chat Message", $.proxy(function () { describe("A Chat Message", $.proxy(function () {
it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () { it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
spyOn(converse, 'emit');
var message = 'This is a received message'; var message = 'This is a received message';
var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
msg = $msg({ msg = $msg({
...@@ -209,6 +217,7 @@ ...@@ -209,6 +217,7 @@
// messageReceived is a handler for received XMPP // messageReceived is a handler for received XMPP
// messages // messages
this.chatboxes.messageReceived(msg); this.chatboxes.messageReceived(msg);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
}, converse)); }, converse));
waits(300); waits(300);
runs($.proxy(function () { runs($.proxy(function () {
...@@ -238,6 +247,7 @@ ...@@ -238,6 +247,7 @@
}, converse)); }, converse));
it("will indate when it has a time difference of more than a day between it and it's predecessor", $.proxy(function () { it("will indate when it has a time difference of more than a day between it and it's predecessor", $.proxy(function () {
spyOn(converse, 'emit');
var contact_name = mock.cur_names[1]; var contact_name = mock.cur_names[1];
var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost'; var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
...@@ -252,16 +262,16 @@ ...@@ -252,16 +262,16 @@
var msg_txt; var msg_txt;
var sender_txt; var sender_txt;
this.chatboxes.messageReceived( var msg = $msg({
$msg({ from: contact_jid,
from: contact_jid, to: this.connection.jid,
to: this.connection.jid, type: 'chat',
type: 'chat', id: one_day_ago.getTime()
id: one_day_ago.getTime() }).c('body').t(message).up()
}).c('body').t(message).up() .c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) })
.c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) }) .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree() this.chatboxes.messageReceived(msg);
); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
expect(chatbox.messages.length).toEqual(1); expect(chatbox.messages.length).toEqual(1);
msg_obj = chatbox.messages.models[0]; msg_obj = chatbox.messages.models[0];
expect(msg_obj.get('message')).toEqual(message); expect(msg_obj.get('message')).toEqual(message);
...@@ -274,16 +284,15 @@ ...@@ -274,16 +284,15 @@
expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy(); expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
message = 'This is a current message'; message = 'This is a current message';
this.chatboxes.messageReceived( msg = $msg({
$msg({ from: contact_jid,
from: contact_jid, to: this.connection.jid,
to: this.connection.jid, type: 'chat',
type: 'chat', id: new Date().getTime()
id: new Date().getTime() }).c('body').t(message).up()
}).c('body').t(message).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree() this.chatboxes.messageReceived(msg);
); expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
// Check that there is a <time> element, with the required // Check that there is a <time> element, with the required
// props. // props.
var $time = $chat_content.find('time'); var $time = $chat_content.find('time');
...@@ -308,8 +317,10 @@ ...@@ -308,8 +317,10 @@
}, converse)); }, converse));
it("can be sent from a chatbox, and will appear inside it", $.proxy(function () { it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
spyOn(converse, 'emit');
var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
utils.openChatBoxFor(contact_jid); utils.openChatBoxFor(contact_jid);
expect(converse.emit).toHaveBeenCalledWith('onChatBoxOpened', jasmine.any(Object));
var view = this.chatboxesview.views[contact_jid]; var view = this.chatboxesview.views[contact_jid];
var message = 'This message is sent from this chatbox'; var message = 'This message is sent from this chatbox';
spyOn(view, 'sendMessage').andCallThrough(); spyOn(view, 'sendMessage').andCallThrough();
...@@ -317,6 +328,8 @@ ...@@ -317,6 +328,8 @@
view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
expect(view.sendMessage).toHaveBeenCalled(); expect(view.sendMessage).toHaveBeenCalled();
expect(view.model.messages.length, 2); expect(view.model.messages.length, 2);
expect(converse.emit.callCount, 2);
expect(converse.emit.mostRecentCall.args, ['onMessageSend', message]);
var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text(); var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
expect(txt).toEqual(message); expect(txt).toEqual(message);
}, converse)); }, converse));
...@@ -325,6 +338,7 @@ ...@@ -325,6 +338,7 @@
describe("Special Messages", $.proxy(function () { describe("Special Messages", $.proxy(function () {
it("'/clear' can be used to clear messages in a conversation", $.proxy(function () { it("'/clear' can be used to clear messages in a conversation", $.proxy(function () {
spyOn(converse, 'emit');
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.chatboxesview.views[contact_jid]; var view = this.chatboxesview.views[contact_jid];
var message = 'This message is another sent from this chatbox'; var message = 'This message is another sent from this chatbox';
...@@ -334,6 +348,7 @@ ...@@ -334,6 +348,7 @@
view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
expect(view.model.messages.length > 0).toBeTruthy(); expect(view.model.messages.length > 0).toBeTruthy();
expect(view.model.messages.localStorage.records.length > 0).toBeTruthy(); expect(view.model.messages.localStorage.records.length > 0).toBeTruthy();
expect(converse.emit).toHaveBeenCalledWith('onMessageSend', message);
message = '/clear'; message = '/clear';
var old_length = view.model.messages.length; var old_length = view.model.messages.length;
...@@ -343,6 +358,8 @@ ...@@ -343,6 +358,8 @@
expect(view.sendMessage).toHaveBeenCalled(); expect(view.sendMessage).toHaveBeenCalled();
expect(view.model.messages.length, 0); // The messages must be removed from the modal expect(view.model.messages.length, 0); // The messages must be removed from the modal
expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage
expect(converse.emit.callCount, 1);
expect(converse.emit.mostRecentCall.args, ['onMessageSend', message]);
}, converse)); }, converse));
}, converse)); }, converse));
...@@ -352,6 +369,7 @@ ...@@ -352,6 +369,7 @@
}, converse)); }, converse));
it("is incremented when the message is received and the window is not focused", $.proxy(function () { it("is incremented when the message is received and the window is not focused", $.proxy(function () {
spyOn(converse, 'emit');
expect(this.msg_counter).toBe(0); expect(this.msg_counter).toBe(0);
spyOn(converse, 'incrementMsgCounter').andCallThrough(); spyOn(converse, 'incrementMsgCounter').andCallThrough();
$(window).trigger('blur'); $(window).trigger('blur');
...@@ -367,6 +385,7 @@ ...@@ -367,6 +385,7 @@
this.chatboxes.messageReceived(msg); this.chatboxes.messageReceived(msg);
expect(converse.incrementMsgCounter).toHaveBeenCalled(); expect(converse.incrementMsgCounter).toHaveBeenCalled();
expect(this.msg_counter).toBe(1); expect(this.msg_counter).toBe(1);
expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
}, converse)); }, converse));
it("is cleared when the window is focused", $.proxy(function () { it("is cleared when the window is focused", $.proxy(function () {
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
} (this, function (mock, utils) { } (this, function (mock, utils) {
return describe("ChatRooms", $.proxy(function (mock, utils) { return describe("ChatRooms", $.proxy(function (mock, utils) {
describe("A Chat Room", $.proxy(function () { describe("A Chat Room", $.proxy(function () {
beforeEach(function () { beforeEach(function () {
utils.closeAllChatBoxes(); utils.closeAllChatBoxes();
utils.createNewChatRoom('lounge', 'dummy'); utils.createNewChatRoom('lounge', 'dummy');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment