(function (root, factory) {
define(["mock", "converse-api", "test_utils", "utils" ], factory);
} (this, function (mock, converse_api, test_utils, utils) {
var _ = converse_api.env._;
var $ = converse_api.env.jQuery;
var $pres = converse_api.env.$pres;
var $iq = converse_api.env.$iq;
var $msg = converse_api.env.$msg;
var Strophe = converse_api.env.Strophe;
return describe("ChatRooms", function () {
describe("The \"rooms\" API", function () {
afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
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');
runs(function () {
test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
test_utils.openChatRoom(converse, 'leisure', 'localhost', 'dummy');
test_utils.openChatRoom(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();
});
waits('100');
runs(function () {
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.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
test_utils.openChatRoom(converse, 'leisure', 'localhost', 'dummy');
expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
});
waits('100');
runs(function () {
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.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
test_utils.openChatRoom(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();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// Test with mixed case
test_utils.openChatRoom(converse, 'Leisure', 'localhost', 'dummy');
var jid = 'Leisure@localhost';
var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
var jid = 'leisure@localhost';
var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
var 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();
});
}));
it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
var chatroomview;
var jid = 'lounge@localhost';
var room = converse_api.rooms.open(jid);
runs(function () {
// 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();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// 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();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// 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();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
converse.muc_instant_rooms = false;
var sent_IQ, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(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');
});
}));
});
describe("A Chat Room", function () {
afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("can have spaces and special characters in its name", mock.initConverse(function (converse) {
test_utils.openChatRoom(converse, 'lounge & leisure', 'localhost', 'dummy');
var view = converse.chatboxviews.get(
Strophe.escapeNode('lounge & leisure')+'@localhost');
expect(view instanceof converse.ChatRoomView).toBe(true);
}));
it("can be configured if you're its owner", mock.initConverse(function (converse) {
var view;
var sent_IQ, IQ_id;
var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_IQ = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
runs(function () {
converse_api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
view = converse.chatboxviews.get('coven@chat.shakespeare.lit');
spyOn(view, 'saveAffiliationAndRole').andCallThrough();
// We pretend this is a new room, so no disco info is returned.
var features_stanza = $iq({
from: 'coven@chat.shakespeare.lit',
'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));
/*
*
*
*
*
*