(function (root, factory) {
define(["mock", "converse-core", "test-utils", "utils" ], factory);
} (this, function (mock, converse, test_utils, utils) {
var _ = converse.env._;
var $ = converse.env.jQuery;
var $pres = converse.env.$pres;
var $iq = converse.env.$iq;
var $msg = converse.env.$msg;
var Strophe = converse.env.Strophe;
return describe("ChatRooms", function () {
describe("The \"rooms\" API", function () {
it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", mock.initConverse(function (_converse) {
test_utils.createContacts(_converse, 'current');
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy');
expect(_converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
expect(_converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
// XXX: bit of a cheat here. We want `cleanup()` to be
// called on the room. Either it's this or faking
// `sendPresence`.
_converse.connection.connected = false;
_converse.api.rooms.close('lounge@localhost');
expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
expect(_converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
_converse.api.rooms.close(['leisure@localhost', 'news@localhost']);
expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined();
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
expect(_converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
_converse.api.rooms.close();
expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
}));
it("has a method 'get' which returns a wrapped chat room (if it exists)", mock.initConverseWithAsync(function (done, _converse) {
test_utils.createContacts(_converse, 'current');
test_utils.waitUntil(function () {
return _converse.rosterview.$el.find('dt').length;
}, 300)
.then(function () {
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
var jid = 'lounge@localhost';
var room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
expect(room.is_chatroom).toBeTruthy();
var chatroomview = _converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy();
chatroomview.close();
// Test with mixed case
test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy');
jid = 'Leisure@localhost';
room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
jid = 'leisure@localhost';
room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
jid = 'leiSure@localhost';
room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
chatroomview.close();
// Non-existing room
jid = 'lounge2@localhost';
room = _converse.api.rooms.get(jid);
expect(typeof room === 'undefined').toBeTruthy();
done();
});
}));
it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box", mock.initConverseWithAsync(function (done, _converse) {
// Mock 'getRoomFeatures', otherwise the room won't be
// displayed as it waits first for the features to be returned
// (when it's a new room being created).
spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
var deferred = new $.Deferred();
deferred.resolve();
return deferred.promise();
});
test_utils.createContacts(_converse, 'current');
test_utils.waitUntil(function () {
return _converse.rosterview.$el.find('dt').length;
}, 300)
.then(function () {
var chatroomview;
var jid = 'lounge@localhost';
var room = _converse.api.rooms.open(jid);
// Test on chat room that doesn't exist.
expect(room instanceof Object).toBeTruthy();
expect(room.is_chatroom).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy();
// Test again, now that the room exists.
room = _converse.api.rooms.open(jid);
expect(room instanceof Object).toBeTruthy();
expect(room.is_chatroom).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy();
// Test with mixed case in JID
jid = 'Leisure@localhost';
room = _converse.api.rooms.open(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
jid = 'leisure@localhost';
room = _converse.api.rooms.open(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
jid = 'leiSure@localhost';
room = _converse.api.rooms.open(jid);
expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
chatroomview.close();
_converse.muc_instant_rooms = false;
var sent_IQ, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_IQ = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
// Test with configuration
_converse.api.rooms.open('room@conference.example.org', {
'nick': 'some1',
'auto_configure': true,
'roomconfig': {
'changesubject': false,
'membersonly': true,
'persistentroom': true,
'publicroom': true,
'roomdesc': 'Welcome to this room',
'whois': 'anyone'
}
});
// We pretend this is a new room, so no disco info is returned.
var features_stanza = $iq({
from: 'room@conference.example.org',
'id': IQ_id,
'to': 'dummy@localhost/desktop',
'type': 'error'
}).c('error', {'type': 'cancel'})
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(features_stanza));
/*
*
*
*
*
*
*
*/
var presence = $pres({
from:'room@conference.example.org/some1',
to:'dummy@localhost/pda'
})
.c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
.c('item', {
affiliation: 'owner',
jid: 'dummy@localhost/pda',
role: 'moderator'
}).up()
.c('status', {code:'110'}).up()
.c('status', {code:'201'});
_converse.connection._dataRecv(test_utils.createRequest(presence));
expect(_converse.connection.sendIQ).toHaveBeenCalled();
expect(sent_IQ.toLocaleString()).toBe(
""
);
_converse.connection._dataRecv(test_utils.createRequest($(
''+
' '+
' '+
' Configuration for room@conference.example.org'+
' Complete and submit this form to configure the room.'+
' '+
' http://jabber.org/protocol/muc#roomconfig'+
' '+
' '+
' Room'+
' '+
' '+
' '+
' 1'+
' '+
' '+
' '+
' '+
' '+
' '+
' '+
' 20'+
' '+
' '+
' ')[0]));
var $sent_stanza = $(sent_IQ.toLocaleString());
expect($sent_stanza.find('field[var="muc#roomconfig_roomname"] value').text()).toBe('Room');
expect($sent_stanza.find('field[var="muc#roomconfig_roomdesc"] value').text()).toBe('Welcome to this room');
expect($sent_stanza.find('field[var="muc#roomconfig_persistentroom"] value').text()).toBe('1');
expect($sent_stanza.find('field[var="muc#roomconfig_publicroom"] value ').text()).toBe('1');
expect($sent_stanza.find('field[var="muc#roomconfig_changesubject"] value').text()).toBe('0');
expect($sent_stanza.find('field[var="muc#roomconfig_whois"] value ').text()).toBe('anyone');
expect($sent_stanza.find('field[var="muc#roomconfig_membersonly"] value').text()).toBe('1');
expect($sent_stanza.find('field[var="muc#roomconfig_historylength"] value').text()).toBe('20');
done();
});
}));
});
describe("An instant chat room", function () {
it("will be created when muc_instant_rooms is set to true", mock.initConverse(function (_converse) {
var sent_IQ, IQ_id;
var sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_IQ = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
/*
*
*
*
*
*
*
*
*/
test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
// We pretend this is a new room, so no disco info is returned.
var features_stanza = $iq({
from: 'lounge@localhost',
'id': IQ_id,
'to': 'dummy@localhost/desktop',
'type': 'error'
}).c('error', {'type': 'cancel'})
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(features_stanza));
var view = _converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'join').and.callThrough();
/*
*
*
*/
expect(sent_IQ.toLocaleString()).toBe(
""+
""
);
/* *
*
*
*
*
*/
var stanza = $iq({
'type': 'error',
'id': IQ_id,
'from': view.model.get('jid'),
'to': _converse.connection.jid
}).c('error', {'type': 'cancel'})
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(stanza));
// TODO: enter nickname
var $input = view.$el.find('input.new-chatroom-nick');
$input.val('nicky').parents('form').submit();
expect(view.join).toHaveBeenCalled();
// 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:'dummy@localhost/resource',
from:'lounge@localhost/thirdwitch',
id:'5025e055-036c-4bc5-a227-706e7e352053'
}).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
.c('item').attrs({
affiliation: 'owner',
jid: 'dummy@localhost/resource',
role: 'moderator'
}).up()
.c('status').attrs({code:'110'}).up()
.c('status').attrs({code:'201'}).nodeTree;
_converse.connection._dataRecv(test_utils.createRequest(presence));
var info_text = view.$el.find('.chat-content .chat-info').text();
expect(info_text).toBe('A new room has been created');
// An instant room is created by saving the default configuratoin.
//
/*
*
*
*/
expect(sent_IQ.toLocaleString()).toBe(
""+
""+
"");
}));
});
describe("A Chat Room", function () {
it("shows join/leave messages when users enter or exit a room", mock.initConverse(function (_converse) {
test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
var $chat_content = view.$el.find('.chat-content');
/* We don't show join/leave messages for existing occupants. We
* know about them because we receive their presences before we
* receive our own.
*/
presence = $pres({
to: 'dummy@localhost/_converse.js-29092160',
from: 'coven@chat.shakespeare.lit/oldguy'
}).c('x', {xmlns: Strophe.NS.MUC_USER})
.c('item', {
'affiliation': 'none',
'jid': 'oldguy@localhost/_converse.js-290929789',
'role': 'participant'
});
_converse.connection._dataRecv(test_utils.createRequest(presence));
expect($chat_content.find('div.chat-info').length).toBe(0);
/*
*
*
*
*
*