Commit dcd1a847 authored by JC Brand's avatar JC Brand

Fix failing tests.

We now need to mock some extra XMPP stanzas being received when a user enters a chat
room in tests.
parent f56b85cf
...@@ -171,7 +171,7 @@ ...@@ -171,7 +171,7 @@
}); });
it("shows users currently present in the room", function () { it("shows users currently present in the room", function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
var name; var name;
var view = this.chatboxviews.get('lounge@localhost'), var view = this.chatboxviews.get('lounge@localhost'),
$occupants = view.$('.occupant-list'); $occupants = view.$('.occupant-list');
...@@ -194,9 +194,9 @@ ...@@ -194,9 +194,9 @@
converse.connection._dataRecv(test_utils.createRequest(presence)); converse.connection._dataRecv(test_utils.createRequest(presence));
expect(view.onChatRoomPresence).toHaveBeenCalled(); expect(view.onChatRoomPresence).toHaveBeenCalled();
expect($occupants.find('li').length).toBe(1+i); expect($occupants.find('li').length).toBe(2+i);
expect($($occupants.find('li')[i]).text()).toBe(mock.chatroom_names[i]); expect($($occupants.find('li')[i+1]).text()).toBe(mock.chatroom_names[i]);
expect($($occupants.find('li')[i]).hasClass('moderator')).toBe(role === "moderator"); expect($($occupants.find('li')[i+1]).hasClass('moderator')).toBe(role === "moderator");
} }
// Test users leaving the room // Test users leaving the room
...@@ -217,13 +217,13 @@ ...@@ -217,13 +217,13 @@
}).nodeTree; }).nodeTree;
converse.connection._dataRecv(test_utils.createRequest(presence)); converse.connection._dataRecv(test_utils.createRequest(presence));
expect(view.onChatRoomPresence).toHaveBeenCalled(); expect(view.onChatRoomPresence).toHaveBeenCalled();
expect($occupants.find('li.online').length).toBe(i); expect($occupants.find('li.online').length).toBe(i+1);
} }
}.bind(converse)); }.bind(converse));
it("indicates moderators by means of a special css class and tooltip", function () { it("indicates moderators by means of a special css class and tooltip", function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
var view = this.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
var presence = $pres({ var presence = $pres({
to:'dummy@localhost/pda', to:'dummy@localhost/pda',
...@@ -236,13 +236,14 @@ ...@@ -236,13 +236,14 @@
}).up() }).up()
.c('status').attrs({code:'110'}).nodeTree; .c('status').attrs({code:'110'}).nodeTree;
this.connection._dataRecv(test_utils.createRequest(presence)); converse.connection._dataRecv(test_utils.createRequest(presence));
var occupant = view.$el.find('.occupant-list').find('li'); var occupant = view.$el.find('.occupant-list').find('li');
expect(occupant.length).toBe(1); expect(occupant.length).toBe(2);
expect($(occupant).text()).toBe("moderatorman"); expect($(occupant).first().text()).toBe("dummy");
expect($(occupant).attr('class').indexOf('moderator')).not.toBe(-1); expect($(occupant).last().text()).toBe("moderatorman");
expect($(occupant).attr('title')).toBe('This user is a moderator'); expect($(occupant).last().attr('class').indexOf('moderator')).not.toBe(-1);
}.bind(converse)); expect($(occupant).last().attr('title')).toBe('This user is a moderator');
});
it("will use the user's reserved nickname, if it exists", function () { it("will use the user's reserved nickname, if it exists", function () {
var sent_IQ, IQ_id; var sent_IQ, IQ_id;
...@@ -269,7 +270,6 @@ ...@@ -269,7 +270,6 @@
"type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+ "type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
"<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>" "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>"
); );
/* <iq from='coven@chat.shakespeare.lit' /* <iq from='coven@chat.shakespeare.lit'
* id='getnick1' * id='getnick1'
* to='hag66@shakespeare.lit/pda' * to='hag66@shakespeare.lit/pda'
...@@ -397,7 +397,7 @@ ...@@ -397,7 +397,7 @@
}.bind(converse)); }.bind(converse));
it("shows sent groupchat messages", function () { it("shows sent groupchat messages", function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); } if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
...@@ -405,7 +405,11 @@ ...@@ -405,7 +405,11 @@
view.$el.find('.chat-textarea').text(text); view.$el.find('.chat-textarea').text(text);
view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
expect(converse.emit).toHaveBeenCalledWith('messageSend', text); expect(converse.emit).toHaveBeenCalledWith('messageSend', text);
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.find('.chat-message').length).toBe(1);
// Let's check that if we receive the same message again, it's
// not shown.
var message = $msg({ var message = $msg({
from: 'lounge@localhost/dummy', from: 'lounge@localhost/dummy',
to: 'dummy@localhost.com', to: 'dummy@localhost.com',
...@@ -413,7 +417,6 @@ ...@@ -413,7 +417,6 @@
id: view.model.messages.at(0).get('msgid') id: view.model.messages.at(0).get('msgid')
}).c('body').t(text); }).c('body').t(text);
view.onChatRoomMessage(message.nodeTree); view.onChatRoomMessage(message.nodeTree);
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.find('.chat-message').length).toBe(1); expect($chat_content.find('.chat-message').length).toBe(1);
expect($chat_content.find('.chat-msg-content').last().text()).toBe(text); expect($chat_content.find('.chat-msg-content').last().text()).toBe(text);
// We don't emit an event if it's our own message // We don't emit an event if it's our own message
...@@ -422,7 +425,7 @@ ...@@ -422,7 +425,7 @@
it("will cause the chat area to be scrolled down only if it was at the bottom already", function () { it("will cause the chat area to be scrolled down only if it was at the bottom already", function () {
var message = 'This message is received while the chat area is scrolled up'; var message = 'This message is received while the chat area is scrolled up';
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'scrollDown').andCallThrough(); spyOn(view, 'scrollDown').andCallThrough();
runs(function () { runs(function () {
...@@ -465,6 +468,8 @@ ...@@ -465,6 +468,8 @@
}); });
it("shows received chatroom subject messages", function () { it("shows received chatroom subject messages", function () {
test_utils.openAndEnterChatRoom('jdev', 'conference.jabber.org', 'jc');
var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org'; var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
var stanza = Strophe.xmlHtmlNode( var stanza = Strophe.xmlHtmlNode(
'<message xmlns="jabber:client" to="jc@opkode.com/converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+ '<message xmlns="jabber:client" to="jc@opkode.com/converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
...@@ -472,7 +477,6 @@ ...@@ -472,7 +477,6 @@
' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+ ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+ ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
'</message>').firstChild; '</message>').firstChild;
test_utils.openChatRoom('jdev', 'conference.jabber.org', 'jc');
converse.connection._dataRecv(test_utils.createRequest(stanza)); converse.connection._dataRecv(test_utils.createRequest(stanza));
var view = converse.chatboxviews.get('jdev@conference.jabber.org'); var view = converse.chatboxviews.get('jdev@conference.jabber.org');
var $chat_content = view.$el.find('.chat-content'); var $chat_content = view.$el.find('.chat-content');
...@@ -517,7 +521,7 @@ ...@@ -517,7 +521,7 @@
* </presence> * </presence>
*/ */
var __ = utils.__.bind(converse); var __ = utils.__.bind(converse);
test_utils.openChatRoom('lounge', 'localhost', 'oldnick'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'oldnick');
var view = this.chatboxviews.get('lounge@localhost'); var view = this.chatboxviews.get('lounge@localhost');
var $chat_content = view.$el.find('.chat-content'); var $chat_content = view.$el.find('.chat-content');
spyOn(view, 'onChatRoomPresence').andCallThrough(); spyOn(view, 'onChatRoomPresence').andCallThrough();
...@@ -613,7 +617,7 @@ ...@@ -613,7 +617,7 @@
* </x> * </x>
* </presence> * </presence>
*/ */
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/dummy', from:'lounge@localhost/dummy',
to:'dummy@localhost/pda', to:'dummy@localhost/pda',
......
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
}); });
it("is shown when you are mentioned in a chat room", function () { it("is shown when you are mentioned in a chat room", function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); } if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
var no_notification = false; var no_notification = false;
...@@ -104,7 +104,7 @@ ...@@ -104,7 +104,7 @@
describe("A notification sound", function () { describe("A notification sound", function () {
it("is played when the current user is mentioned in a chat room", function () { it("is played when the current user is mentioned in a chat room", function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy');
converse.play_sounds = true; converse.play_sounds = true;
spyOn(converse, 'playSoundNotification'); spyOn(converse, 'playSoundNotification');
var view = this.chatboxviews.get('lounge@localhost'); var view = this.chatboxviews.get('lounge@localhost');
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
return factory($, mock); return factory($, mock);
}); });
}(this, function ($, mock) { }(this, function ($, mock) {
var $pres = converse_api.env.$pres;
var $iq = converse_api.env.$iq;
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var utils = {}; var utils = {};
...@@ -113,6 +115,41 @@ ...@@ -113,6 +115,41 @@
this.closeControlBox(); this.closeControlBox();
}; };
utils.openAndEnterChatRoom = function (room, server, nick) {
var IQ_id, sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
utils.openChatRoom(room, server);
var view = converse.chatboxviews.get(room+'@'+server);
// The XMPP server returns the reserved nick for this user.
var stanza = $iq({
'type': 'result',
'id': IQ_id,
'from': view.model.get('jid'),
'to': converse.connection.jid
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
.c('identity', {'category': 'conference', 'name': nick, 'type': 'text'});
converse.connection._dataRecv(utils.createRequest(stanza));
// The user has just entered the room (because join was called)
// and receives their own presence from the server.
// See example 24: http://xmpp.org/extensions/xep-0045.html#enter-pres
var presence = $pres({
to: converse.connection.jid,
from: room+'@'+server+'/'+nick,
id: 'DC352437-C019-40EC-B590-AF29E879AF97'
}).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
.c('item').attrs({
affiliation: 'member',
jid: converse.bare_jid,
role: 'occupant'
}).up()
.c('status').attrs({code:'110'});
converse.connection._dataRecv(utils.createRequest(presence));
};
utils.removeRosterContacts = function () { utils.removeRosterContacts = function () {
var model; var model;
while (converse.rosterview.model.length) { while (converse.rosterview.model.length) {
......
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