Commit a2f4acdc authored by JC Brand's avatar JC Brand

All tests suites now updated and isolated.

parent 3e409573
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery", "jquery",
...@@ -14,7 +13,12 @@ ...@@ -14,7 +13,12 @@
describe("A chat room", function () { describe("A chat room", function () {
it("can be bookmarked", function () { afterEach(function () {
converse_api.user.logout();
test_utils.clearBrowserStorage();
});
it("can be bookmarked", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -23,7 +27,7 @@ ...@@ -23,7 +27,7 @@
}); });
spyOn(converse.connection, 'getUniqueId').andCallThrough(); spyOn(converse.connection, 'getUniqueId').andCallThrough();
test_utils.openChatRoom('theplay', 'conference.shakespeare.lit', 'JC'); test_utils.openChatRoom(converse, 'theplay', 'conference.shakespeare.lit', 'JC');
var jid = 'theplay@conference.shakespeare.lit'; var jid = 'theplay@conference.shakespeare.lit';
var view = converse.chatboxviews.get(jid); var view = converse.chatboxviews.get(jid);
spyOn(view, 'renderBookmarkForm').andCallThrough(); spyOn(view, 'renderBookmarkForm').andCallThrough();
...@@ -121,9 +125,9 @@ ...@@ -121,9 +125,9 @@
converse.connection._dataRecv(test_utils.createRequest(stanza)); converse.connection._dataRecv(test_utils.createRequest(stanza));
// We ignore this IQ stanza... (unless it's an error stanza), so // We ignore this IQ stanza... (unless it's an error stanza), so
// nothing to test for here. // nothing to test for here.
}); }));
it("will be automatically opened if 'autojoin' is set on the bookmark", function () { it("will be automatically opened if 'autojoin' is set on the bookmark", mock.initConverse(function (converse) {
var jid = 'lounge@localhost'; var jid = 'lounge@localhost';
converse.bookmarks.create({ converse.bookmarks.create({
'jid': jid, 'jid': jid,
...@@ -141,17 +145,18 @@ ...@@ -141,17 +145,18 @@
'nick': ' Othello' 'nick': ' Othello'
}); });
expect(_.isUndefined(converse.chatboxviews.get(jid))).toBeFalsy(); expect(_.isUndefined(converse.chatboxviews.get(jid))).toBeFalsy();
}); }));
describe("when bookmarked", function () { describe("when bookmarked", function () {
beforeEach(function () {
test_utils.closeAllChatBoxes(); afterEach(function () {
converse.bookmarks.reset(); converse_api.user.logout();
test_utils.clearBrowserStorage();
}); });
it("displays that it's bookmarked through its bookmark icon", function () { it("displays that it's bookmarked through its bookmark icon", mock.initConverse(function (converse) {
runs(function () { runs(function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
}); });
waits(100); waits(100);
runs(function () { runs(function () {
...@@ -163,9 +168,9 @@ ...@@ -163,9 +168,9 @@
view.model.set('bookmarked', false); view.model.set('bookmarked', false);
expect($bookmark_icon.hasClass('button-on')).toBeFalsy(); expect($bookmark_icon.hasClass('button-on')).toBeFalsy();
}); });
}); }));
it("can be unbookmarked", function () { it("can be unbookmarked", mock.initConverse(function (converse) {
var view, sent_stanza, IQ_id; var view, sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -175,7 +180,7 @@ ...@@ -175,7 +180,7 @@
spyOn(converse.connection, 'getUniqueId').andCallThrough(); spyOn(converse.connection, 'getUniqueId').andCallThrough();
runs(function () { runs(function () {
test_utils.openChatRoom('theplay', 'conference.shakespeare.lit', 'JC'); test_utils.openChatRoom(converse, 'theplay', 'conference.shakespeare.lit', 'JC');
}); });
waits(100); waits(100);
runs(function () { runs(function () {
...@@ -229,15 +234,16 @@ ...@@ -229,15 +234,16 @@
"</iq>" "</iq>"
); );
}); });
}); }));
}); });
describe("and when autojoin is set", function () { describe("and when autojoin is set", function () {
beforeEach(function () { afterEach(function () {
converse.bookmarks.reset(); converse_api.user.logout();
test_utils.clearBrowserStorage();
}); });
it("will be be opened and joined automatically upon login", function () { it("will be be opened and joined automatically upon login", mock.initConverse(function (converse) {
spyOn(converse_api.rooms, 'open'); spyOn(converse_api.rooms, 'open');
var jid = 'theplay@conference.shakespeare.lit'; var jid = 'theplay@conference.shakespeare.lit';
var model = converse.bookmarks.create({ var model = converse.bookmarks.create({
...@@ -256,17 +262,18 @@ ...@@ -256,17 +262,18 @@
'nick': '' 'nick': ''
}); });
expect(converse_api.rooms.open).toHaveBeenCalled(); expect(converse_api.rooms.open).toHaveBeenCalled();
}); }));
}); });
}); });
describe("Bookmarks", function () { describe("Bookmarks", function () {
beforeEach(function () { afterEach(function () {
window.sessionStorage.clear(); converse_api.user.logout();
test_utils.clearBrowserStorage();
}); });
it("can be pushed from the XMPP server", function () { it("can be pushed from the XMPP server", mock.initConverse(function (converse) {
// TODO // TODO
/* The stored data is automatically pushed to all of the user's /* The stored data is automatically pushed to all of the user's
* connected resources. * connected resources.
...@@ -311,9 +318,9 @@ ...@@ -311,9 +318,9 @@
* </event> * </event>
* </message> * </message>
*/ */
}); }));
it("can be retrieved from the XMPP server", function () { it("can be retrieved from the XMPP server", mock.initConverse(function (converse) {
var sent_stanza, IQ_id, var sent_stanza, IQ_id,
sendIQ = converse.connection.sendIQ; sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -380,21 +387,21 @@ ...@@ -380,21 +387,21 @@
expect(converse.bookmarks.models.length).toBe(2); expect(converse.bookmarks.models.length).toBe(2);
expect(converse.bookmarks.findWhere({'jid': 'theplay@conference.shakespeare.lit'}).get('autojoin')).toBe(true); expect(converse.bookmarks.findWhere({'jid': 'theplay@conference.shakespeare.lit'}).get('autojoin')).toBe(true);
expect(converse.bookmarks.findWhere({'jid': 'another@conference.shakespeare.lit'}).get('autojoin')).toBe(false); expect(converse.bookmarks.findWhere({'jid': 'another@conference.shakespeare.lit'}).get('autojoin')).toBe(false);
}); }));
describe("The rooms panel", function () { describe("The rooms panel", function () {
beforeEach(function () { afterEach(function () {
test_utils.openRoomsPanel(); converse_api.user.logout();
converse.bookmarks.reset(); test_utils.clearBrowserStorage();
converse.chatboxviews.get('controlbox').$('#chatrooms dl.bookmarks').html('');
}); });
it("shows a list of bookmarks", function () { it("shows a list of bookmarks", mock.initConverse(function (converse) {
var IQ_id; var IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
IQ_id = sendIQ.bind(this)(iq, callback, errback); IQ_id = sendIQ.bind(this)(iq, callback, errback);
}); });
converse.chatboxviews.get('controlbox').$('#chatrooms dl.bookmarks').html('');
converse.emit('chatBoxesFetched'); converse.emit('chatBoxesFetched');
var stanza = $iq({'to': converse.connection.jid, 'type':'result', 'id':IQ_id}) var stanza = $iq({'to': converse.connection.jid, 'type':'result', 'id':IQ_id})
.c('pubsub', {'xmlns': Strophe.NS.PUBSUB}) .c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
...@@ -418,7 +425,7 @@ ...@@ -418,7 +425,7 @@
}).c('nick').t('JC').up().up(); }).c('nick').t('JC').up().up();
converse.connection._dataRecv(test_utils.createRequest(stanza)); converse.connection._dataRecv(test_utils.createRequest(stanza));
expect($('#chatrooms dl.bookmarks dd').length).toBe(3); expect($('#chatrooms dl.bookmarks dd').length).toBe(3);
}); }));
}); });
}); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery",
"underscore",
"utils", "utils",
"mock", "mock",
"test_utils" "test_utils"
], factory); ], factory);
} (this, function ($, _, utils, mock, test_utils) { } (this, function (utils, mock, test_utils) {
"use strict"; "use strict";
var _ = converse_api.env._;
var $ = converse_api.env.jQuery;
var $msg = converse_api.env.$msg; var $msg = converse_api.env.$msg;
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var moment = converse_api.env.moment; var moment = converse_api.env.moment;
return describe("Chatboxes", $.proxy(function(mock, test_utils) { return describe("Chatboxes", function() {
describe("A Chatbox", function () { describe("A Chatbox", function () {
beforeEach(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.removeControlBox();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
test_utils.initConverse();
test_utils.createContacts('current');
test_utils.openControlBox();
test_utils.openContactsPanel();
});
}); });
it("is created when you click on a roster item", function () { it("is created when you click on a roster item", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
var i, $el, jid, chatboxview; var i, $el, jid, chatboxview;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
expect(this.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
spyOn(this.chatboxviews, 'trimChats'); spyOn(converse.chatboxviews, 'trimChats');
expect($("#conversejs .chatbox").length).toBe(1); // Controlbox is open expect($("#conversejs .chatbox").length).toBe(1); // Controlbox is open
var online_contacts = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact').find('a.open-chat'); var online_contacts = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact').find('a.open-chat');
for (i=0; i<online_contacts.length; i++) { for (i=0; i<online_contacts.length; i++) {
$el = $(online_contacts[i]); $el = $(online_contacts[i]);
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
$el.click(); $el.click();
chatboxview = this.chatboxviews.get(jid); chatboxview = converse.chatboxviews.get(jid);
expect(this.chatboxes.length).toEqual(i+2); expect(converse.chatboxes.length).toEqual(i+2);
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(converse.chatboxviews.trimChats).toHaveBeenCalled();
// Check that new chat boxes are created to the left of the // Check that new chat boxes are created to the left of the
// controlbox (but to the right of all existing chat boxes) // controlbox (but to the right of all existing chat boxes)
expect($("#conversejs .chatbox").length).toBe(i+2); expect($("#conversejs .chatbox").length).toBe(i+2);
expect($("#conversejs .chatbox")[1].id).toBe(chatboxview.model.get('box_id')); expect($("#conversejs .chatbox")[1].id).toBe(chatboxview.model.get('box_id'));
} }
}.bind(converse)); }));
it("can be trimmed to conserve space", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be trimmed to conserve space", function () {
var i, $el, jid, chatbox, chatboxview, trimmedview; var i, $el, jid, chatbox, chatboxview, trimmedview;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
var trimmed_chatboxes = converse.minimized_chats; var trimmed_chatboxes = converse.minimized_chats;
expect(this.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
spyOn(this.chatboxviews, 'trimChats'); spyOn(converse.chatboxviews, 'trimChats');
spyOn(trimmed_chatboxes, 'addChat').andCallThrough(); spyOn(trimmed_chatboxes, 'addChat').andCallThrough();
spyOn(trimmed_chatboxes, 'removeChat').andCallThrough(); spyOn(trimmed_chatboxes, 'removeChat').andCallThrough();
expect($("#conversejs .chatbox").length).toBe(1); // Controlbox is open expect($("#conversejs .chatbox").length).toBe(1); // Controlbox is open
...@@ -64,47 +65,51 @@ ...@@ -64,47 +65,51 @@
// Test that they can be trimmed // Test that they can be trimmed
runs(function () { runs(function () {
converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced. converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
}.bind(this)); });
waits(50); waits(50);
runs(function () { runs(function () {
// Test that they can be maximized again // Test that they can be maximized again
var online_contacts = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact').find('a.open-chat'); var online_contacts = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact').find('a.open-chat');
for (i=0; i<online_contacts.length; i++) { for (i=0; i<online_contacts.length; i++) {
$el = $(online_contacts[i]); $el = $(online_contacts[i]);
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
$el.click(); $el.click();
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(converse.chatboxviews.trimChats).toHaveBeenCalled();
chatboxview = this.chatboxviews.get(jid); chatboxview = converse.chatboxviews.get(jid);
spyOn(chatboxview, 'minimize').andCallThrough(); spyOn(chatboxview, 'minimize').andCallThrough();
chatboxview.model.set({'minimized': true}); chatboxview.model.set({'minimized': true});
expect(trimmed_chatboxes.addChat).toHaveBeenCalled(); expect(trimmed_chatboxes.addChat).toHaveBeenCalled();
expect(chatboxview.minimize).toHaveBeenCalled(); expect(chatboxview.minimize).toHaveBeenCalled();
trimmedview = trimmed_chatboxes.get(jid); trimmedview = trimmed_chatboxes.get(jid);
} }
var key = this.chatboxviews.keys()[1]; var key = converse.chatboxviews.keys()[1];
trimmedview = trimmed_chatboxes.get(key); trimmedview = trimmed_chatboxes.get(key);
chatbox = trimmedview.model; chatbox = trimmedview.model;
spyOn(chatbox, 'maximize').andCallThrough(); spyOn(chatbox, 'maximize').andCallThrough();
spyOn(trimmedview, 'restore').andCallThrough(); spyOn(trimmedview, 'restore').andCallThrough();
trimmedview.delegateEvents(); trimmedview.delegateEvents();
trimmedview.$("a.restore-chat").click(); trimmedview.$("a.restore-chat").click();
}.bind(this)); });
waits(250); waits(250);
runs(function () { runs(function () {
expect(trimmedview.restore).toHaveBeenCalled(); expect(trimmedview.restore).toHaveBeenCalled();
expect(chatbox.maximize).toHaveBeenCalled(); expect(chatbox.maximize).toHaveBeenCalled();
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(converse.chatboxviews.trimChats).toHaveBeenCalled();
}.bind(this)); });
}.bind(converse)); }));
it("is focused if its already open and you click on its corresponding roster item", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is focused if its already open and you click on its corresponding roster item", function () {
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
var $el, jid, chatboxview, chatbox; var $el, jid, chatboxview, chatbox;
// openControlBox was called earlier, so the controlbox is // openControlBox was called earlier, so the controlbox is
// visible, but no other chat boxes have been created. // visible, but no other chat boxes have been created.
expect(converse.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
chatbox = test_utils.openChatBoxFor(contact_jid); chatbox = test_utils.openChatBoxFor(converse, contact_jid);
chatboxview = converse.chatboxviews.get(contact_jid); chatboxview = converse.chatboxviews.get(contact_jid);
spyOn(chatboxview, 'focus'); spyOn(chatboxview, 'focus');
// Test that they can be trimmed // Test that they can be trimmed
...@@ -119,21 +124,25 @@ ...@@ -119,21 +124,25 @@
expect(converse.chatboxes.length).toEqual(2); expect(converse.chatboxes.length).toEqual(2);
expect(chatboxview.focus).toHaveBeenCalled(); expect(chatboxview.focus).toHaveBeenCalled();
}); });
}); }));
it("can be saved to, and retrieved from, browserStorage", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be saved to, and retrieved from, browserStorage", function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.chatboxviews, 'trimChats'); spyOn(converse.chatboxviews, 'trimChats');
runs(function () { runs(function () {
test_utils.openControlBox(); test_utils.openControlBox();
}); });
waits(250); waits(250);
runs(function () { runs(function () {
test_utils.openChatBoxes(6); test_utils.openChatBoxes(converse, 6);
expect(this.chatboxviews.trimChats).toHaveBeenCalled(); expect(converse.chatboxviews.trimChats).toHaveBeenCalled();
// We instantiate a new ChatBoxes collection, which by default // We instantiate a new ChatBoxes collection, which by default
// will be empty. // will be empty.
var newchatboxes = new this.ChatBoxes(); var newchatboxes = new converse.ChatBoxes();
expect(newchatboxes.length).toEqual(0); expect(newchatboxes.length).toEqual(0);
// The chatboxes will then be fetched from browserStorage inside the // The chatboxes will then be fetched from browserStorage inside the
// onConnected method // onConnected method
...@@ -145,17 +154,21 @@ ...@@ -145,17 +154,21 @@
var new_attrs, old_attrs; var new_attrs, old_attrs;
for (var i=0; i<attrs.length; i++) { for (var i=0; i<attrs.length; i++) {
new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]); new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]); old_attrs = _.pluck(_.pluck(converse.chatboxes.models, 'attributes'), attrs[i]);
expect(_.isEqual(new_attrs, old_attrs)).toEqual(true); expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
} }
this.rosterview.render(); converse.rosterview.render();
}.bind(converse)); });
}.bind(converse)); }));
it("can be closed by clicking a DOM element with class 'close-chatbox-button'", function () { it("can be closed by clicking a DOM element with class 'close-chatbox-button'", mock.initConverse(function (converse) {
var chatbox = test_utils.openChatBoxes(1)[0], test_utils.createContacts(converse, 'current');
controlview = this.chatboxviews.get('controlbox'), // The controlbox is currently open test_utils.openControlBox();
chatview = this.chatboxviews.get(chatbox.get('jid')); test_utils.openContactsPanel(converse);
var chatbox = test_utils.openChatBoxes(converse, 1)[0],
controlview = converse.chatboxviews.get('controlbox'), // The controlbox is currently open
chatview = converse.chatboxviews.get(chatbox.get('jid'));
spyOn(chatview, 'close').andCallThrough(); spyOn(chatview, 'close').andCallThrough();
spyOn(controlview, 'close').andCallThrough(); spyOn(controlview, 'close').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -180,12 +193,16 @@ ...@@ -180,12 +193,16 @@
expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
expect(converse.emit.callCount, 2); expect(converse.emit.callCount, 2);
}); });
}.bind(converse)); }));
it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", function () { var chatbox = test_utils.openChatBoxes(converse, 1)[0],
var chatbox = test_utils.openChatBoxes(1)[0], chatview = converse.chatboxviews.get(chatbox.get('jid')),
chatview = this.chatboxviews.get(chatbox.get('jid')), trimmed_chatboxes = converse.minimized_chats,
trimmed_chatboxes = this.minimized_chats,
trimmedview; trimmedview;
spyOn(chatview, 'minimize').andCallThrough(); spyOn(chatview, 'minimize').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -217,12 +234,16 @@ ...@@ -217,12 +234,16 @@
expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeFalsy(); expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeFalsy();
expect(chatview.model.get('minimized')).toBeFalsy(); expect(chatview.model.get('minimized')).toBeFalsy();
}); });
}.bind(converse)); }));
it("will be removed from browserStorage when closed", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will be removed from browserStorage when closed", function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(converse.chatboxviews, 'trimChats'); spyOn(converse.chatboxviews, 'trimChats');
this.chatboxes.browserStorage._clear(); converse.chatboxes.browserStorage._clear();
runs(function () { runs(function () {
test_utils.closeControlBox(); test_utils.closeControlBox();
}); });
...@@ -231,18 +252,18 @@ ...@@ -231,18 +252,18 @@
expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
expect(converse.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']); expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']);
test_utils.openChatBoxes(6); test_utils.openChatBoxes(converse, 6);
expect(converse.chatboxviews.trimChats).toHaveBeenCalled(); expect(converse.chatboxviews.trimChats).toHaveBeenCalled();
expect(converse.chatboxes.length).toEqual(7); expect(converse.chatboxes.length).toEqual(7);
expect(converse.emit).toHaveBeenCalledWith('chatBoxOpened', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxOpened', jasmine.any(Object));
test_utils.closeAllChatBoxes(); test_utils.closeAllChatBoxes(converse);
}); });
waits(50); waits(50);
runs(function () { runs(function () {
expect(converse.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']); expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']);
expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
var newchatboxes = new this.ChatBoxes(); var newchatboxes = new converse.ChatBoxes();
expect(newchatboxes.length).toEqual(0); expect(newchatboxes.length).toEqual(0);
expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']); expect(converse.chatboxes.pluck('id')).toEqual(['controlbox']);
// onConnected will fetch chatboxes in browserStorage, but // onConnected will fetch chatboxes in browserStorage, but
...@@ -251,28 +272,42 @@ ...@@ -251,28 +272,42 @@
newchatboxes.onConnected(); newchatboxes.onConnected();
expect(newchatboxes.length).toEqual(1); expect(newchatboxes.length).toEqual(1);
expect(newchatboxes.models[0].id).toBe("controlbox"); expect(newchatboxes.models[0].id).toBe("controlbox");
}.bind(converse)); });
}.bind(converse)); }));
describe("A chat toolbar", function () { describe("A chat toolbar", function () {
it("can be found on each chat box", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("can be found on each chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var chatbox = this.chatboxes.get(contact_jid); var chatbox = converse.chatboxes.get(contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
expect(chatbox).toBeDefined(); expect(chatbox).toBeDefined();
expect(view).toBeDefined(); expect(view).toBeDefined();
var $toolbar = view.$el.find('ul.chat-toolbar'); var $toolbar = view.$el.find('ul.chat-toolbar');
expect($toolbar.length).toBe(1); expect($toolbar.length).toBe(1);
expect($toolbar.children('li').length).toBe(3); expect($toolbar.children('li').length).toBe(3);
}.bind(converse)); }));
it("contains a button for inserting emoticons", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("contains a button for inserting emoticons", function () {
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost', var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost',
view, $toolbar, $textarea; view, $toolbar, $textarea;
waits(300); // ChatBox.show() is debounced for 250ms waits(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = converse.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
$textarea = view.$el.find('textarea.chat-textarea'); $textarea = view.$el.find('textarea.chat-textarea');
...@@ -324,13 +359,17 @@ ...@@ -324,13 +359,17 @@
expect(view.$el.find('.toggle-smiley ul').is(':visible')).toBeFalsy(); expect(view.$el.find('.toggle-smiley ul').is(':visible')).toBeFalsy();
expect($textarea.val()).toBe(':) <3 '); expect($textarea.val()).toBe(':) <3 ');
}); });
}); }));
it("contains a button for starting an encrypted chat session", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("contains a button for starting an encrypted chat session", function () {
// TODO: More tests can be added here... // TODO: More tests can be added here...
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
var $toolbar = view.$el.find('ul.chat-toolbar'); var $toolbar = view.$el.find('ul.chat-toolbar');
expect($toolbar.children('li.toggle-otr').length).toBe(1); expect($toolbar.children('li.toggle-otr').length).toBe(1);
// Register spies // Register spies
...@@ -348,17 +387,21 @@ ...@@ -348,17 +387,21 @@
expect($menu.children('li').length).toBe(2); expect($menu.children('li').length).toBe(2);
}); });
}.bind(converse)); }));
it("can contain a button for starting a call", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can contain a button for starting a call", function () {
var view, callButton, $toolbar; var view, callButton, $toolbar;
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
spyOn(converse, 'emit'); spyOn(converse, 'emit');
// First check that the button doesn't show if it's not enabled // First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons" // via "visible_toolbar_buttons"
converse.visible_toolbar_buttons.call = false; converse.visible_toolbar_buttons.call = false;
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
callButton = $toolbar.find('.toggle-call'); callButton = $toolbar.find('.toggle-call');
expect(callButton.length).toBe(0); expect(callButton.length).toBe(0);
...@@ -366,24 +409,28 @@ ...@@ -366,24 +409,28 @@
// Now check that it's shown if enabled and that it emits // Now check that it's shown if enabled and that it emits
// callButtonClicked // callButtonClicked
converse.visible_toolbar_buttons.call = true; // enable the button converse.visible_toolbar_buttons.call = true; // enable the button
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
callButton = $toolbar.find('.toggle-call'); callButton = $toolbar.find('.toggle-call');
expect(callButton.length).toBe(1); expect(callButton.length).toBe(1);
callButton.click(); callButton.click();
expect(converse.emit).toHaveBeenCalledWith('callButtonClicked', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('callButtonClicked', jasmine.any(Object));
}.bind(converse)); }));
it("can contain a button for clearing messages", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can contain a button for clearing messages", function () {
var view, clearButton, $toolbar; var view, clearButton, $toolbar;
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
// First check that the button doesn't show if it's not enabled // First check that the button doesn't show if it's not enabled
// via "visible_toolbar_buttons" // via "visible_toolbar_buttons"
converse.visible_toolbar_buttons.clear = false; converse.visible_toolbar_buttons.clear = false;
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
clearButton = $toolbar.find('.toggle-clear'); clearButton = $toolbar.find('.toggle-clear');
expect(clearButton.length).toBe(0); expect(clearButton.length).toBe(0);
...@@ -391,8 +438,8 @@ ...@@ -391,8 +438,8 @@
// Now check that it's shown if enabled and that it calls // Now check that it's shown if enabled and that it calls
// clearMessages // clearMessages
converse.visible_toolbar_buttons.clear = true; // enable the button converse.visible_toolbar_buttons.clear = true; // enable the button
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
$toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
clearButton = $toolbar.find('.toggle-clear'); clearButton = $toolbar.find('.toggle-clear');
expect(clearButton.length).toBe(1); expect(clearButton.length).toBe(1);
...@@ -400,48 +447,46 @@ ...@@ -400,48 +447,46 @@
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
clearButton.click(); clearButton.click();
expect(view.clearMessages).toHaveBeenCalled(); expect(view.clearMessages).toHaveBeenCalled();
}.bind(converse)); }));
});
}.bind(converse));
describe("A Chat Message", function () { describe("A Chat Message", function () {
afterEach(function () {
beforeEach(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.removeControlBox(); test_utils.clearBrowserStorage();
converse.roster.browserStorage._clear();
test_utils.initConverse();
test_utils.createContacts('current');
test_utils.openControlBox();
test_utils.openContactsPanel();
}); });
describe("when received from someone else", function () { describe("when received from someone else", function () {
it("can be received which will open a chatbox and be displayed inside it", function () { it("can be received which will open a chatbox and be displayed inside it", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var message = 'This is a received message'; var message = 'converse is a received message';
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.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();
// We don't already have an open chatbox for this user // We don't already have an open chatbox for this user
expect(this.chatboxes.get(sender_jid)).not.toBeDefined(); expect(converse.chatboxes.get(sender_jid)).not.toBeDefined();
runs(function () { runs(function () {
// onMessage is a handler for received XMPP messages // onMessage is a handler for received XMPP messages
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
}.bind(converse)); });
waits(50); waits(50);
runs(function () { runs(function () {
// Check that the chatbox and its view now exist // Check that the chatbox and its view now exist
var chatbox = this.chatboxes.get(sender_jid); var chatbox = converse.chatboxes.get(sender_jid);
var chatboxview = this.chatboxviews.get(sender_jid); var chatboxview = converse.chatboxviews.get(sender_jid);
expect(chatbox).toBeDefined(); expect(chatbox).toBeDefined();
expect(chatboxview).toBeDefined(); expect(chatboxview).toBeDefined();
// Check that the message was received and check the message parameters // Check that the message was received and check the message parameters
...@@ -457,11 +502,21 @@ ...@@ -457,11 +502,21 @@
expect(msg_txt).toEqual(message); expect(msg_txt).toEqual(message);
var sender_txt = $chat_content.find('span.chat-msg-them').text(); var sender_txt = $chat_content.find('span.chat-msg-them').text();
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();
}.bind(converse)); });
}.bind(converse)); }));
describe("and for which then an error message is received from the server", function () { describe("and for which then an error message is received from the server", function () {
it("will have the error message displayed after itself", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("will have the error message displayed after itself", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
// TODO: what could still be done for error // TODO: what could still be done for error
// messages... if the <error> element has type // messages... if the <error> element has type
// "cancel", then we know the messages wasn't sent, // "cancel", then we know the messages wasn't sent,
...@@ -553,13 +608,17 @@ ...@@ -553,13 +608,17 @@
.t('Server-to-server connection failed: Connecting failed: connection timeout'); .t('Server-to-server connection failed: Connecting failed: connection timeout');
converse.connection._dataRecv(test_utils.createRequest(stanza)); converse.connection._dataRecv(test_utils.createRequest(stanza));
expect($chat_content.find('.chat-error').length).toEqual(1); expect($chat_content.find('.chat-error').length).toEqual(1);
}); }));
}); });
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", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
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';
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(sender_jid); test_utils.openChatBoxFor(converse, sender_jid);
var chatboxview = converse.chatboxviews.get(sender_jid); var chatboxview = converse.chatboxviews.get(sender_jid);
spyOn(chatboxview, 'scrollDown').andCallThrough(); spyOn(chatboxview, 'scrollDown').andCallThrough();
runs(function () { runs(function () {
...@@ -606,9 +665,13 @@ ...@@ -606,9 +665,13 @@
runs(function () { runs(function () {
expect(chatboxview.$('.new-msgs-indicator').is(':visible')).toBeFalsy(); expect(chatboxview.$('.new-msgs-indicator').is(':visible')).toBeFalsy();
}); });
}); }));
it("is ignored if it's intended for a different resource and filter_by_resource is set to true", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is ignored if it's intended for a different resource and filter_by_resource is set to true", function () {
// Send a message from a different resource // Send a message from a different resource
var message, sender_jid, msg; var message, sender_jid, msg;
spyOn(converse, 'log'); spyOn(converse, 'log');
...@@ -652,16 +715,20 @@ ...@@ -652,16 +715,20 @@
var msg_txt = $chat_content.find('.chat-message').find('.chat-msg-content').text(); var msg_txt = $chat_content.find('.chat-message').find('.chat-msg-content').text();
expect(msg_txt).toEqual(message); expect(msg_txt).toEqual(message);
}); });
}); }));
}); });
describe("when sent by the current user", function () { describe("when sent by the current user", function () {
it("will always cause the chat area to be scrolled down", function () { it("will always cause the chat area to be scrolled down", mock.initConverse(function (converse) {
// TODO // TODO
}));
}); });
});
it("is ignored if it's a malformed headline message", function () { it("is ignored if it's a malformed headline message", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
/* Ideally we wouldn't have to filter out headline /* Ideally we wouldn't have to filter out headline
* messages, but Prosody gives them the wrong 'type' :( * messages, but Prosody gives them the wrong 'type' :(
*/ */
...@@ -686,9 +753,13 @@ ...@@ -686,9 +753,13 @@
converse.log.restore(); converse.log.restore();
converse.chatboxes.getChatBox.restore(); converse.chatboxes.getChatBox.restore();
utils.isHeadlineMessage.restore(); utils.isHeadlineMessage.restore();
}); }));
it("can be a carbon message, as defined in XEP-0280", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be a carbon message, as defined in XEP-0280", function () {
// Send a message from a different resource // Send a message from a different resource
spyOn(converse, 'log'); spyOn(converse, 'log');
var msgtext = 'This is a carbon message'; var msgtext = 'This is a carbon message';
...@@ -727,9 +798,13 @@ ...@@ -727,9 +798,13 @@
expect(msg_txt).toEqual(msgtext); expect(msg_txt).toEqual(msgtext);
var sender_txt = $chat_content.find('span.chat-msg-them').text(); var sender_txt = $chat_content.find('span.chat-msg-them').text();
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();
}); }));
it("can be a carbon message that this user sent from a different client, as defined in XEP-0280", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be a carbon message that this user sent from a different client, as defined in XEP-0280", function () {
// Send a message from a different resource // Send a message from a different resource
spyOn(converse, 'log'); spyOn(converse, 'log');
var msgtext = 'This is a sent carbon message'; var msgtext = 'This is a sent carbon message';
...@@ -766,14 +841,18 @@ ...@@ -766,14 +841,18 @@
var $chat_content = chatboxview.$el.find('.chat-content'); var $chat_content = chatboxview.$el.find('.chat-content');
var msg_txt = $chat_content.find('.chat-message').find('.chat-msg-content').text(); var msg_txt = $chat_content.find('.chat-message').find('.chat-msg-content').text();
expect(msg_txt).toEqual(msgtext); expect(msg_txt).toEqual(msgtext);
}); }));
it("received for a minimized chat box will increment a counter on its header", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("received for a minimized chat box will increment a counter on its header", function () {
var contact_name = mock.cur_names[0]; var contact_name = mock.cur_names[0];
var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost';
spyOn(this, 'emit').andCallThrough(); spyOn(converse, 'emit').andCallThrough();
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var chatview = this.chatboxviews.get(contact_jid); var chatview = converse.chatboxviews.get(contact_jid);
expect(chatview.$el.is(':visible')).toBeTruthy(); expect(chatview.$el.is(':visible')).toBeTruthy();
expect(chatview.model.get('minimized')).toBeFalsy(); expect(chatview.model.get('minimized')).toBeFalsy();
chatview.$el.find('.toggle-chatbox-button').click(); chatview.$el.find('.toggle-chatbox-button').click();
...@@ -782,24 +861,24 @@ ...@@ -782,24 +861,24 @@
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.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.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(this.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
var trimmed_chatboxes = this.minimized_chats; var trimmed_chatboxes = converse.minimized_chats;
var trimmedview = trimmed_chatboxes.get(contact_jid); var trimmedview = trimmed_chatboxes.get(contact_jid);
var $count = trimmedview.$el.find('.chat-head-message-count'); var $count = trimmedview.$el.find('.chat-head-message-count');
expect(chatview.$el.is(':visible')).toBeFalsy(); expect(chatview.$el.is(':visible')).toBeFalsy();
expect(trimmedview.model.get('minimized')).toBeTruthy(); expect(trimmedview.model.get('minimized')).toBeTruthy();
expect($count.is(':visible')).toBeTruthy(); expect($count.is(':visible')).toBeTruthy();
expect($count.html()).toBe('1'); expect($count.html()).toBe('1');
this.chatboxes.onMessage( converse.chatboxes.onMessage(
$msg({ $msg({
from: mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost', from: mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').t('This message is also sent to a minimized chatbox').up() }).c('body').t('This message is also sent to a minimized chatbox').up()
...@@ -812,19 +891,23 @@ ...@@ -812,19 +891,23 @@
expect($count.html()).toBe('2'); expect($count.html()).toBe('2');
trimmedview.$el.find('.restore-chat').click(); trimmedview.$el.find('.restore-chat').click();
expect(trimmed_chatboxes.keys().length).toBe(0); expect(trimmed_chatboxes.keys().length).toBe(0);
}.bind(converse)); }));
it("will indicate when it has a time difference of more than a day between it and its predecessor", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will indicate when it has a time difference of more than a day between it and its predecessor", function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var contact_name = mock.cur_names[1]; var contact_name = mock.cur_names[1];
var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
test_utils.clearChatBoxMessages(contact_jid); test_utils.clearChatBoxMessages(converse, contact_jid);
var one_day_ago = moment(); var one_day_ago = moment();
one_day_ago.subtract('days', 1); one_day_ago.subtract('days', 1);
var message = 'This is a day old message'; var message = 'This is a day old message';
var chatbox = this.chatboxes.get(contact_jid); var chatbox = converse.chatboxes.get(contact_jid);
var chatboxview = this.chatboxviews.get(contact_jid); var chatboxview = converse.chatboxviews.get(contact_jid);
var $chat_content = chatboxview.$el.find('.chat-content'); var $chat_content = chatboxview.$el.find('.chat-content');
var msg_obj; var msg_obj;
var msg_txt; var msg_txt;
...@@ -832,13 +915,13 @@ ...@@ -832,13 +915,13 @@
var msg = $msg({ var msg = $msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: one_day_ago.unix() id: one_day_ago.unix()
}).c('body').t(message).up() }).c('body').t(message).up()
.c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: one_day_ago.format() }) .c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: one_day_ago.format() })
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', 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];
...@@ -860,12 +943,12 @@ ...@@ -860,12 +943,12 @@
message = 'This is a current message'; message = 'This is a current message';
msg = $msg({ msg = $msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.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.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
// Check that there is a <time> element, with the required // Check that there is a <time> element, with the required
// props. // props.
...@@ -888,14 +971,18 @@ ...@@ -888,14 +971,18 @@
expect(msg_txt).toEqual(message); expect(msg_txt).toEqual(message);
sender_txt = $chat_content.find('span.chat-msg-them').last().text(); sender_txt = $chat_content.find('span.chat-msg-them').last().text();
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();
}.bind(converse)); }));
it("can be sent from a chatbox, and will appear inside it", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can be sent from a chatbox, and will appear inside it", function () {
spyOn(converse, 'emit'); 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';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
expect(converse.emit).toHaveBeenCalledWith('chatBoxFocused', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxFocused', jasmine.any(Object));
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(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();
test_utils.sendMessage(view, message); test_utils.sendMessage(view, message);
...@@ -903,12 +990,16 @@ ...@@ -903,12 +990,16 @@
expect(view.model.messages.length, 2); expect(view.model.messages.length, 2);
expect(converse.emit.mostRecentCall.args, ['messageSend', message]); expect(converse.emit.mostRecentCall.args, ['messageSend', message]);
expect(view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content').text()).toEqual(message); expect(view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content').text()).toEqual(message);
}.bind(converse)); }));
it("is sanitized to prevent Javascript injection attacks", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is sanitized to prevent Javascript injection attacks", function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
var message = '<p>This message contains <em>some</em> <b>markup</b></p>'; var message = '<p>This message contains <em>some</em> <b>markup</b></p>';
spyOn(view, 'sendMessage').andCallThrough(); spyOn(view, 'sendMessage').andCallThrough();
test_utils.sendMessage(view, message); test_utils.sendMessage(view, message);
...@@ -916,12 +1007,16 @@ ...@@ -916,12 +1007,16 @@
var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content'); var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content');
expect(msg.text()).toEqual(message); expect(msg.text()).toEqual(message);
expect(msg.html()).toEqual('&lt;p&gt;This message contains &lt;em&gt;some&lt;/em&gt; &lt;b&gt;markup&lt;/b&gt;&lt;/p&gt;'); expect(msg.html()).toEqual('&lt;p&gt;This message contains &lt;em&gt;some&lt;/em&gt; &lt;b&gt;markup&lt;/b&gt;&lt;/p&gt;');
}.bind(converse)); }));
it("should display emoticons correctly", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("should display emoticons correctly", function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
var messages = [':)', ';)', ':D', ':P', '8)', '>:)', ':S', ':\\', '>:(', ':(', ':O', '(^.^)b', '<3']; var messages = [':)', ';)', ':D', ':P', '8)', '>:)', ':S', ':\\', '>:(', ':(', ':O', '(^.^)b', '<3'];
var emoticons = [ var emoticons = [
'<span class="emoticon icon-smiley"></span>', '<span class="emoticon icon-wink"></span>', '<span class="emoticon icon-smiley"></span>', '<span class="emoticon icon-wink"></span>',
...@@ -940,11 +1035,15 @@ ...@@ -940,11 +1035,15 @@
var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content'); var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content');
expect(msg.html()).toEqual(emoticons[i]); expect(msg.html()).toEqual(emoticons[i]);
} }
}.bind(converse)); }));
it("can contain hyperlinks, which will be clickable", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("can contain hyperlinks, which will be clickable", function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
var message = 'This message contains a hyperlink: www.opkode.com'; var message = 'This message contains a hyperlink: www.opkode.com';
spyOn(view, 'sendMessage').andCallThrough(); spyOn(view, 'sendMessage').andCallThrough();
...@@ -958,12 +1057,16 @@ ...@@ -958,12 +1057,16 @@
expect(msg.text()).toEqual(message); expect(msg.text()).toEqual(message);
expect(msg.html()).toEqual('This message contains a hyperlink: <a target="_blank" rel="noopener" href="http://www.opkode.com">www.opkode.com</a>'); expect(msg.html()).toEqual('This message contains a hyperlink: <a target="_blank" rel="noopener" href="http://www.opkode.com">www.opkode.com</a>');
}); });
}); }));
it("will have properly escaped URLs", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will have properly escaped URLs", function () {
var message, msg; var message, msg;
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
spyOn(view, 'sendMessage').andCallThrough(); spyOn(view, 'sendMessage').andCallThrough();
runs(function () { runs(function () {
...@@ -1007,9 +1110,13 @@ ...@@ -1007,9 +1110,13 @@
expect(msg.text()).toEqual(message); expect(msg.text()).toEqual(message);
expect(msg.html()).toEqual('<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender%27s_Game</a>'); expect(msg.html()).toEqual('<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender%27s_Game</a>');
}); });
}); }));
it("will render images from their URLs", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will render images from their URLs", function () {
if (/PhantomJS/.test(window.navigator.userAgent)) { if (/PhantomJS/.test(window.navigator.userAgent)) {
// Doesn't work when running tests in PhantomJS, since // Doesn't work when running tests in PhantomJS, since
// the page is loaded via file:/// // the page is loaded via file:///
...@@ -1017,7 +1124,7 @@ ...@@ -1017,7 +1124,7 @@
} }
var message = document.URL.split(window.location.pathname)[0] + "/logo/conversejs.svg"; var message = document.URL.split(window.location.pathname)[0] + "/logo/conversejs.svg";
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
spyOn(view, 'sendMessage').andCallThrough(); spyOn(view, 'sendMessage').andCallThrough();
runs(function () { runs(function () {
...@@ -1029,13 +1136,22 @@ ...@@ -1029,13 +1136,22 @@
var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content'); var msg = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-msg-content');
expect(msg.html()).toEqual('<img src="'+message+'" class="chat-image">'); expect(msg.html()).toEqual('<img src="'+message+'" class="chat-image">');
}); });
}); }));
}.bind(converse)); });
describe("A Chat Status Notification", function () { describe("A Chat Status Notification", function () {
afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("does not open automatically if a chat state notification is received", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("does not open automatically if a chat state notification is received", function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
// <composing> state // <composing> state
...@@ -1047,15 +1163,25 @@ ...@@ -1047,15 +1163,25 @@
}).c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree(); }).c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
converse.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
}); }));
describe("An active notification", function () { describe("An active notification", function () {
it("is sent when the user opens a chat box", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("is sent when the user opens a chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
waits(300); // ChatBox.show() is debounced for 250ms waits(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
expect(converse.connection.send).toHaveBeenCalled(); expect(converse.connection.send).toHaveBeenCalled();
...@@ -1066,12 +1192,16 @@ ...@@ -1066,12 +1192,16 @@
expect($stanza.children().get(1).tagName).toBe('no-store'); expect($stanza.children().get(1).tagName).toBe('no-store');
expect($stanza.children().get(2).tagName).toBe('no-permanent-store'); expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
}); });
}); }));
it("is sent when the user maximizes a minimized a chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is sent when the user maximizes a minimized a chat box", function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
view.minimize(); view.minimize();
expect(view.model.get('chat_state')).toBe('inactive'); expect(view.model.get('chat_state')).toBe('inactive');
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
...@@ -1084,15 +1214,25 @@ ...@@ -1084,15 +1214,25 @@
expect($stanza.children().get(0).tagName).toBe('active'); expect($stanza.children().get(0).tagName).toBe('active');
expect($stanza.children().get(1).tagName).toBe('no-store'); expect($stanza.children().get(1).tagName).toBe('no-store');
expect($stanza.children().get(2).tagName).toBe('no-permanent-store'); expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
}.bind(converse)); }));
}.bind(converse)); });
describe("A composing notification", function () { describe("A composing notification", function () {
it("is sent as soon as the user starts typing a message which is not a command", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("is sent as soon as the user starts typing a message which is not a command", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
waits(300); // ChatBox.show() is debounced for 250ms waits(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
...@@ -1116,9 +1256,13 @@ ...@@ -1116,9 +1256,13 @@
expect(view.model.get('chat_state')).toBe('composing'); expect(view.model.get('chat_state')).toBe('composing');
expect(converse.emit.callCount, 1); expect(converse.emit.callCount, 1);
}); });
}); }));
it("will be shown if received", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will be shown if received", function () {
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
...@@ -1126,28 +1270,38 @@ ...@@ -1126,28 +1270,38 @@
// <composing> state // <composing> state
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree(); }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
var chatboxview = this.chatboxviews.get(sender_jid); var chatboxview = converse.chatboxviews.get(sender_jid);
expect(chatboxview).toBeDefined(); expect(chatboxview).toBeDefined();
// Check that the notification appears inside the chatbox in the DOM // Check that the notification appears inside the chatbox in the DOM
var $events = chatboxview.$el.find('.chat-event'); var $events = chatboxview.$el.find('.chat-event');
expect($events.text()).toEqual(mock.cur_names[1] + ' is typing'); expect($events.text()).toEqual(mock.cur_names[1] + ' is typing');
}.bind(converse)); }));
}.bind(converse)); });
describe("A paused notification", function () { describe("A paused notification", function () {
it("is sent if the user has stopped typing since 30 seconds", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("is sent if the user has stopped typing since 30 seconds", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
var view, contact_jid; var view, contact_jid;
converse.TIMEOUTS.PAUSED = 200; // Make the timeout shorter so that we can test converse.TIMEOUTS.PAUSED = 200; // Make the timeout shorter so that we can test
waits(300); // ChatBox.show() is debounced for 250ms waits(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
view = converse.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
spyOn(view, 'setChatState').andCallThrough(); spyOn(view, 'setChatState').andCallThrough();
...@@ -1193,9 +1347,13 @@ ...@@ -1193,9 +1347,13 @@
runs(function () { runs(function () {
expect(view.model.get('chat_state')).toBe('composing'); expect(view.model.get('chat_state')).toBe('composing');
}); });
}); }));
it("will be shown if received", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will be shown if received", function () {
// TODO: only show paused state if the previous state was composing // TODO: only show paused state if the previous state was composing
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -1203,26 +1361,36 @@ ...@@ -1203,26 +1361,36 @@
// <paused> state // <paused> state
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree(); }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
var chatboxview = this.chatboxviews.get(sender_jid); var chatboxview = converse.chatboxviews.get(sender_jid);
var $events = chatboxview.$el.find('.chat-event'); var $events = chatboxview.$el.find('.chat-event');
expect($events.text()).toEqual(mock.cur_names[1] + ' has stopped typing'); expect($events.text()).toEqual(mock.cur_names[1] + ' has stopped typing');
}.bind(converse)); }));
}.bind(converse)); });
describe("An inactive notifciation", function () { describe("An inactive notifciation", function () {
it("is sent if the user has stopped typing since 2 minutes", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("is sent if the user has stopped typing since 2 minutes", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
// Make the timeouts shorter so that we can test // Make the timeouts shorter so that we can test
this.TIMEOUTS.PAUSED = 200; converse.TIMEOUTS.PAUSED = 200;
this.TIMEOUTS.INACTIVE = 200; converse.TIMEOUTS.INACTIVE = 200;
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
runs(function () { runs(function () {
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
view.keyPressed({ view.keyPressed({
...@@ -1248,12 +1416,16 @@ ...@@ -1248,12 +1416,16 @@
expect($stanza.children().get(2).tagName).toBe('no-permanent-store'); expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
}); });
}.bind(converse)); }));
it("is sent when the user a minimizes a chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is sent when the user a minimizes a chat box", function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
view.minimize(); view.minimize();
expect(view.model.get('chat_state')).toBe('inactive'); expect(view.model.get('chat_state')).toBe('inactive');
...@@ -1261,13 +1433,17 @@ ...@@ -1261,13 +1433,17 @@
var $stanza = $(converse.connection.send.argsForCall[0][0].tree()); var $stanza = $(converse.connection.send.argsForCall[0][0].tree());
expect($stanza.attr('to')).toBe(contact_jid); expect($stanza.attr('to')).toBe(contact_jid);
expect($stanza.children().get(0).tagName).toBe('inactive'); expect($stanza.children().get(0).tagName).toBe('inactive');
}.bind(converse)); }));
it("is sent if the user closes a chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is sent if the user closes a chat box", function () {
waits(300); // ChatBox.show() is debounced for 250ms waits(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = converse.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
...@@ -1281,67 +1457,81 @@ ...@@ -1281,67 +1457,81 @@
expect($stanza.children().get(1).tagName).toBe('no-store'); expect($stanza.children().get(1).tagName).toBe('no-store');
expect($stanza.children().get(2).tagName).toBe('no-permanent-store'); expect($stanza.children().get(2).tagName).toBe('no-permanent-store');
}); });
}); }));
it("will clear any other chat status notifications if its received", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("will clear any other chat status notifications if its received", function () {
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(sender_jid); test_utils.openChatBoxFor(converse, sender_jid);
var view = this.chatboxviews.get(sender_jid); var view = converse.chatboxviews.get(sender_jid);
expect(view.$el.find('.chat-event').length).toBe(0); expect(view.$el.find('.chat-event').length).toBe(0);
view.showStatusNotification(sender_jid+' '+'is typing'); view.showStatusNotification(sender_jid+' '+'is typing');
expect(view.$el.find('.chat-event').length).toBe(1); expect(view.$el.find('.chat-event').length).toBe(1);
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree(); }).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
expect(view.$el.find('.chat-event').length).toBe(0); expect(view.$el.find('.chat-event').length).toBe(0);
}.bind(converse)); }));
}.bind(converse)); });
describe("A gone notifciation", function () { describe("A gone notifciation", function () {
it("will be shown if received", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("will be shown if received", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
// <paused> state // <paused> state
var msg = $msg({ var msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').c('gone', {'xmlns': Strophe.NS.CHATSTATES}).tree(); }).c('body').c('gone', {'xmlns': Strophe.NS.CHATSTATES}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
var chatboxview = this.chatboxviews.get(sender_jid); var chatboxview = converse.chatboxviews.get(sender_jid);
var $events = chatboxview.$el.find('.chat-event'); var $events = chatboxview.$el.find('.chat-event');
expect($events.text()).toEqual(mock.cur_names[1] + ' has gone away'); expect($events.text()).toEqual(mock.cur_names[1] + ' has gone away');
}.bind(converse)); }));
}.bind(converse)); });
}.bind(converse)); });
}.bind(converse)); });
describe("Special Messages", function () { describe("Special Messages", function () {
beforeEach(function () { afterEach(function () {
test_utils.closeAllChatBoxes(); converse_api.user.logout();
test_utils.removeControlBox(); converse_api.listen.not();
converse.roster.browserStorage._clear(); test_utils.clearBrowserStorage();
test_utils.initConverse();
test_utils.createContacts('current');
test_utils.openControlBox();
test_utils.openContactsPanel();
}); });
it("'/clear' can be used to clear messages in a conversation", function () { it("'/clear' can be used to clear messages in a conversation", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
spyOn(converse, 'emit'); 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';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var view = this.chatboxviews.get(contact_jid); var view = converse.chatboxviews.get(contact_jid);
var message = 'This message is another sent from this chatbox'; var message = 'This message is another sent from this chatbox';
// Lets make sure there is at least one message already // Lets make sure there is at least one message already
// (e.g for when this test is run on its own). // (e.g for when this test is run on its own).
...@@ -1364,44 +1554,47 @@ ...@@ -1364,44 +1554,47 @@
expect(view.model.messages.browserStorage.records.length, 0); // And also from browserStorage expect(view.model.messages.browserStorage.records.length, 0); // And also from browserStorage
expect(converse.emit.callCount, 1); expect(converse.emit.callCount, 1);
expect(converse.emit.mostRecentCall.args, ['messageSend', message]); expect(converse.emit.mostRecentCall.args, ['messageSend', message]);
}.bind(converse)); }));
}.bind(converse)); });
describe("A Message Counter", function () { describe("A Message Counter", function () {
beforeEach(function () { afterEach(function () {
converse.clearMsgCounter(); converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.removeControlBox(); test_utils.clearBrowserStorage();
converse.roster.browserStorage._clear();
test_utils.initConverse();
test_utils.createContacts('current');
test_utils.openControlBox();
test_utils.openContactsPanel();
}); });
it("is incremented when the message is received and the window is not focused", function () { it("is incremented when the message is received and the window is not focused", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
spyOn(converse, 'emit'); spyOn(converse, 'emit');
expect(this.msg_counter).toBe(0); expect(converse.msg_counter).toBe(0);
spyOn(converse, 'incrementMsgCounter').andCallThrough(); spyOn(converse, 'incrementMsgCounter').andCallThrough();
var previous_state = converse.windowState; var previous_state = converse.windowState;
var message = 'This message will increment the message counter'; var message = 'This message will increment the message counter';
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost', var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
msg = $msg({ msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.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();
converse.windowState = 'hidden'; converse.windowState = 'hidden';
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).toHaveBeenCalled(); expect(converse.incrementMsgCounter).toHaveBeenCalled();
expect(this.msg_counter).toBe(1); expect(converse.msg_counter).toBe(1);
expect(converse.emit).toHaveBeenCalledWith('message', msg); expect(converse.emit).toHaveBeenCalledWith('message', msg);
converse.windowSate = previous_state; converse.windowSate = previous_state;
}.bind(converse)); }));
it("is cleared when the window is focused", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
it("is cleared when the window is focused", function () {
converse.windowState = 'hidden'; converse.windowState = 'hidden';
spyOn(converse, 'clearMsgCounter').andCallThrough(); spyOn(converse, 'clearMsgCounter').andCallThrough();
runs(function () { runs(function () {
...@@ -1412,25 +1605,29 @@ ...@@ -1412,25 +1605,29 @@
runs(function () { runs(function () {
expect(converse.clearMsgCounter).toHaveBeenCalled(); expect(converse.clearMsgCounter).toHaveBeenCalled();
}); });
}.bind(converse)); }));
it("is not incremented when the message is received and the window is focused", function () { it("is not incremented when the message is received and the window is focused", mock.initConverse(function (converse) {
expect(this.msg_counter).toBe(0); test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
expect(converse.msg_counter).toBe(0);
spyOn(converse, 'incrementMsgCounter').andCallThrough(); spyOn(converse, 'incrementMsgCounter').andCallThrough();
converse.saveWindowState(null, 'focus'); converse.saveWindowState(null, 'focus');
var message = 'This message will not increment the message counter'; var message = 'This message will not increment the message counter';
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost', var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
msg = $msg({ msg = $msg({
from: sender_jid, from: sender_jid,
to: this.connection.jid, to: converse.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.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(converse.incrementMsgCounter).not.toHaveBeenCalled(); expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
expect(this.msg_counter).toBe(0); expect(converse.msg_counter).toBe(0);
}.bind(converse)); }));
}.bind(converse)); });
}, converse, mock, test_utils)); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils", "utils" ], factory);
"jquery", } (this, function (mock, test_utils, utils) {
"underscore", var _ = converse_api.env._;
"mock", var $ = converse_api.env.jQuery;
"test_utils",
"utils"
], function ($, _, mock, test_utils, utils) {
return factory($, _, mock, test_utils, utils);
}
);
} (this, function ($, _, mock, test_utils, utils) {
var $pres = converse_api.env.$pres; var $pres = converse_api.env.$pres;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
var $msg = converse_api.env.$msg; var $msg = converse_api.env.$msg;
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
return describe("ChatRooms", function (mock, test_utils) { return describe("ChatRooms", function () {
beforeEach(function () {
runs(function () {
test_utils.closeAllChatBoxes();
test_utils.clearBrowserStorage();
});
});
describe("The \"rooms\" API", function () { describe("The \"rooms\" API", function () {
beforeEach(function () { afterEach(function () {
test_utils.closeAllChatBoxes(); converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
converse.rosterview.model.reset();
test_utils.createContacts('current');
}); });
it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", 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');
runs(function () { runs(function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
test_utils.openChatRoom('leisure', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'leisure', 'localhost', 'dummy');
test_utils.openChatRoom('news', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'news', 'localhost', 'dummy');
expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy(); expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
expect(converse.chatboxviews.get('leisure@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(); expect(converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
...@@ -52,8 +37,8 @@ ...@@ -52,8 +37,8 @@
expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined(); expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
expect(converse.chatboxviews.get('news@localhost')).toBeUndefined(); expect(converse.chatboxviews.get('news@localhost')).toBeUndefined();
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
test_utils.openChatRoom('leisure', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'leisure', 'localhost', 'dummy');
expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy(); expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy(); expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
}); });
...@@ -63,12 +48,13 @@ ...@@ -63,12 +48,13 @@
expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined(); expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined(); expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
}); });
}); }));
it("has a method 'get' which returns a wrapped chat room (if it exists)", function () { 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 waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
var jid = 'lounge@localhost'; var jid = 'lounge@localhost';
var room = converse_api.rooms.get(jid); var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
...@@ -80,7 +66,7 @@ ...@@ -80,7 +66,7 @@
waits('300'); // ChatBox.show() is debounced for 250ms waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
// Test with mixed case // Test with mixed case
test_utils.openChatRoom('Leisure', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'Leisure', 'localhost', 'dummy');
var jid = 'Leisure@localhost'; var jid = 'Leisure@localhost';
var room = converse_api.rooms.get(jid); var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
...@@ -107,9 +93,10 @@ ...@@ -107,9 +93,10 @@
room = converse_api.rooms.get(jid); room = converse_api.rooms.get(jid);
expect(typeof room === 'undefined').toBeTruthy(); expect(typeof room === 'undefined').toBeTruthy();
}); });
}); }));
it("has a method 'open' which opens and returns a wrapped chat box", function () { it("has a method 'open' which opens and returns a wrapped chat box", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
var chatroomview; var chatroomview;
var jid = 'lounge@localhost'; var jid = 'lounge@localhost';
var room = converse_api.rooms.open(jid); var room = converse_api.rooms.open(jid);
...@@ -151,27 +138,25 @@ ...@@ -151,27 +138,25 @@
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
chatroomview.close(); chatroomview.close();
}); });
}));
}); });
});
describe("A Chat Room", function () { describe("A Chat Room", function () {
beforeEach(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
}); });
});
it("can have spaces and special characters in its name", function () { it("can have spaces and special characters in its name", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge & leisure', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge & leisure', 'localhost', 'dummy');
var view = converse.chatboxviews.get( var view = converse.chatboxviews.get(
Strophe.escapeNode('lounge & leisure')+'@localhost'); Strophe.escapeNode('lounge & leisure')+'@localhost');
expect(view instanceof converse.ChatRoomView).toBe(true); expect(view instanceof converse.ChatRoomView).toBe(true);
}); }));
it("shows users currently present in the room", function () { it("shows users currently present in the room", mock.initConverse(function (converse) {
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom(converse, 'lounge', 'localhost', 'dummy');
var name; var name;
var view = converse.chatboxviews.get('lounge@localhost'), var view = converse.chatboxviews.get('lounge@localhost'),
$occupants = view.$('.occupant-list'); $occupants = view.$('.occupant-list');
...@@ -215,10 +200,10 @@ ...@@ -215,10 +200,10 @@
converse.connection._dataRecv(test_utils.createRequest(presence)); converse.connection._dataRecv(test_utils.createRequest(presence));
expect($occupants.find('li').length).toBe(i+1); expect($occupants.find('li').length).toBe(i+1);
} }
}); }));
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", mock.initConverse(function (converse) {
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom(converse, 'lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
var presence = $pres({ var presence = $pres({
...@@ -239,9 +224,9 @@ ...@@ -239,9 +224,9 @@
expect($(occupant).last().text()).toBe("moderatorman"); expect($(occupant).last().text()).toBe("moderatorman");
expect($(occupant).last().attr('class').indexOf('moderator')).not.toBe(-1); expect($(occupant).last().attr('class').indexOf('moderator')).not.toBe(-1);
expect($(occupant).last().attr('title')).toBe('This user is a moderator. Click to mention this user in your message.'); expect($(occupant).last().attr('title')).toBe('This user is a moderator. Click to mention this user in your message.');
}); }));
it("will use the user's reserved nickname, if it exists", function () { it("will use the user's reserved nickname, if it exists", mock.initConverse(function (converse) {
var sent_IQ, IQ_id; var sent_IQ, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -249,7 +234,7 @@ ...@@ -249,7 +234,7 @@
IQ_id = sendIQ.bind(this)(iq, callback, errback); IQ_id = sendIQ.bind(this)(iq, callback, errback);
}); });
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'join').andCallThrough(); spyOn(view, 'join').andCallThrough();
...@@ -310,19 +295,19 @@ ...@@ -310,19 +295,19 @@
converse.connection._dataRecv(test_utils.createRequest(presence)); converse.connection._dataRecv(test_utils.createRequest(presence));
var info_text = view.$el.find('.chat-content .chat-info').text(); var info_text = view.$el.find('.chat-content .chat-info').text();
expect(info_text).toBe('Your nickname has been automatically set to: thirdwitch'); expect(info_text).toBe('Your nickname has been automatically set to: thirdwitch');
}); }));
it("allows the user to invite their roster contacts to enter the chat room", function () { it("allows the user to invite their roster contacts to enter the chat room", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(window, 'prompt').andCallFake(function () { spyOn(window, 'prompt').andCallFake(function () {
return null; return null;
}); });
var $input; var $input;
var view = this.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
view.$el.find('.chat-area').remove(); view.$el.find('.chat-area').remove();
view.renderChatArea(); // Will init the widget view.renderChatArea(); // Will init the widget
test_utils.createContacts('current'); // We need roster contacts, so that we have someone to invite test_utils.createContacts(converse, 'current'); // We need roster contacts, so that we have someone to invite
$input = view.$el.find('input.invited-contact.tt-input'); $input = view.$el.find('input.invited-contact.tt-input');
var $hint = view.$el.find('input.invited-contact.tt-hint'); var $hint = view.$el.find('input.invited-contact.tt-hint');
runs (function () { runs (function () {
...@@ -340,15 +325,15 @@ ...@@ -340,15 +325,15 @@
$sugg.trigger('click'); $sugg.trigger('click');
expect(window.prompt).toHaveBeenCalled(); expect(window.prompt).toHaveBeenCalled();
}); });
}.bind(converse)); }));
it("can be joined automatically, based upon a received invite", function () { it("can be joined automatically, based upon a received invite", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
spyOn(window, 'confirm').andCallFake(function () { spyOn(window, 'confirm').andCallFake(function () {
return true; return true;
}); });
test_utils.createContacts('current'); // We need roster contacts, who can invite us test_utils.createContacts(converse, 'current'); // We need roster contacts, who can invite us
var view = this.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
view.close(); view.close();
var name = mock.cur_names[0]; var name = mock.cur_names[0];
var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
...@@ -370,12 +355,12 @@ ...@@ -370,12 +355,12 @@
expect(converse.chatboxes.models.length).toBe(2); expect(converse.chatboxes.models.length).toBe(2);
expect(converse.chatboxes.models[0].id).toBe('controlbox'); expect(converse.chatboxes.models[0].id).toBe('controlbox');
expect(converse.chatboxes.models[1].id).toBe(room_jid); expect(converse.chatboxes.models[1].id).toBe(room_jid);
}.bind(converse)); }));
it("shows received groupchat messages", function () { it("shows received groupchat messages", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
spyOn(converse, 'emit'); spyOn(converse, 'emit');
var view = this.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 nick = mock.chatroom_names[0]; var nick = mock.chatroom_names[0];
var text = 'This is a received message'; var text = 'This is a received message';
...@@ -390,10 +375,10 @@ ...@@ -390,10 +375,10 @@
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').text()).toBe(text); expect($chat_content.find('.chat-msg-content').text()).toBe(text);
expect(converse.emit).toHaveBeenCalledWith('message', message.nodeTree); expect(converse.emit).toHaveBeenCalledWith('message', message.nodeTree);
}.bind(converse)); }));
it("shows sent groupchat messages", function () { it("shows sent groupchat messages", mock.initConverse(function (converse) {
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom(converse, '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(); }
...@@ -417,11 +402,11 @@ ...@@ -417,11 +402,11 @@
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
expect(converse.emit.callCount, 1); expect(converse.emit.callCount, 1);
}); }));
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", mock.initConverse(function (converse) {
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.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom(converse, '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 () {
...@@ -461,10 +446,10 @@ ...@@ -461,10 +446,10 @@
expect(msg_txt).toEqual(message); expect(msg_txt).toEqual(message);
expect(view.$content.scrollTop()).toBe(0); expect(view.$content.scrollTop()).toBe(0);
}); });
}); }));
it("shows received chatroom subject messages", function () { it("shows received chatroom subject messages", mock.initConverse(function (converse) {
test_utils.openAndEnterChatRoom('jdev', 'conference.jabber.org', 'jc'); test_utils.openAndEnterChatRoom(converse, '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(
...@@ -478,9 +463,9 @@ ...@@ -478,9 +463,9 @@
var $chat_content = view.$el.find('.chat-content'); var $chat_content = view.$el.find('.chat-content');
expect($chat_content.find('.chat-info').length).toBe(1); expect($chat_content.find('.chat-info').length).toBe(1);
expect($chat_content.find('.chat-info').text()).toBe('Topic set by ralphm to: '+text); expect($chat_content.find('.chat-info').text()).toBe('Topic set by ralphm to: '+text);
}); }));
it("informs users if their nicknames has been changed.", function () { it("informs users if their nicknames has been changed.", mock.initConverse(function (converse) {
/* The service then sends two presence stanzas to the full JID /* The service then sends two presence stanzas to the full JID
* of each occupant (including the occupant who is changing his * of each occupant (including the occupant who is changing his
* or her room nickname), one of type "unavailable" for the old * or her room nickname), one of type "unavailable" for the old
...@@ -517,7 +502,7 @@ ...@@ -517,7 +502,7 @@
* </presence> * </presence>
*/ */
var __ = utils.__.bind(converse); var __ = utils.__.bind(converse);
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'oldnick'); test_utils.openAndEnterChatRoom(converse, 'lounge', 'localhost', 'oldnick');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
var $chat_content = view.$el.find('.chat-content'); var $chat_content = view.$el.find('.chat-content');
...@@ -588,9 +573,9 @@ ...@@ -588,9 +573,9 @@
$occupants = view.$('.occupant-list'); $occupants = view.$('.occupant-list');
expect($occupants.children().length).toBe(1); expect($occupants.children().length).toBe(1);
expect($occupants.children().first(0).text()).toBe("newnick"); expect($occupants.children().first(0).text()).toBe("newnick");
}); }));
it("informs users if they have been kicked out of the chat room", function () { it("informs users if they have been kicked out of the chat room", mock.initConverse(function (converse) {
/* <presence /* <presence
* from='harfleur@chat.shakespeare.lit/pistol' * from='harfleur@chat.shakespeare.lit/pistol'
* to='pistol@shakespeare.lit/harfleur' * to='pistol@shakespeare.lit/harfleur'
...@@ -604,7 +589,7 @@ ...@@ -604,7 +589,7 @@
* </x> * </x>
* </presence> * </presence>
*/ */
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.openAndEnterChatRoom(converse, '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',
...@@ -621,20 +606,20 @@ ...@@ -621,20 +606,20 @@
.up() .up()
.c('status').attrs({code:'307'}).nodeTree; .c('status').attrs({code:'307'}).nodeTree;
var view = this.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$('.chat-area').is(':visible')).toBeFalsy(); expect(view.$('.chat-area').is(':visible')).toBeFalsy();
expect(view.$('.occupants').is(':visible')).toBeFalsy(); expect(view.$('.occupants').is(':visible')).toBeFalsy();
var $chat_body = view.$('.chatroom-body'); var $chat_body = view.$('.chatroom-body');
expect($chat_body.html().trim().indexOf('<p>You have been kicked from this room</p><p>The reason given is: "Avaunt, you cullion!"</p>')).not.toBe(-1); expect($chat_body.html().trim().indexOf('<p>You have been kicked from this room</p><p>The reason given is: "Avaunt, you cullion!"</p>')).not.toBe(-1);
}.bind(converse)); }));
it("can be saved to, and retrieved from, browserStorage", function () { it("can be saved to, and retrieved from, browserStorage", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
// We instantiate a new ChatBoxes collection, which by default // We instantiate a new ChatBoxes collection, which by default
// will be empty. // will be empty.
test_utils.openControlBox(); test_utils.openControlBox();
var newchatboxes = new this.ChatBoxes(); var newchatboxes = new converse.ChatBoxes();
expect(newchatboxes.length).toEqual(0); expect(newchatboxes.length).toEqual(0);
// The chatboxes will then be fetched from browserStorage inside the // The chatboxes will then be fetched from browserStorage inside the
// onConnected method // onConnected method
...@@ -646,20 +631,20 @@ ...@@ -646,20 +631,20 @@
var new_attrs, old_attrs; var new_attrs, old_attrs;
for (var i=0; i<attrs.length; i++) { for (var i=0; i<attrs.length; i++) {
new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]); new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]); old_attrs = _.pluck(_.pluck(converse.chatboxes.models, 'attributes'), attrs[i]);
// FIXME: should have have to sort here? Order must // FIXME: should have have to sort here? Order must
// probably be the same... // probably be the same...
// This should be fixed once the controlbox always opens // This should be fixed once the controlbox always opens
// only on the right. // only on the right.
expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true); expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
} }
this.rosterview.render(); converse.rosterview.render();
}.bind(converse)); }));
it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", function () { it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
var view = this.chatboxviews.get('lounge@localhost'), var view = converse.chatboxviews.get('lounge@localhost'),
trimmed_chatboxes = this.minimized_chats; trimmed_chatboxes = converse.minimized_chats;
spyOn(view, 'minimize').andCallThrough(); spyOn(view, 'minimize').andCallThrough();
spyOn(view, 'maximize').andCallThrough(); spyOn(view, 'maximize').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -686,11 +671,10 @@ ...@@ -686,11 +671,10 @@
expect(view.model.get('minimized')).toBeFalsy(); expect(view.model.get('minimized')).toBeFalsy();
expect(converse.emit.callCount, 3); expect(converse.emit.callCount, 3);
}); });
}.bind(converse)); }));
it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", mock.initConverse(function (converse) {
it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", function () { test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
test_utils.openChatRoom('lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'close').andCallThrough(); spyOn(view, 'close').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -705,20 +689,19 @@ ...@@ -705,20 +689,19 @@
expect(view.leave).toHaveBeenCalled(); expect(view.leave).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
}); });
}));
}); });
}.bind(converse));
describe("Each chat room can take special commands", function () { describe("Each chat room can take special commands", function () {
beforeEach(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
}); });
});
it("to clear messages", function () { it("to clear messages", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'onMessageSubmitted').andCallThrough(); spyOn(view, 'onMessageSubmitted').andCallThrough();
spyOn(view, 'clearChatRoomMessages'); spyOn(view, 'clearChatRoomMessages');
...@@ -727,10 +710,10 @@ ...@@ -727,10 +710,10 @@
expect(view.onMessageSubmitted).toHaveBeenCalled(); expect(view.onMessageSubmitted).toHaveBeenCalled();
expect(view.clearChatRoomMessages).toHaveBeenCalled(); expect(view.clearChatRoomMessages).toHaveBeenCalled();
}); }));
it("to ban a user", function () { it("to ban a user", mock.initConverse(function (converse) {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom(converse, 'lounge', 'localhost', 'dummy');
var view = converse.chatboxviews.get('lounge@localhost'); var view = converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'onMessageSubmitted').andCallThrough(); spyOn(view, 'onMessageSubmitted').andCallThrough();
spyOn(view, 'setAffiliation').andCallThrough(); spyOn(view, 'setAffiliation').andCallThrough();
...@@ -754,13 +737,18 @@ ...@@ -754,13 +737,18 @@
expect(view.validateRoleChangeCommand.callCount).toBe(2); expect(view.validateRoleChangeCommand.callCount).toBe(2);
expect(view.showStatusNotification.callCount).toBe(1); expect(view.showStatusNotification.callCount).toBe(1);
expect(view.setAffiliation).toHaveBeenCalled(); expect(view.setAffiliation).toHaveBeenCalled();
}));
}); });
}.bind(converse));
describe("When attempting to enter a chatroom", function () { describe("When attempting to enter a chatroom", function () {
beforeEach(function () { afterEach(function () {
var roomspanel = this.chatboxviews.get('controlbox').roomspanel; converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
var submitRoomForm = function (converse) {
var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name'); var $input = roomspanel.$el.find('input.new-chatroom-name');
var $nick = roomspanel.$el.find('input.new-chatroom-nick'); var $nick = roomspanel.$el.find('input.new-chatroom-nick');
var $server = roomspanel.$el.find('input.new-chatroom-server'); var $server = roomspanel.$el.find('input.new-chatroom-server');
...@@ -768,14 +756,10 @@ ...@@ -768,14 +756,10 @@
$nick.val('dummy'); $nick.val('dummy');
$server.val('muc.localhost'); $server.val('muc.localhost');
roomspanel.$el.find('form').submit(); roomspanel.$el.find('form').submit();
}.bind(converse)); };
afterEach(function () {
var view = this.chatboxviews.get('problematic@muc.localhost');
view.close();
}.bind(converse));
it("will show an error message if the room requires a password", function () { it("will show an error message if the room requires a password", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -785,7 +769,7 @@ ...@@ -785,7 +769,7 @@
.c('error').attrs({by:'lounge@localhost', type:'auth'}) .c('error').attrs({by:'lounge@localhost', type:'auth'})
.c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree; .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxviews.get('problematic@muc.localhost'); var view = converse.chatboxviews.get('problematic@muc.localhost');
spyOn(view, 'renderPasswordForm').andCallThrough(); spyOn(view, 'renderPasswordForm').andCallThrough();
runs(function () { runs(function () {
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
...@@ -797,9 +781,10 @@ ...@@ -797,9 +781,10 @@
expect($chat_body.find('form.chatroom-form').length).toBe(1); expect($chat_body.find('form.chatroom-form').length).toBe(1);
expect($chat_body.find('legend').text()).toBe('This chatroom requires a password'); expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
}); });
}.bind(converse)); }));
it("will show an error message if the room is members-only and the user not included", function () { it("will show an error message if the room is members-only and the user not included", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -812,9 +797,10 @@ ...@@ -812,9 +797,10 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room'); expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room');
}); }));
it("will show an error message if the user has been banned", function () { it("will show an error message if the user has been banned", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -827,9 +813,10 @@ ...@@ -827,9 +813,10 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe('You have been banned from this room'); expect(view.$el.find('.chatroom-body p:last').text()).toBe('You have been banned from this room');
}); }));
it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false", function () { it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -842,9 +829,9 @@ ...@@ -842,9 +829,9 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname'); expect(view.$el.find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname');
}); }));
it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true", function () { it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true", mock.initConverse(function (converse) {
/* /*
<presence <presence
from='coven@chat.shakespeare.lit/thirdwitch' from='coven@chat.shakespeare.lit/thirdwitch'
...@@ -857,6 +844,7 @@ ...@@ -857,6 +844,7 @@
</error> </error>
</presence> </presence>
*/ */
submitRoomForm(converse);
converse.muc_nickname_from_jid = true; converse.muc_nickname_from_jid = true;
var attrs = { var attrs = {
...@@ -895,9 +883,10 @@ ...@@ -895,9 +883,10 @@
.c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree; .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.join).toHaveBeenCalledWith('dummy-4'); expect(view.join).toHaveBeenCalledWith('dummy-4');
}); }));
it("will show an error message if the user is not allowed to have created the room", function () { it("will show an error message if the user is not allowed to have created the room", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -906,13 +895,14 @@ ...@@ -906,13 +895,14 @@
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up() .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'lounge@localhost', type:'cancel'}) .c('error').attrs({by:'lounge@localhost', type:'cancel'})
.c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree; .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxviews.get('problematic@muc.localhost'); var view = converse.chatboxviews.get('problematic@muc.localhost');
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms'); expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms');
}.bind(converse)); }));
it("will show an error message if the user's nickname doesn't conform to room policy", function () { it("will show an error message if the user's nickname doesn't conform to room policy", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -925,9 +915,10 @@ ...@@ -925,9 +915,10 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies"); expect(view.$el.find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies");
}); }));
it("will show an error message if the room doesn't yet exist", function () { it("will show an error message if the room doesn't yet exist", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -940,9 +931,10 @@ ...@@ -940,9 +931,10 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist"); expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist");
}); }));
it("will show an error message if the room has reached its maximum number of occupants", function () { it("will show an error message if the room has reached its maximum number of occupants", mock.initConverse(function (converse) {
submitRoomForm(converse);
var presence = $pres().attrs({ var presence = $pres().attrs({
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'n13mt3l', id:'n13mt3l',
...@@ -955,7 +947,7 @@ ...@@ -955,7 +947,7 @@
spyOn(view, 'showErrorMessage').andCallThrough(); spyOn(view, 'showErrorMessage').andCallThrough();
view.onChatRoomPresence(presence); view.onChatRoomPresence(presence);
expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants"); expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants");
}));
});
}); });
}.bind(converse));
}.bind(converse, mock, test_utils));
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"underscore", var _ = converse_api.env._;
"mock", var $ = converse_api.env.jQuery;
"test_utils"
], function ($, _, mock, test_utils) {
return factory($, _, mock, test_utils);
}
);
} (this, function ($, _, mock, test_utils) {
var $pres = converse_api.env.$pres; var $pres = converse_api.env.$pres;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
...@@ -29,52 +22,51 @@ ...@@ -29,52 +22,51 @@
expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy(); expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
}; };
describe("The Control Box", $.proxy(function (mock, test_utils) { describe("The Control Box", function () {
beforeEach(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.openControlBox(); converse_api.listen.not();
}); test_utils.clearBrowserStorage();
}); });
it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () { it("can be opened by clicking a DOM element with class 'toggle-controlbox'", mock.initConverse(function (converse) {
runs(function () {
test_utils.closeControlBox();
});
waits(50);
runs(function () { runs(function () {
// This spec will only pass if the controlbox is not currently // This spec will only pass if the controlbox is not currently
// open yet. // open yet.
expect($("div#controlbox").is(':visible')).toBe(false); expect($("div#controlbox").is(':visible')).toBe(false);
spyOn(this.controlboxtoggle, 'onClick').andCallThrough(); spyOn(converse.controlboxtoggle, 'onClick').andCallThrough();
spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough(); spyOn(converse.controlboxtoggle, 'showControlBox').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
// Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick') // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
this.controlboxtoggle.delegateEvents(); converse.controlboxtoggle.delegateEvents();
$('.toggle-controlbox').click(); $('.toggle-controlbox').click();
}.bind(converse)); }.bind(converse));
waits(50); waits(50);
runs(function () { runs(function () {
expect(this.controlboxtoggle.onClick).toHaveBeenCalled(); expect(converse.controlboxtoggle.onClick).toHaveBeenCalled();
expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled(); expect(converse.controlboxtoggle.showControlBox).toHaveBeenCalled();
expect(this.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object)); expect(converse.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
expect($("div#controlbox").is(':visible')).toBe(true); expect($("div#controlbox").is(':visible')).toBe(true);
}.bind(converse)); }.bind(converse));
}, converse)); }));
describe("The Status Widget", $.proxy(function () {
beforeEach(function () { describe("The Status Widget", function () {
test_utils.openControlBox(); afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
}); });
it("shows the user's chat status, which is online by default", $.proxy(function () { it("shows the user's chat status, which is online by default", mock.initConverse(function (converse) {
var view = this.xmppstatusview; test_utils.openControlBox();
var view = converse.xmppstatusview;
expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online'); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
}, converse)); }));
it("can be used to set the current user's chat status", $.proxy(function () { it("can be used to set the current user's chat status", mock.initConverse(function (converse) {
var view = this.xmppstatusview; test_utils.openControlBox();
var view = converse.xmppstatusview;
spyOn(view, 'toggleOptions').andCallThrough(); spyOn(view, 'toggleOptions').andCallThrough();
spyOn(view, 'setStatus').andCallThrough(); spyOn(view, 'setStatus').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -92,17 +84,18 @@ ...@@ -92,17 +84,18 @@
expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd'); expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
}); });
waits(250); waits(250);
runs($.proxy(function () { runs(function () {
expect(view.updateStatusUI).toHaveBeenCalled(); expect(view.updateStatusUI).toHaveBeenCalled();
expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false); expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy'); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
}, converse)); });
}, converse)); }));
it("can be used to set a custom status message", $.proxy(function () { it("can be used to set a custom status message", mock.initConverse(function (converse) {
var view = this.xmppstatusview; test_utils.openControlBox();
this.xmppstatus.save({'status': 'online'}); var view = converse.xmppstatusview;
converse.xmppstatus.save({'status': 'online'});
spyOn(view, 'setStatusMessage').andCallThrough(); spyOn(view, 'setStatusMessage').andCallThrough();
spyOn(view, 'renderStatusChangeForm').andCallThrough(); spyOn(view, 'renderStatusChangeForm').andCallThrough();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
...@@ -123,27 +116,24 @@ ...@@ -123,27 +116,24 @@
expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
}); });
}, converse)); }));
}, converse)); });
}, converse, mock, test_utils)); });
describe("The Contacts Roster", $.proxy(function (mock, utils) { describe("The Contacts Roster", function () {
function _clearContacts () {
utils.clearBrowserStorage();
converse.rosterview.model.reset();
}
describe("The live filter", $.proxy(function () { describe("The live filter", function () {
beforeEach(function () { afterEach(function () {
_clearContacts(); converse_api.user.logout();
test_utils.openControlBox(); converse_api.listen.not();
test_utils.openContactsPanel(); test_utils.clearBrowserStorage();
}); });
it("will only appear when roster contacts flow over the visible area", function () { it("will only appear when roster contacts flow over the visible area", mock.initConverse(function (converse) {
var $filter = converse.rosterview.$('.roster-filter'); var $filter = converse.rosterview.$('.roster-filter');
var names = mock.cur_names; var names = mock.cur_names;
runs(function () { runs(function () {
test_utils.openControlBox();
converse.rosterview.update(); // XXX: Will normally called as event handler converse.rosterview.update(); // XXX: Will normally called as event handler
}); });
waits(5); // Needed, due to debounce waits(5); // Needed, due to debounce
...@@ -170,14 +160,18 @@ ...@@ -170,14 +160,18 @@
expect($filter.is(':visible')).toBeFalsy(); expect($filter.is(':visible')).toBeFalsy();
} }
}); });
}); }));
it("can be used to filter the contacts shown", function () { it("can be used to filter the contacts shown", mock.initConverse(function (converse) {
var $filter; var $filter;
var $roster; var $roster;
runs(function () { runs(function () {
converse.roster_groups = true; converse.roster_groups = true;
utils.createGroupedContacts(); test_utils.openControlBox();
});
waits(50);
runs(function () {
test_utils.createGroupedContacts(converse);
$filter = converse.rosterview.$('.roster-filter'); $filter = converse.rosterview.$('.roster-filter');
$roster = converse.rosterview.$roster; $roster = converse.rosterview.$roster;
}); });
...@@ -223,15 +217,19 @@ ...@@ -223,15 +217,19 @@
expect($roster.find('dt:visible').length).toBe(5); expect($roster.find('dt:visible').length).toBe(5);
}); });
converse.roster_groups = false; converse.roster_groups = false;
}); }));
it("can be used to filter the groups shown", function () { it("can be used to filter the groups shown", mock.initConverse(function (converse) {
var $filter; var $filter;
var $roster; var $roster;
var $type; var $type;
runs(function () { runs(function () {
converse.roster_groups = true; converse.roster_groups = true;
utils.createGroupedContacts(); test_utils.openControlBox();
});
waits(50); // Needed, due to debounce in "update" method
runs(function () {
test_utils.createGroupedContacts(converse);
converse.rosterview.filter_view.delegateEvents(); converse.rosterview.filter_view.delegateEvents();
$filter = converse.rosterview.$('.roster-filter'); $filter = converse.rosterview.$('.roster-filter');
$roster = converse.rosterview.$roster; $roster = converse.rosterview.$roster;
...@@ -243,6 +241,9 @@ ...@@ -243,6 +241,9 @@
expect($roster.find('dd:visible').length).toBe(15); expect($roster.find('dd:visible').length).toBe(15);
expect($roster.find('dt:visible').length).toBe(5); expect($roster.find('dt:visible').length).toBe(5);
$filter.val("colleagues"); $filter.val("colleagues");
});
waits(50); // Needed, due to debounce
runs(function () {
expect($roster.find('dd:visible').length).toBe(15); // because no keydown event expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
expect($roster.find('dt:visible').length).toBe(5); // ditto expect($roster.find('dt:visible').length).toBe(5); // ditto
$filter.trigger('keydown'); $filter.trigger('keydown');
...@@ -270,11 +271,11 @@ ...@@ -270,11 +271,11 @@
expect($roster.find('dt:visible').length).toBe(5); expect($roster.find('dt:visible').length).toBe(5);
}); });
converse.roster_groups = false; converse.roster_groups = false;
}); }));
it("has a button with which its contents can be cleared", function () { it("has a button with which its contents can be cleared", mock.initConverse(function (converse) {
converse.roster_groups = true; converse.roster_groups = true;
utils.createGroupedContacts(); test_utils.createGroupedContacts(converse);
var $filter = converse.rosterview.$('.roster-filter'); var $filter = converse.rosterview.$('.roster-filter');
runs (function () { runs (function () {
converse.rosterview.filter_view.delegateEvents(); converse.rosterview.filter_view.delegateEvents();
...@@ -290,17 +291,18 @@ ...@@ -290,17 +291,18 @@
expect($filter.val()).toBe(""); expect($filter.val()).toBe("");
}); });
converse.roster_groups = false; converse.roster_groups = false;
}); }));
it("can be used to filter contacts by their chat state", function () { it("can be used to filter contacts by their chat state", mock.initConverse(function (converse) {
var $filter; var $filter;
var $roster; var $roster;
converse.roster_groups = true; converse.roster_groups = true;
utils.createGroupedContacts(); test_utils.createGroupedContacts(converse);
var jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
converse.roster.get(jid).set('chat_status', 'online'); converse.roster.get(jid).set('chat_status', 'online');
runs(function () { runs(function () {
test_utils.openControlBox();
converse.rosterview.filter_view.delegateEvents(); converse.rosterview.filter_view.delegateEvents();
var $type = converse.rosterview.$('.filter-type'); var $type = converse.rosterview.$('.filter-type');
$type.val('state').trigger('change'); $type.val('state').trigger('change');
...@@ -326,33 +328,31 @@ ...@@ -326,33 +328,31 @@
$type.val('contacts').trigger('change'); $type.val('contacts').trigger('change');
converse.roster_groups = false; converse.roster_groups = false;
}); });
}));
}); });
}, converse)); describe("A Roster Group", function () {
describe("A Roster Group", $.proxy(function () {
beforeEach(function () {
_clearContacts();
converse.roster_groups = true;
});
afterEach(function () { afterEach(function () {
converse.roster_groups = false; converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
}); });
it("can be used to organize existing contacts", $.proxy(function () { it("can be used to organize existing contacts", mock.initConverse(function (converse) {
runs($.proxy(function () { runs(function () {
converse.roster_groups = true;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
converse.rosterview.render(); converse.rosterview.render();
utils.createContacts('pending'); test_utils.createContacts(converse, 'pending');
utils.createContacts('requesting'); test_utils.createContacts(converse, 'requesting');
utils.createGroupedContacts(); test_utils.createGroupedContacts(converse);
}, this)); });
waits(50); // Needed, due to debounce waits(50); // Needed, due to debounce
runs($.proxy(function () { runs(function () {
// Check that the groups appear alphabetically and that // Check that the groups appear alphabetically and that
// requesting and pending contacts are last. // requesting and pending contacts are last.
var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); }); var group_titles = $.map(converse.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
expect(group_titles).toEqual([ expect(group_titles).toEqual([
"Contact requests", "Contact requests",
"colleagues", "colleagues",
...@@ -363,23 +363,24 @@ ...@@ -363,23 +363,24 @@
"Pending contacts" "Pending contacts"
]); ]);
// Check that usernames appear alphabetically per group // Check that usernames appear alphabetically per group
_.each(_.keys(mock.groups), $.proxy(function (name) { _.each(_.keys(mock.groups), function (name) {
var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd'); var $contacts = converse.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
var names = $.map($contacts, function (o) { return $(o).text().trim(); }); var names = $.map($contacts, function (o) { return $(o).text().trim(); });
expect(names).toEqual(_.clone(names).sort()); expect(names).toEqual(_.clone(names).sort());
}, converse)); });
}, this)); });
}, converse)); }));
it("can share contacts with other roster groups", $.proxy(function () { it("can share contacts with other roster groups", mock.initConverse(function (converse) {
converse.roster_groups = true;
var groups = ['colleagues', 'friends']; var groups = ['colleagues', 'friends'];
runs($.proxy(function () { runs(function () {
var i=0; var i=0;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
converse.rosterview.render(); converse.rosterview.render();
for (i=0; i<mock.cur_names.length; i++) { for (i=0; i<mock.cur_names.length; i++) {
this.roster.create({ converse.roster.create({
jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost', jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'both', subscription: 'both',
ask: null, ask: null,
...@@ -387,30 +388,31 @@ ...@@ -387,30 +388,31 @@
fullname: mock.cur_names[i] fullname: mock.cur_names[i]
}); });
} }
}, this)); });
waits(50); // Needed, due to debounce waits(50); // Needed, due to debounce
runs($.proxy(function () { runs(function () {
// Check that usernames appear alphabetically per group // Check that usernames appear alphabetically per group
_.each(groups, $.proxy(function (name) { _.each(groups, function (name) {
var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd'); var $contacts = converse.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
var names = $.map($contacts, function (o) { return $(o).text().trim(); }); var names = $.map($contacts, function (o) { return $(o).text().trim(); });
expect(names).toEqual(_.clone(names).sort()); expect(names).toEqual(_.clone(names).sort());
expect(names.length).toEqual(mock.cur_names.length); expect(names.length).toEqual(mock.cur_names.length);
}, this)); });
}, this)); });
}, converse)); }));
it("remembers whether it is closed or opened", $.proxy(function () { it("remembers whether it is closed or opened", mock.initConverse(function (converse) {
converse.roster_groups = true;
var i=0, j=0; var i=0, j=0;
var groups = { var groups = {
'colleagues': 3, 'colleagues': 3,
'friends & acquaintences': 3, 'friends & acquaintences': 3,
'Ungrouped': 2 'Ungrouped': 2
}; };
_.each(_.keys(groups), $.proxy(function (name) { _.each(_.keys(groups), function (name) {
j = i; j = i;
for (i=j; i<j+groups[name]; i++) { for (i=j; i<j+groups[name]; i++) {
this.roster.create({ converse.roster.create({
jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost', jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'both', subscription: 'both',
ask: null, ask: null,
...@@ -418,65 +420,62 @@ ...@@ -418,65 +420,62 @@
fullname: mock.cur_names[i] fullname: mock.cur_names[i]
}); });
} }
}, converse)); });
var view = this.rosterview.get('colleagues'); var view = converse.rosterview.get('colleagues');
var $toggle = view.$el.find('a.group-toggle'); var $toggle = view.$el.find('a.group-toggle');
expect(view.model.get('state')).toBe('opened'); expect(view.model.get('state')).toBe('opened');
$toggle.click(); $toggle.click();
expect(view.model.get('state')).toBe('closed'); expect(view.model.get('state')).toBe('closed');
$toggle.click(); $toggle.click();
expect(view.model.get('state')).toBe('opened'); expect(view.model.get('state')).toBe('opened');
}, converse)); }));
}, converse)); });
describe("Pending Contacts", $.proxy(function () { describe("Pending Contacts", function () {
function _clearContacts () { afterEach(function () {
utils.clearBrowserStorage(); converse_api.user.logout();
converse.rosterview.model.reset(); converse_api.listen.not();
} test_utils.clearBrowserStorage();
});
function _addContacts () { function _addContacts (converse) {
// Must be initialized, so that render is called and documentFragment set up. // Must be initialized, so that render is called and documentFragment set up.
utils.createContacts('pending').openControlBox().openContactsPanel(); test_utils.createContacts(converse, 'pending').openControlBox().openContactsPanel(converse);
} }
beforeEach(function () { it("can be collapsed under their own header", mock.initConverse(function (converse) {
_clearContacts();
});
it("can be collapsed under their own header", $.proxy(function () {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
checkHeaderToggling.apply(this, [this.rosterview.get('Pending contacts').$el]); checkHeaderToggling.apply(converse, [converse.rosterview.get('Pending contacts').$el]);
}, this)); });
}, converse)); }));
it("can be added to the roster", $.proxy(function () { it("can be added to the roster", mock.initConverse(function (converse) {
_clearContacts();
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
runs($.proxy(function () { runs(function () {
this.roster.create({ test_utils.openControlBox();
converse.roster.create({
jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost', jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'none', subscription: 'none',
ask: 'subscribe', ask: 'subscribe',
fullname: mock.pend_names[0] fullname: mock.pend_names[0]
}); });
}, converse)); });
waits(300); waits(300);
runs($.proxy(function () { runs(function () {
expect(this.rosterview.$el.is(':visible')).toEqual(true); expect(converse.rosterview.$el.is(':visible')).toEqual(true);
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
}, converse)); });
}, converse)); }));
it("are shown in the roster when show_only_online_users", function () { it("are shown in the roster when show_only_online_users", mock.initConverse(function (converse) {
converse.show_only_online_users = true; converse.show_only_online_users = true;
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
spyOn(converse.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
...@@ -490,40 +489,40 @@ ...@@ -490,40 +489,40 @@
expect(converse.rosterview.$el.find('dt:visible').length).toBe(1); expect(converse.rosterview.$el.find('dt:visible').length).toBe(1);
}); });
converse.show_only_online_users = false; converse.show_only_online_users = false;
}); }));
it("are shown in the roster when hide_offline_users", $.proxy(function () { it("are shown in the roster when hide_offline_users", mock.initConverse(function (converse) {
converse.hide_offline_users = true; converse.hide_offline_users = true;
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
runs($.proxy(function () { runs(function () {
expect(this.rosterview.$el.is(':visible')).toEqual(true); expect(converse.rosterview.$el.is(':visible')).toEqual(true);
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
}, converse)); });
waits(300); // Needed, due to debounce waits(300); // Needed, due to debounce
runs ($.proxy(function () { runs (function () {
expect(this.rosterview.$el.find('dd:visible').length).toBe(3); expect(converse.rosterview.$el.find('dd:visible').length).toBe(3);
expect(this.rosterview.$el.find('dt:visible').length).toBe(1); expect(converse.rosterview.$el.find('dt:visible').length).toBe(1);
}, converse)); });
converse.hide_offline_users = false; converse.hide_offline_users = false;
}, converse)); }));
it("can be removed by the user", $.proxy(function () { it("can be removed by the user", mock.initConverse(function (converse) {
runs($.proxy(function () { runs(function () {
_addContacts(); _addContacts(converse);
}, this)); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var name = mock.pend_names[0]; var name = mock.pend_names[0];
var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var contact = this.roster.get(jid); var contact = converse.roster.get(jid);
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(contact, 'unauthorize').andCallFake(function () { return contact; }); spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
spyOn(contact, 'removeFromRoster'); spyOn(contact, 'removeFromRoster');
spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
if (typeof callback === "function") { return callback(); } if (typeof callback === "function") { return callback(); }
}); });
...@@ -533,40 +532,39 @@ ...@@ -533,40 +532,39 @@
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(converse.connection.sendIQ).toHaveBeenCalled(); expect(converse.connection.sendIQ).toHaveBeenCalled();
expect(contact.removeFromRoster).toHaveBeenCalled(); expect(contact.removeFromRoster).toHaveBeenCalled();
expect(this.connection.sendIQ).toHaveBeenCalled(); expect(converse.connection.sendIQ).toHaveBeenCalled();
expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0); expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
}, this)); });
}, converse)); }));
it("do not have a header if there aren't any", $.proxy(function () { it("do not have a header if there aren't any", mock.initConverse(function (converse) {
runs(function () {
test_utils.openControlBox();
var name = mock.pend_names[0]; var name = mock.pend_names[0];
runs($.proxy(function () { converse.roster.create({
_clearContacts();
}, this));
waits(50);
runs($.proxy(function () {
this.roster.create({
jid: name.replace(/ /g,'.').toLowerCase() + '@localhost', jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'none', subscription: 'none',
ask: 'subscribe', ask: 'subscribe',
fullname: name fullname: name
}); });
});
waits(20);
runs(function () {
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
if (typeof callback === "function") { return callback(); } if (typeof callback === "function") { return callback(); }
}); });
expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true); expect(converse.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')") converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
.siblings('.remove-xmpp-contact').click(); .siblings('.remove-xmpp-contact').click();
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(this.connection.sendIQ).toHaveBeenCalled(); expect(converse.connection.sendIQ).toHaveBeenCalled();
expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false); expect(converse.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
}, this)); });
}, converse)); }));
it("will lose their own header once the last one has been removed", $.proxy(function () { it("will lose their own header once the last one has been removed", mock.initConverse(function (converse) {
_addContacts(); _addContacts(converse);
var name; var name;
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
for (var i=0; i<mock.pend_names.length; i++) { for (var i=0; i<mock.pend_names.length; i++) {
...@@ -574,10 +572,10 @@ ...@@ -574,10 +572,10 @@
converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')") converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
.siblings('.remove-xmpp-contact').click(); .siblings('.remove-xmpp-contact').click();
} }
expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy(); expect(converse.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
}, converse)); }));
it("can be added to the roster and they will be sorted alphabetically", function () { it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (converse) {
var i, t; var i, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(converse.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
...@@ -593,38 +591,34 @@ ...@@ -593,38 +591,34 @@
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = converse.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text(); t = converse.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
}); }));
}, converse));
describe("Existing Contacts", $.proxy(function () { });
function _clearContacts () {
utils.clearBrowserStorage();
converse.rosterview.model.reset();
}
var _addContacts = function () { describe("Existing Contacts", function () {
utils.createContacts('current').openControlBox().openContactsPanel(); var _addContacts = function (converse) {
test_utils.createContacts(converse, 'current').openControlBox().openContactsPanel(converse);
}; };
beforeEach(function () { afterEach(function () {
_clearContacts(); converse_api.user.logout();
utils.openControlBox().openContactsPanel(); converse_api.listen.not();
test_utils.clearBrowserStorage();
}); });
it("can be collapsed under their own header", $.proxy(function () { it("can be collapsed under their own header", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]); checkHeaderToggling.apply(converse, [converse.rosterview.$el.find('dt.roster-group')]);
}, this)); });
}, converse)); }));
it("will be hidden when appearing under a collapsed group", $.proxy(function () { it("will be hidden when appearing under a collapsed group", mock.initConverse(function (converse) {
_addContacts(); _addContacts(converse);
this.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click(); converse.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
var name = "Max Mustermann"; var name = "Max Mustermann";
var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
converse.roster.create({ converse.roster.create({
...@@ -634,45 +628,44 @@ ...@@ -634,45 +628,44 @@
requesting: false, requesting: false,
subscription: 'both' subscription: 'both'
}); });
var view = this.rosterview.get('My contacts').get(jid); var view = converse.rosterview.get('My contacts').get(jid);
expect(view.$el.is(':visible')).toBe(false); expect(view.$el.is(':visible')).toBe(false);
}, converse)); }));
it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () { it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () {
_clearContacts();
});
waits(50);
runs($.proxy(function () {
var i, t; var i, t;
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
runs(function () {
for (i=0; i<mock.cur_names.length; i++) { for (i=0; i<mock.cur_names.length; i++) {
this.roster.create({ converse.roster.create({
jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost', jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'both', subscription: 'both',
ask: null, ask: null,
fullname: mock.cur_names[i] fullname: mock.cur_names[i]
}); });
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
} }
});
waits(10);
runs(function () {
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
}, this)); });
}, converse)); }));
it("can be removed by the user", $.proxy(function () { it("can be removed by the user", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var name = mock.cur_names[0]; var name = mock.cur_names[0];
var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var contact = this.roster.get(jid); var contact = converse.roster.get(jid);
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(contact, 'removeFromRoster'); spyOn(contact, 'removeFromRoster');
spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
if (typeof callback === "function") { return callback(); } if (typeof callback === "function") { return callback(); }
}); });
...@@ -683,168 +676,168 @@ ...@@ -683,168 +676,168 @@
expect(converse.connection.sendIQ).toHaveBeenCalled(); expect(converse.connection.sendIQ).toHaveBeenCalled();
expect(contact.removeFromRoster).toHaveBeenCalled(); expect(contact.removeFromRoster).toHaveBeenCalled();
expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0); expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
}, this)); });
}, converse)); }));
it("do not have a header if there aren't any", $.proxy(function () { it("do not have a header if there aren't any", mock.initConverse(function (converse) {
var name = mock.cur_names[0]; var name = mock.cur_names[0];
var contact;
runs(function () { runs(function () {
_clearContacts(); contact = converse.roster.create({
});
waits(50);
runs($.proxy(function () {
var contact = this.roster.create({
jid: name.replace(/ /g,'.').toLowerCase() + '@localhost', jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'both', subscription: 'both',
ask: null, ask: null,
fullname: name fullname: name
}); });
});
waits(50);
runs(function () {
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(contact, 'removeFromRoster'); spyOn(contact, 'removeFromRoster');
spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
if (typeof callback === "function") { return callback(); } if (typeof callback === "function") { return callback(); }
}); });
expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block'); expect(converse.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
converse.rosterview.$el.find(".open-chat:contains('"+name+"')") converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
.siblings('.remove-xmpp-contact').click(); .siblings('.remove-xmpp-contact').click();
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(this.connection.sendIQ).toHaveBeenCalled(); expect(converse.connection.sendIQ).toHaveBeenCalled();
expect(contact.removeFromRoster).toHaveBeenCalled(); expect(contact.removeFromRoster).toHaveBeenCalled();
expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none'); expect(converse.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
}, this)); });
}, converse)); }));
it("can change their status to online and be sorted alphabetically", $.proxy(function () { it("can change their status to online and be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var jid, t; var jid, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(converse.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
for (var i=0; i<mock.cur_names.length; i++) { for (var i=0; i<mock.cur_names.length; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'online'); converse.roster.get(jid).set('chat_status', 'online');
expect(converse.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
} }
}, this)); });
}, converse)); }));
it("can change their status to busy and be sorted alphabetically", $.proxy(function () { it("can change their status to busy and be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var jid, t; var jid, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
for (var i=0; i<mock.cur_names.length; i++) { for (var i=0; i<mock.cur_names.length; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'dnd'); converse.roster.get(jid).set('chat_status', 'dnd');
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
} }
}, this)); });
}, converse)); }));
it("can change their status to away and be sorted alphabetically", $.proxy(function () { it("can change their status to away and be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var jid, t; var jid, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
for (var i=0; i<mock.cur_names.length; i++) { for (var i=0; i<mock.cur_names.length; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'away'); converse.roster.get(jid).set('chat_status', 'away');
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
} }
}, this)); });
}, converse)); }));
it("can change their status to xa and be sorted alphabetically", $.proxy(function () { it("can change their status to xa and be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var jid, t; var jid, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
for (var i=0; i<mock.cur_names.length; i++) { for (var i=0; i<mock.cur_names.length; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'xa'); converse.roster.get(jid).set('chat_status', 'xa');
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
} }
}, this)); });
}, converse)); }));
it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () { it("can change their status to unavailable and be sorted alphabetically", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var jid, t; var jid, t;
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.rosterview, 'update').andCallThrough(); spyOn(converse.rosterview, 'update').andCallThrough();
for (var i=0; i<mock.cur_names.length; i++) { for (var i=0; i<mock.cur_names.length; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'unavailable'); converse.roster.get(jid).set('chat_status', 'unavailable');
expect(this.rosterview.update).toHaveBeenCalled(); expect(converse.rosterview.update).toHaveBeenCalled();
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text(); t = converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
} }
}, this)); });
}, converse)); }));
it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () { it("are ordered according to status: online, busy, away, xa, unavailable, offline", mock.initConverse(function (converse) {
runs(function () { runs(function () {
_addContacts(); _addContacts(converse);
}); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var i, jid; var i, jid;
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'online'); converse.roster.get(jid).set('chat_status', 'online');
} }
for (i=3; i<6; i++) { for (i=3; i<6; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'dnd'); converse.roster.get(jid).set('chat_status', 'dnd');
} }
for (i=6; i<9; i++) { for (i=6; i<9; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'away'); converse.roster.get(jid).set('chat_status', 'away');
} }
for (i=9; i<12; i++) { for (i=9; i<12; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'xa'); converse.roster.get(jid).set('chat_status', 'xa');
} }
for (i=12; i<15; i++) { for (i=12; i<15; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
this.roster.get(jid).set('chat_status', 'unavailable'); converse.roster.get(jid).set('chat_status', 'unavailable');
} }
var contacts = this.rosterview.$el.find('dd.current-xmpp-contact'); var contacts = converse.rosterview.$el.find('dd.current-xmpp-contact');
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
expect($(contacts[i]).hasClass('online')).toBeTruthy(); expect($(contacts[i]).hasClass('online')).toBeTruthy();
expect($(contacts[i]).hasClass('both')).toBeTruthy(); expect($(contacts[i]).hasClass('both')).toBeTruthy();
...@@ -899,24 +892,20 @@ ...@@ -899,24 +892,20 @@
expect($(contacts[i]).hasClass('xa')).toBeFalsy(); expect($(contacts[i]).hasClass('xa')).toBeFalsy();
expect($(contacts[i]).hasClass('unavailable')).toBeFalsy(); expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
} }
}, this));
}, converse));
}, converse));
describe("Requesting Contacts", $.proxy(function () {
beforeEach($.proxy(function () {
runs(function () {
utils.clearBrowserStorage();
converse.rosterview.model.reset();
utils.createContacts('requesting').openControlBox();
}); });
waits(50); }));
runs(function () {
utils.openContactsPanel();
}); });
}, converse));
it("can be added to the roster and they will be sorted alphabetically", function () { describe("Requesting Contacts", function () {
afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'requesting').openControlBox();
test_utils.openContactsPanel(converse);
converse.rosterview.model.reset(); // We want to manually create users so that we can spy converse.rosterview.model.reset(); // We want to manually create users so that we can spy
var i, children; var i, children;
var names = []; var names = [];
...@@ -946,43 +935,55 @@ ...@@ -946,43 +935,55 @@
names = []; names = [];
children.each(addName); children.each(addName);
expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join('')); expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
}); }));
it("do not have a header if there aren't any", mock.initConverse(function (converse) {
test_utils.openContactsPanel(converse);
it("do not have a header if there aren't any", $.proxy(function () {
converse.rosterview.model.reset(); // We want to manually create users so that we can spy
var name = mock.req_names[0]; var name = mock.req_names[0];
runs($.proxy(function () { runs(function () {
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
this.roster.create({ converse.roster.create({
jid: name.replace(/ /g,'.').toLowerCase() + '@localhost', jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
subscription: 'none', subscription: 'none',
ask: null, ask: null,
requesting: true, requesting: true,
fullname: name fullname: name
}); });
}, this)); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true); expect(converse.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')") converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
.siblings('.request-actions') .siblings('.request-actions')
.find('.decline-xmpp-request').click(); .find('.decline-xmpp-request').click();
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false); expect(converse.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
}, this)); });
}, converse)); }));
it("can be collapsed under their own header", $.proxy(function () { it("can be collapsed under their own header", mock.initConverse(function (converse) {
checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]); runs(function () {
}, converse)); test_utils.createContacts(converse, 'requesting').openControlBox();
});
waits(10);
runs(function () {
checkHeaderToggling.apply(converse, [converse.rosterview.get('Contact requests').$el]);
});
}));
it("can have their requests accepted by the user", $.proxy(function () { it("can have their requests accepted by the user", mock.initConverse(function (converse) {
runs(function () {
test_utils.createContacts(converse, 'requesting').openControlBox();
});
waits(10);
runs(function () {
// TODO: Testing can be more thorough here, the user is // TODO: Testing can be more thorough here, the user is
// actually not accepted/authorized because of // actually not accepted/authorized because of
// mock_connection. // mock_connection.
var name = mock.req_names.sort()[0]; var name = mock.req_names.sort()[0];
var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var contact = this.roster.get(jid); var contact = converse.roster.get(jid);
spyOn(converse.roster, 'sendContactAddIQ').andCallFake(function (jid, fullname, groups, callback) { spyOn(converse.roster, 'sendContactAddIQ').andCallFake(function (jid, fullname, groups, callback) {
callback(); callback();
}); });
...@@ -992,19 +993,19 @@ ...@@ -992,19 +993,19 @@
.find('.accept-xmpp-request').click(); .find('.accept-xmpp-request').click();
expect(converse.roster.sendContactAddIQ).toHaveBeenCalled(); expect(converse.roster.sendContactAddIQ).toHaveBeenCalled();
expect(contact.authorize).toHaveBeenCalled(); expect(contact.authorize).toHaveBeenCalled();
}, converse)); });
}));
it("can have their requests denied by the user", $.proxy(function () { it("can have their requests denied by the user", mock.initConverse(function (converse) {
this.rosterview.model.reset(); runs(function () {
runs($.proxy(function () { test_utils.createContacts(converse, 'requesting').openControlBox();
utils.createContacts('requesting').openControlBox();
converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced. converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
}, this)); });
waits(50); waits(50);
runs($.proxy(function () { runs(function () {
var name = mock.req_names.sort()[1]; var name = mock.req_names.sort()[1];
var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var contact = this.roster.get(jid); var contact = converse.roster.get(jid);
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(contact, 'unauthorize').andCallFake(function () { return contact; }); spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')") converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
...@@ -1013,26 +1014,25 @@ ...@@ -1013,26 +1014,25 @@
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
expect(contact.unauthorize).toHaveBeenCalled(); expect(contact.unauthorize).toHaveBeenCalled();
// There should now be one less contact // There should now be one less contact
expect(this.roster.length).toEqual(mock.req_names.length-1); expect(converse.roster.length).toEqual(mock.req_names.length-1);
}, this)); });
}, converse)); }));
it("are persisted even if other contacts' change their presence ", $.proxy(function() { it("are persisted even if other contacts' change their presence ", mock.initConverse(function (converse) {
/* This is a regression test. /* This is a regression test.
* https://github.com/jcbrand/converse.js/issues/262 * https://github.com/jcbrand/converse.js/issues/262
*/ */
this.rosterview.model.reset(); expect(converse.roster.pluck('jid').length).toBe(0);
expect(this.roster.pluck('jid').length).toBe(0);
var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'}); var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
this.connection._dataRecv(test_utils.createRequest(stanza)); converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(this.roster.pluck('jid').length).toBe(1); expect(converse.roster.pluck('jid').length).toBe(1);
expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy(); expect(_.contains(converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
// Taken from the spec // Taken from the spec
// http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3 // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
stanza = $iq({ stanza = $iq({
to: this.connection.jid, to: converse.connection.jid,
type: 'result', type: 'result',
id: 'roster_1' id: 'roster_1'
}).c('query', { }).c('query', {
...@@ -1052,32 +1052,27 @@ ...@@ -1052,32 +1052,27 @@
name: 'Benvolio', name: 'Benvolio',
subscription:'both' subscription:'both'
}).c('group').t('Friends'); }).c('group').t('Friends');
this.roster.onReceivedFromServer(stanza.tree()); converse.roster.onReceivedFromServer(stanza.tree());
expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy(); expect(_.contains(converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
}, converse)); }));
}, converse));
describe("All Contacts", $.proxy(function () {
beforeEach($.proxy(function () {
runs(function () {
utils.clearBrowserStorage();
converse.rosterview.model.reset();
utils.createContacts('all').openControlBox();
}); });
waits(50);
runs(function () { describe("All Contacts", function () {
utils.openContactsPanel(); afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
}); });
}, converse));
it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () { it("are saved to, and can be retrieved from browserStorage", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'all').openControlBox();
test_utils.openContactsPanel(converse);
var new_attrs, old_attrs, attrs; var new_attrs, old_attrs, attrs;
var num_contacts = this.roster.length; var num_contacts = converse.roster.length;
var new_roster = new this.RosterContacts(); var new_roster = new converse.RosterContacts();
// Roster items are yet to be fetched from browserStorage // Roster items are yet to be fetched from browserStorage
expect(new_roster.length).toEqual(0); expect(new_roster.length).toEqual(0);
new_roster.browserStorage = this.roster.browserStorage; new_roster.browserStorage = converse.roster.browserStorage;
new_roster.fetch(); new_roster.fetch();
expect(new_roster.length).toEqual(num_contacts); expect(new_roster.length).toEqual(num_contacts);
// Check that the roster items retrieved from browserStorage // Check that the roster items retrieved from browserStorage
...@@ -1085,78 +1080,84 @@ ...@@ -1085,78 +1080,84 @@
attrs = ['jid', 'fullname', 'subscription', 'ask']; attrs = ['jid', 'fullname', 'subscription', 'ask'];
for (var i=0; i<attrs.length; i++) { for (var i=0; i<attrs.length; i++) {
new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]); new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]); old_attrs = _.pluck(_.pluck(converse.roster.models, 'attributes'), attrs[i]);
// Roster items in storage are not necessarily sorted, // Roster items in storage are not necessarily sorted,
// so we have to sort them here to do a proper // so we have to sort them here to do a proper
// comparison // comparison
expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true); expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
} }
}, converse)); }));
it("will show fullname and jid properties on tooltip", $.proxy(function () { it("will show fullname and jid properties on tooltip", mock.initConverse(function (converse) {
runs(function () {
test_utils.createContacts(converse, 'all').openControlBox();
test_utils.openContactsPanel(converse);
});
waits(10);
runs(function () {
var jid, name, i; var jid, name, i;
for (i=0; i<mock.cur_names.length; i++) { for (i=0; i<mock.cur_names.length; i++) {
name = mock.cur_names[i]; name = mock.cur_names[i];
jid = name.replace(/ /g,'.').toLowerCase() + '@localhost'; jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var $dd = this.rosterview.$el.find("dd:contains('"+name+"')").children().first(); var $dd = converse.rosterview.$el.find("dd:contains('"+name+"')").children().first();
var dd_text = $dd.text(); var dd_text = $dd.text();
var dd_title = $dd.attr('title'); var dd_title = $dd.attr('title');
expect(dd_text).toBe(name); expect(dd_text).toBe(name);
expect(dd_title).toContain(name); expect(dd_title).toContain(name);
expect(dd_title).toContain(jid); expect(dd_title).toContain(jid);
} }
}, converse)); });
}));
}, converse)); });
}, converse, mock, test_utils)); });
describe("The 'Add Contact' widget", function () {
afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) { it("opens up an add form when you click on it", mock.initConverse(function (converse) {
it("opens up an add form when you click on it", $.proxy(function () { var panel = converse.chatboxviews.get('controlbox').contactspanel;
var panel = this.chatboxviews.get('controlbox').contactspanel;
spyOn(panel, 'toggleContactForm').andCallThrough(); spyOn(panel, 'toggleContactForm').andCallThrough();
panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
panel.$el.find('a.toggle-xmpp-contact-form').click(); panel.$el.find('a.toggle-xmpp-contact-form').click();
expect(panel.toggleContactForm).toHaveBeenCalled(); expect(panel.toggleContactForm).toHaveBeenCalled();
// XXX: Awaiting more tests, close it again for now... // XXX: Awaiting more tests, close it again for now...
panel.$el.find('a.toggle-xmpp-contact-form').click(); panel.$el.find('a.toggle-xmpp-contact-form').click();
}, converse)); }));
}, converse, mock, test_utils));
describe("The Controlbox Tabs", $.proxy(function () {
beforeEach($.proxy(function () {
runs(function () {
test_utils.closeAllChatBoxes();
}); });
waits(50);
runs(function () { describe("The Controlbox Tabs", function () {
test_utils.openControlBox(); afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
}); });
}, converse));
it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () { it("contains two tabs, 'Contacts' and 'ChatRooms'", mock.initConverse(function (converse) {
var cbview = this.chatboxviews.get('controlbox'); test_utils.openControlBox();
var cbview = converse.chatboxviews.get('controlbox');
var $panels = cbview.$el.find('.controlbox-panes'); var $panels = cbview.$el.find('.controlbox-panes');
expect($panels.children().length).toBe(2); expect($panels.children().length).toBe(2);
expect($panels.children().first().attr('id')).toBe('users'); expect($panels.children().first().attr('id')).toBe('users');
expect($panels.children().first().is(':visible')).toBe(true); expect($panels.children().first().is(':visible')).toBe(true);
expect($panels.children().last().attr('id')).toBe('chatrooms'); expect($panels.children().last().attr('id')).toBe('chatrooms');
expect($panels.children().last().is(':visible')).toBe(false); expect($panels.children().last().is(':visible')).toBe(false);
}, converse)); }));
describe("chatrooms panel", $.proxy(function () { describe("chatrooms panel", function () {
beforeEach($.proxy(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
}); test_utils.clearBrowserStorage();
waits(50);
runs(function () {
test_utils.openControlBox();
}); });
}, converse));
it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () { it("is opened by clicking the 'Chatrooms' tab", mock.initConverse(function (converse) {
var cbview = this.chatboxviews.get('controlbox'); test_utils.openControlBox();
var cbview = converse.chatboxviews.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs'); var $tabs = cbview.$el.find('#controlbox-tabs');
var $panels = cbview.$el.find('.controlbox-panes'); var $panels = cbview.$el.find('.controlbox-panes');
var $contacts = $panels.children().first(); var $contacts = $panels.children().first();
...@@ -1167,10 +1168,11 @@ ...@@ -1167,10 +1168,11 @@
expect($contacts.is(':visible')).toBe(false); expect($contacts.is(':visible')).toBe(false);
expect($chatrooms.is(':visible')).toBe(true); expect($chatrooms.is(':visible')).toBe(true);
expect(cbview.switchTab).toHaveBeenCalled(); expect(cbview.switchTab).toHaveBeenCalled();
}, converse)); }));
it("contains a form through which a new chatroom can be created", $.proxy(function () { it("contains a form through which a new chatroom can be created", mock.initConverse(function (converse) {
var roomspanel = this.chatboxviews.get('controlbox').roomspanel; test_utils.openControlBox();
var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name'); var $input = roomspanel.$el.find('input.new-chatroom-name');
var $nick = roomspanel.$el.find('input.new-chatroom-nick'); var $nick = roomspanel.$el.find('input.new-chatroom-nick');
var $server = roomspanel.$el.find('input.new-chatroom-server'); var $server = roomspanel.$el.find('input.new-chatroom-server');
...@@ -1190,13 +1192,14 @@ ...@@ -1190,13 +1192,14 @@
expect(roomspanel.createChatRoom).toHaveBeenCalled(); expect(roomspanel.createChatRoom).toHaveBeenCalled();
}); });
waits('250'); waits('250');
runs($.proxy(function () { runs(function () {
expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
}, converse)); });
}, converse)); }));
it("can list rooms publically available on the server", $.proxy(function () { it("can list rooms publically available on the server", mock.initConverse(function (converse) {
var panel = this.chatboxviews.get('controlbox').roomspanel; test_utils.openControlBox();
var panel = converse.chatboxviews.get('controlbox').roomspanel;
panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab
panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
// See: http://xmpp.org/extensions/xep-0045.html#disco-rooms // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
...@@ -1217,7 +1220,7 @@ ...@@ -1217,7 +1220,7 @@
expect(panel.$('#available-chatrooms').children('dt').length).toBe(1); expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost"); expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
expect(panel.$('#available-chatrooms').children('dd').length).toBe(4); expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
}, converse)); }));
}, converse)); });
}, converse, mock, test_utils)); });
})); }));
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
afterEach(function () { afterEach(function () {
converse_api.user.logout(); converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
}); });
......
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery", "jquery",
...@@ -12,15 +11,21 @@ ...@@ -12,15 +11,21 @@
"use strict"; "use strict";
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
describe("Service Discovery", $.proxy(function (mock, test_utils) { describe("Service Discovery", function () {
describe("Whenever converse.js discovers a new server feature", $.proxy(function (mock, test_utils) { describe("Whenever converse.js discovers a new server feature", function () {
it("emits the serviceDiscovered event", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("emits the serviceDiscovered event", mock.initConverse(function (converse) {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
converse.features.create({'var': Strophe.NS.MAM}); converse.features.create({'var': Strophe.NS.MAM});
expect(converse.emit).toHaveBeenCalled(); expect(converse.emit).toHaveBeenCalled();
expect(converse.emit.argsForCall[0][1].get('var')).toBe(Strophe.NS.MAM); expect(converse.emit.argsForCall[0][1].get('var')).toBe(Strophe.NS.MAM);
}));
});
}); });
}, converse, mock, test_utils));
}, converse, mock, test_utils));
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"mock",
"test_utils"
], function ($, mock, test_utils) {
return factory($, mock, test_utils);
}
);
} (this, function ($, mock, test_utils) {
return describe("The Converse Event Emitter", $.proxy(function(mock, test_utils) {
window.localStorage.clear();
window.sessionStorage.clear();
it("allows you to subscribe to emitted events", function () { return describe("The Converse Event Emitter", function() {
it("allows you to subscribe to emitted events", mock.initConverse(function (converse) {
this.callback = function () {}; this.callback = function () {};
spyOn(this, 'callback'); spyOn(this, 'callback');
converse.on('connected', this.callback); converse.on('connected', this.callback);
...@@ -23,9 +14,9 @@ ...@@ -23,9 +14,9 @@
expect(this.callback.callCount, 2); expect(this.callback.callCount, 2);
converse.emit('connected'); converse.emit('connected');
expect(this.callback.callCount, 3); expect(this.callback.callCount, 3);
}); }));
it("allows you to listen once for an emitted event", function () { it("allows you to listen once for an emitted event", mock.initConverse(function (converse) {
this.callback = function () {}; this.callback = function () {};
spyOn(this, 'callback'); spyOn(this, 'callback');
converse.once('connected', this.callback); converse.once('connected', this.callback);
...@@ -35,9 +26,9 @@ ...@@ -35,9 +26,9 @@
expect(this.callback.callCount, 1); expect(this.callback.callCount, 1);
converse.emit('connected'); converse.emit('connected');
expect(this.callback.callCount, 1); expect(this.callback.callCount, 1);
}); }));
it("allows you to stop listening or subscribing to an event", function () { it("allows you to stop listening or subscribing to an event", mock.initConverse(function (converse) {
this.callback = function () {}; this.callback = function () {};
this.anotherCallback = function () {}; this.anotherCallback = function () {};
this.neverCalled = function () {}; this.neverCalled = function () {};
...@@ -65,6 +56,6 @@ ...@@ -65,6 +56,6 @@
expect(this.callback.callCount, 1); expect(this.callback.callCount, 1);
expect(this.anotherCallback.callCount, 3); expect(this.anotherCallback.callCount, 3);
expect(this.neverCalled).not.toHaveBeenCalled(); expect(this.neverCalled).not.toHaveBeenCalled();
}));
}); });
}, converse, mock, test_utils));
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define([
"jquery", "jquery",
...@@ -13,12 +12,12 @@ ...@@ -13,12 +12,12 @@
describe("A headlines box", function () { describe("A headlines box", function () {
beforeEach(function () { afterEach(function () {
test_utils.openControlBox(); converse_api.user.logout();
test_utils.openContactsPanel(); test_utils.clearBrowserStorage();
}); });
it("will not open nor display non-headline messages", function () { it("will not open nor display non-headline messages", mock.initConverse(function (converse) {
/* XMPP spam message: /* XMPP spam message:
* *
* <message xmlns="jabber:client" * <message xmlns="jabber:client"
...@@ -29,6 +28,8 @@ ...@@ -29,6 +28,8 @@
* <body>SORRY FOR THIS ADVERT</body * <body>SORRY FOR THIS ADVERT</body
* </message * </message
*/ */
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
sinon.spy(utils, 'isHeadlineMessage'); sinon.spy(utils, 'isHeadlineMessage');
runs(function () { runs(function () {
var stanza = $msg({ var stanza = $msg({
...@@ -47,11 +48,10 @@ ...@@ -47,11 +48,10 @@
expect(utils.isHeadlineMessage.returned(false)).toBeTruthy(); expect(utils.isHeadlineMessage.returned(false)).toBeTruthy();
utils.isHeadlineMessage.restore(); utils.isHeadlineMessage.restore();
}); });
}));
});
it("will open and display headline messages", function () { it("will open and display headline messages", mock.initConverse(function (converse) {
/* /*
* <message from='notify.example.com' * <message from='notify.example.com'
* to='romeo@im.example.com' * to='romeo@im.example.com'
...@@ -66,6 +66,8 @@ ...@@ -66,6 +66,8 @@
* </x> * </x>
* </message> * </message>
*/ */
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
sinon.spy(utils, 'isHeadlineMessage'); sinon.spy(utils, 'isHeadlineMessage');
runs(function () { runs(function () {
var stanza = $msg({ var stanza = $msg({
...@@ -91,6 +93,6 @@ ...@@ -91,6 +93,6 @@
expect(utils.isHeadlineMessage.returned(true)).toBeTruthy(); expect(utils.isHeadlineMessage.returned(true)).toBeTruthy();
utils.isHeadlineMessage.restore(); // unwraps utils.isHeadlineMessage.restore(); // unwraps
}); });
}); }));
}); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"underscore",
"mock",
"test_utils"
], function ($, _, mock, test_utils) {
return factory($, _, mock, test_utils);
}
);
} (this, function ($, _, mock, test_utils) {
"use strict"; "use strict";
var _ = converse_api.env._;
var $ = converse_api.env.jQuery;
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
var $msg = converse_api.env.$msg; var $msg = converse_api.env.$msg;
var moment = converse_api.env.moment; var moment = converse_api.env.moment;
// See: https://xmpp.org/rfcs/rfc3921.html // See: https://xmpp.org/rfcs/rfc3921.html
describe("Message Archive Management", $.proxy(function (mock, test_utils) { describe("Message Archive Management", function () {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config // Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
describe("The archive.query API", $.proxy(function (mock, test_utils) { describe("The archive.query API", function () {
it("can be used to query for all archived messages", function () { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("can be used to query for all archived messages", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -36,9 +35,9 @@ ...@@ -36,9 +35,9 @@
var queryid = $(sent_stanza.toString()).find('query').attr('queryid'); var queryid = $(sent_stanza.toString()).find('query').attr('queryid');
expect(sent_stanza.toString()).toBe( expect(sent_stanza.toString()).toBe(
"<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'><query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'/></iq>"); "<iq type='set' xmlns='jabber:client' id='"+IQ_id+"'><query xmlns='urn:xmpp:mam:0' queryid='"+queryid+"'/></iq>");
}); }));
it("can be used to query for all messages to/from a particular JID", function () { it("can be used to query for all messages to/from a particular JID", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -64,9 +63,9 @@ ...@@ -64,9 +63,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("can be used to query for all messages in a certain timespan", function () { it("can be used to query for all messages in a certain timespan", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -101,15 +100,18 @@ ...@@ -101,15 +100,18 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("throws a TypeError if an invalid date is provided", function () { it("throws a TypeError if an invalid date is provided", mock.initConverse(function (converse) {
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM});
}
expect(_.partial(converse_api.archive.query, {'start': 'not a real date'})).toThrow( expect(_.partial(converse_api.archive.query, {'start': 'not a real date'})).toThrow(
new TypeError('archive.query: invalid date provided for: start') new TypeError('archive.query: invalid date provided for: start')
); );
}); }));
it("can be used to query for all messages after a certain time", function () { it("can be used to query for all messages after a certain time", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -136,9 +138,9 @@ ...@@ -136,9 +138,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("can be used to query for a limited set of results", function () { it("can be used to query for a limited set of results", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -168,9 +170,9 @@ ...@@ -168,9 +170,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("can be used to page through results", function () { it("can be used to page through results", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -205,9 +207,9 @@ ...@@ -205,9 +207,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("accepts \"before\" with an empty string as value to reverse the order", function () { it("accepts \"before\" with an empty string as value to reverse the order", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -234,9 +236,9 @@ ...@@ -234,9 +236,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("accepts a Strophe.RSM object for the query options", function () { it("accepts a Strophe.RSM object for the query options", mock.initConverse(function (converse) {
// Normally the user wouldn't manually make a Strophe.RSM object // Normally the user wouldn't manually make a Strophe.RSM object
// and pass it in. However, in the callback method an RSM object is // and pass it in. However, in the callback method an RSM object is
// returned which can be reused for easy paging. This test is // returned which can be reused for easy paging. This test is
...@@ -276,9 +278,9 @@ ...@@ -276,9 +278,9 @@
"</query>"+ "</query>"+
"</iq>" "</iq>"
); );
}); }));
it("accepts a callback function, which it passes the messages and a Strophe.RSM object", function () { it("accepts a callback function, which it passes the messages and a Strophe.RSM object", mock.initConverse(function (converse) {
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.features.create({'var': Strophe.NS.MAM}); converse.features.create({'var': Strophe.NS.MAM});
} }
...@@ -365,13 +367,19 @@ ...@@ -365,13 +367,19 @@
expect(args[1].count).toBe('16'); expect(args[1].count).toBe('16');
expect(args[1].first).toBe('23452-4534-1'); expect(args[1].first).toBe('23452-4534-1');
expect(args[1].last).toBe('390-2342-22'); expect(args[1].last).toBe('390-2342-22');
}));
}); });
}, converse, mock, test_utils)); describe("The default preference", function () {
describe("The default preference", $.proxy(function (mock, test_utils) { afterEach(function () {
converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage();
});
it("is set once server support for MAM has been confirmed", function () { it("is set once server support for MAM has been confirmed", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -443,7 +451,7 @@ ...@@ -443,7 +451,7 @@
// Restore // Restore
converse.message_archiving = 'never'; converse.message_archiving = 'never';
}));
});
}); });
}, converse, mock, test_utils));
}, converse, mock, test_utils));
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"underscore", var _ = converse_api.env._;
"mock",
"test_utils"
], function ($, _, mock, test_utils) {
return factory($, _, mock, test_utils);
}
);
} (this, function ($, _, mock, test_utils) {
var $msg = converse_api.env.$msg; var $msg = converse_api.env.$msg;
return describe("The Minimized Chats Widget", $.proxy(function(mock, test_utils) { describe("The Minimized Chats Widget", function () {
beforeEach(function () { afterEach(function () {
runs(function () { converse_api.user.logout();
test_utils.closeAllChatBoxes(); converse_api.listen.not();
test_utils.removeControlBox(); test_utils.clearBrowserStorage();
converse.roster.browserStorage._clear(); });
test_utils.initConverse();
test_utils.createContacts('current');
it("shows chats that have been minimized", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox(); test_utils.openControlBox();
test_utils.openContactsPanel(); test_utils.openContactsPanel(converse);
converse.minimized_chats.toggleview.model.browserStorage._clear(); converse.minimized_chats.toggleview.model.browserStorage._clear();
converse.minimized_chats.initToggle(); converse.minimized_chats.initToggle();
});
});
it("shows chats that have been minimized", $.proxy(function () {
var contact_jid, chatview; var contact_jid, chatview;
contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
chatview = converse.chatboxviews.get(contact_jid); chatview = converse.chatboxviews.get(contact_jid);
expect(chatview.model.get('minimized')).toBeFalsy(); expect(chatview.model.get('minimized')).toBeFalsy();
expect(this.minimized_chats.$el.is(':visible')).toBeFalsy(); expect(converse.minimized_chats.$el.is(':visible')).toBeFalsy();
chatview.$el.find('.toggle-chatbox-button').click(); chatview.$el.find('.toggle-chatbox-button').click();
expect(chatview.model.get('minimized')).toBeTruthy(); expect(chatview.model.get('minimized')).toBeTruthy();
expect(this.minimized_chats.$el.is(':visible')).toBeTruthy(); expect(converse.minimized_chats.$el.is(':visible')).toBeTruthy();
expect(this.minimized_chats.keys().length).toBe(1); expect(converse.minimized_chats.keys().length).toBe(1);
expect(this.minimized_chats.keys()[0]).toBe(contact_jid); expect(converse.minimized_chats.keys()[0]).toBe(contact_jid);
contact_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; contact_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
chatview = converse.chatboxviews.get(contact_jid); chatview = converse.chatboxviews.get(contact_jid);
expect(chatview.model.get('minimized')).toBeFalsy(); expect(chatview.model.get('minimized')).toBeFalsy();
chatview.$el.find('.toggle-chatbox-button').click(); chatview.$el.find('.toggle-chatbox-button').click();
expect(chatview.model.get('minimized')).toBeTruthy(); expect(chatview.model.get('minimized')).toBeTruthy();
expect(this.minimized_chats.$el.is(':visible')).toBeTruthy(); expect(converse.minimized_chats.$el.is(':visible')).toBeTruthy();
expect(this.minimized_chats.keys().length).toBe(2); expect(converse.minimized_chats.keys().length).toBe(2);
expect(_.contains(this.minimized_chats.keys(), contact_jid)).toBeTruthy(); expect(_.contains(converse.minimized_chats.keys(), contact_jid)).toBeTruthy();
}, converse)); }));
it("can be toggled to hide or show minimized chats", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
converse.minimized_chats.toggleview.model.browserStorage._clear();
converse.minimized_chats.initToggle();
it("can be toggled to hide or show minimized chats", $.proxy(function () {
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var chatview = converse.chatboxviews.get(contact_jid); var chatview = converse.chatboxviews.get(contact_jid);
expect(this.minimized_chats.$el.is(':visible')).toBeFalsy(); expect(converse.minimized_chats.$el.is(':visible')).toBeFalsy();
chatview.model.set({'minimized': true}); chatview.model.set({'minimized': true});
expect(this.minimized_chats.$el.is(':visible')).toBeTruthy(); expect(converse.minimized_chats.$el.is(':visible')).toBeTruthy();
expect(this.minimized_chats.keys().length).toBe(1); expect(converse.minimized_chats.keys().length).toBe(1);
expect(this.minimized_chats.keys()[0]).toBe(contact_jid); expect(converse.minimized_chats.keys()[0]).toBe(contact_jid);
expect(this.minimized_chats.$('.minimized-chats-flyout').is(':visible')).toBeTruthy(); expect(converse.minimized_chats.$('.minimized-chats-flyout').is(':visible')).toBeTruthy();
expect(this.minimized_chats.toggleview.model.get('collapsed')).toBeFalsy(); expect(converse.minimized_chats.toggleview.model.get('collapsed')).toBeFalsy();
this.minimized_chats.$('#toggle-minimized-chats').click(); converse.minimized_chats.$('#toggle-minimized-chats').click();
expect(this.minimized_chats.$('.minimized-chats-flyout').is(':visible')).toBeFalsy(); expect(converse.minimized_chats.$('.minimized-chats-flyout').is(':visible')).toBeFalsy();
expect(this.minimized_chats.toggleview.model.get('collapsed')).toBeTruthy(); expect(converse.minimized_chats.toggleview.model.get('collapsed')).toBeTruthy();
}, converse)); }));
it("shows the number messages received to minimized chats", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
converse.minimized_chats.toggleview.model.browserStorage._clear();
converse.minimized_chats.initToggle();
it("shows the number messages received to minimized chats", $.proxy(function () {
var i, contact_jid, chatview, msg; var i, contact_jid, chatview, msg;
this.minimized_chats.toggleview.model.set({'collapsed': true}); converse.minimized_chats.toggleview.model.set({'collapsed': true});
expect(this.minimized_chats.toggleview.$('.unread-message-count').is(':visible')).toBeFalsy(); expect(converse.minimized_chats.toggleview.$('.unread-message-count').is(':visible')).toBeFalsy();
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
contact_jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; contact_jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
chatview = converse.chatboxviews.get(contact_jid); chatview = converse.chatboxviews.get(contact_jid);
chatview.model.set({'minimized': true}); chatview.model.set({'minimized': true});
msg = $msg({ msg = $msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('body').t('This message is sent to a minimized chatbox').up() }).c('body').t('This message is sent to a minimized chatbox').up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
this.chatboxes.onMessage(msg); converse.chatboxes.onMessage(msg);
expect(this.minimized_chats.toggleview.$('.unread-message-count').is(':visible')).toBeTruthy(); expect(converse.minimized_chats.toggleview.$('.unread-message-count').is(':visible')).toBeTruthy();
expect(this.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i+1).toString()); expect(converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i+1).toString());
} }
// Chat state notifications don't increment the unread messages counter // Chat state notifications don't increment the unread messages counter
// <composing> state // <composing> state
this.chatboxes.onMessage($msg({ converse.chatboxes.onMessage($msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('composing', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()); }).c('composing', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
expect(this.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString()); expect(converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString());
// <paused> state // <paused> state
this.chatboxes.onMessage($msg({ converse.chatboxes.onMessage($msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('paused', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()); }).c('paused', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
expect(this.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString()); expect(converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString());
// <gone> state // <gone> state
this.chatboxes.onMessage($msg({ converse.chatboxes.onMessage($msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('gone', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()); }).c('gone', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
expect(this.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString()); expect(converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString());
// <inactive> state // <inactive> state
this.chatboxes.onMessage($msg({ converse.chatboxes.onMessage($msg({
from: contact_jid, from: contact_jid,
to: this.connection.jid, to: converse.connection.jid,
type: 'chat', type: 'chat',
id: (new Date()).getTime() id: (new Date()).getTime()
}).c('inactive', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree()); }).c('inactive', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree());
expect(this.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString()); expect(converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe((i).toString());
}, converse)); }));
});
}, converse, mock, test_utils));
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"underscore",
"mock",
"test_utils"
], function ($, _, mock, test_utils) {
return factory($, _, mock, test_utils);
}
);
} (this, function ($, _, mock, test_utils) {
"use strict"; "use strict";
var $msg = converse_api.env.$msg; var $msg = converse_api.env.$msg;
describe("Notifications", function () { describe("Notifications", function () {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config // Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
beforeEach(function () {
runs(function () {
test_utils.closeAllChatBoxes();
test_utils.createContacts('current');
});
});
describe("When show_desktop_notifications is set to true", function () { describe("When show_desktop_notifications is set to true", function () {
describe("And the desktop is not focused", function () { describe("And the desktop is not focused", function () {
describe("an HTML5 Notification", function () { describe("an HTML5 Notification", function () {
afterEach(function () {
converse_api.user.logout();
test_utils.clearBrowserStorage();
});
it("is shown when a new private message is received", function () { it("is shown when a new private message is received", mock.initConverse(function (converse) {
// TODO: not yet testing show_desktop_notifications setting // TODO: not yet testing show_desktop_notifications setting
test_utils.createContacts(converse, 'current');
spyOn(converse, 'showMessageNotification'); spyOn(converse, 'showMessageNotification');
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
...@@ -43,10 +33,11 @@ ...@@ -43,10 +33,11 @@
converse.chatboxes.onMessage(msg); // This will emit 'message' converse.chatboxes.onMessage(msg); // This will emit 'message'
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled(); expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showMessageNotification).toHaveBeenCalled(); expect(converse.showMessageNotification).toHaveBeenCalled();
}); }));
it("is shown when you are mentioned in a chat room", function () { it("is shown when you are mentioned in a chat room", mock.initConverse(function (converse) {
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); test_utils.createContacts(converse, 'current');
test_utils.openAndEnterChatRoom(converse, '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;
...@@ -75,39 +66,46 @@ ...@@ -75,39 +66,46 @@
if (no_notification) { if (no_notification) {
delete window.Notification; delete window.Notification;
} }
}); }));
it("is shown when a user changes their chat state", function () { it("is shown when a user changes their chat state", mock.initConverse(function (converse) {
// TODO: not yet testing show_desktop_notifications setting // TODO: not yet testing show_desktop_notifications setting
test_utils.createContacts(converse, 'current');
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
spyOn(converse, 'showChatStateNotification'); spyOn(converse, 'showChatStateNotification');
var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged' converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled(); expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showChatStateNotification).toHaveBeenCalled(); expect(converse.showChatStateNotification).toHaveBeenCalled();
}); }));
}); });
}); });
describe("When a new contact request is received", function () { describe("When a new contact request is received", function () {
it("an HTML5 Notification is received", function () { it("an HTML5 Notification is received", mock.initConverse(function (converse) {
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true); spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
spyOn(converse, 'showContactRequestNotification'); spyOn(converse, 'showContactRequestNotification');
converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'}); converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled(); expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
expect(converse.showContactRequestNotification).toHaveBeenCalled(); expect(converse.showContactRequestNotification).toHaveBeenCalled();
}); }));
}); });
}); });
describe("When play_sounds is set to true", function () { describe("When play_sounds is set to true", function () {
describe("A notification sound", function () { describe("A notification sound", function () {
it("is played when the current user is mentioned in a chat room", function () { afterEach(function () {
test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); converse_api.user.logout();
test_utils.clearBrowserStorage();
});
it("is played when the current user is mentioned in a chat room", mock.initConverse(function (converse) {
test_utils.createContacts(converse, 'current');
test_utils.openAndEnterChatRoom(converse, '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 = converse.chatboxviews.get('lounge@localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); } if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
var text = 'This message will play a sound because it mentions dummy'; var text = 'This message will play a sound because it mentions dummy';
var message = $msg({ var message = $msg({
...@@ -140,7 +138,7 @@ ...@@ -140,7 +138,7 @@
view.onChatRoomMessage(message.nodeTree); view.onChatRoomMessage(message.nodeTree);
expect(converse.playSoundNotification, 1); expect(converse.playSoundNotification, 1);
converse.play_sounds = false; converse.play_sounds = false;
}.bind(converse)); }));
}); });
}); });
}); });
......
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock","test_utils" ], factory);
"jquery", } (this, function (mock, test_utils) {
"mock", var $ = converse_api.env.jQuery;
"test_utils"
], function ($, mock, test_utils) {
return factory($, mock, test_utils);
}
);
} (this, function ($, mock, test_utils) {
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var b64_sha1 = converse_api.env.b64_sha1; var b64_sha1 = converse_api.env.b64_sha1;
return describe("The OTR module", function() { return describe("The OTR module", function() {
beforeEach(function () { afterEach(function () {
test_utils.openControlBox(); converse_api.user.logout();
test_utils.openContactsPanel(); test_utils.clearBrowserStorage();
test_utils.createContacts('current');
}); });
it("can store a session passphrase in session storage", function () { it("can store a session passphrase in session storage", mock.initConverse(function (converse) {
// With no prebind, the user's XMPP password is used and nothing is // With no prebind, the user's XMPP password is used and nothing is
// stored in session storage. // stored in session storage.
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
test_utils.createContacts(converse, 'current');
var auth = converse.authentication; var auth = converse.authentication;
var pass = converse.connection.pass; var pass = converse.connection.pass;
converse.authentication = "manual"; converse.authentication = "manual";
...@@ -38,13 +34,17 @@ ...@@ -38,13 +34,17 @@
// Clean up // Clean up
converse.authentication = auth; converse.authentication = auth;
converse.connection.pass = pass; converse.connection.pass = pass;
}); }));
it("will add processing hints to sent out encrypted <message> stanzas", mock.initConverse(function (converse) {
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
test_utils.createContacts(converse, 'current');
it("will add processing hints to sent out encrypted <message> stanzas", function () {
var UNVERIFIED = 1, UNENCRYPTED = 0; var UNVERIFIED = 1, UNENCRYPTED = 0;
var contact_name = mock.cur_names[0]; var contact_name = mock.cur_names[0];
var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost'; var contact_jid = contact_name.replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(converse, contact_jid);
var chatview = converse.chatboxviews.get(contact_jid); var chatview = converse.chatboxviews.get(contact_jid);
chatview.model.set('otr_status', UNVERIFIED); chatview.model.set('otr_status', UNVERIFIED);
var stanza = chatview.createMessageStanza(new converse.Message({ message: 'hello world'})); var stanza = chatview.createMessageStanza(new converse.Message({ message: 'hello world'}));
...@@ -54,11 +54,15 @@ ...@@ -54,11 +54,15 @@
expect($hints.get(1).tagName).toBe('no-permanent-store'); expect($hints.get(1).tagName).toBe('no-permanent-store');
expect($hints.get(2).tagName).toBe('no-copy'); expect($hints.get(2).tagName).toBe('no-copy');
chatview.model.set('otr_status', UNENCRYPTED); // Reset again to UNENCRYPTED chatview.model.set('otr_status', UNENCRYPTED); // Reset again to UNENCRYPTED
}); }));
describe("An OTR Chat Message", function () { describe("An OTR Chat Message", function () {
it("will not be carbon copied when it's sent out", function () { it("will not be carbon copied when it's sent out", mock.initConverse(function (converse) {
test_utils.openControlBox();
test_utils.openContactsPanel(converse);
test_utils.createContacts(converse, 'current');
var msgtext = "?OTR,1,3,?OTR:AAIDAAAAAAEAAAABAAAAwCQ8HKsag0y0DGKsneo0kzKu1ua5L93M4UKTkCf1I2kbm2RgS5kIxDTxrTj3wVRB+H5Si86E1fKtuBgsDf/bKkGTM0h/49vh5lOD9HkE8cnSrFEn5GN,"; var msgtext = "?OTR,1,3,?OTR:AAIDAAAAAAEAAAABAAAAwCQ8HKsag0y0DGKsneo0kzKu1ua5L93M4UKTkCf1I2kbm2RgS5kIxDTxrTj3wVRB+H5Si86E1fKtuBgsDf/bKkGTM0h/49vh5lOD9HkE8cnSrFEn5GN,";
var sender_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost'; var sender_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
converse_api.chats.open(sender_jid); converse_api.chats.open(sender_jid);
...@@ -71,7 +75,7 @@ ...@@ -71,7 +75,7 @@
expect($sent.find('private').length).toBe(1); expect($sent.find('private').length).toBe(1);
expect($sent.find('private').attr('xmlns')).toBe('urn:xmpp:carbons:2'); expect($sent.find('private').attr('xmlns')).toBe('urn:xmpp:carbons:2');
chatbox.set('otr_status', 0); // Reset again to UNENCRYPTED chatbox.set('otr_status', 0); // Reset again to UNENCRYPTED
}); }));
}); });
}); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils", "converse-ping"], factory);
"jquery", } (this, function (mock, test_utils) {
"converse-ping"
], function ($) {
return factory($);
}
);
} (this, function ($) {
"use strict"; "use strict";
describe("XMPP Ping", function () { describe("XMPP Ping", function () {
describe("Ping and pong handlers", function () { describe("Ping and pong handlers", function () {
it("are registered when converse.js is connected", function () { afterEach(function () {
converse_api.user.logout();
test_utils.clearBrowserStorage();
});
it("are registered when converse.js is connected", mock.initConverse(function (converse) {
spyOn(converse, 'registerPingHandler').andCallThrough(); spyOn(converse, 'registerPingHandler').andCallThrough();
spyOn(converse, 'registerPongHandler').andCallThrough(); spyOn(converse, 'registerPongHandler').andCallThrough();
converse.emit('connected'); converse.emit('connected');
expect(converse.registerPingHandler).toHaveBeenCalled(); expect(converse.registerPingHandler).toHaveBeenCalled();
expect(converse.registerPongHandler).toHaveBeenCalled(); expect(converse.registerPongHandler).toHaveBeenCalled();
}); }));
it("are registered when converse.js reconnected", function () { it("are registered when converse.js reconnected", mock.initConverse(function (converse) {
spyOn(converse, 'registerPingHandler').andCallThrough(); spyOn(converse, 'registerPingHandler').andCallThrough();
spyOn(converse, 'registerPongHandler').andCallThrough(); spyOn(converse, 'registerPongHandler').andCallThrough();
converse.emit('reconnected'); converse.emit('reconnected');
expect(converse.registerPingHandler).toHaveBeenCalled(); expect(converse.registerPingHandler).toHaveBeenCalled();
expect(converse.registerPongHandler).toHaveBeenCalled(); expect(converse.registerPongHandler).toHaveBeenCalled();
}); }));
}); });
describe("An IQ stanza", function () { describe("An IQ stanza", function () {
it("is sent out when converse.js pings a server", function () { afterEach(function () {
converse_api.user.logout();
test_utils.clearBrowserStorage();
});
it("is sent out when converse.js pings a server", mock.initConverse(function (converse) {
var sent_stanza, IQ_id; var sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
...@@ -42,7 +45,7 @@ ...@@ -42,7 +45,7 @@
"<iq type='get' to='localhost' id='"+IQ_id+"' xmlns='jabber:client'>"+ "<iq type='get' to='localhost' id='"+IQ_id+"' xmlns='jabber:client'>"+
"<ping xmlns='urn:xmpp:ping'/>"+ "<ping xmlns='urn:xmpp:ping'/>"+
"</iq>"); "</iq>");
}); }));
}); });
}); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"underscore", var _ = converse_api.env._;
"mock",
"test_utils"
], function ($, _, mock, test_utils) {
return factory($, _, mock, test_utils);
}
);
} (this, function ($, _, mock, test_utils) {
var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
describe("Profiling", function() { describe("Profiling", function() {
beforeEach(function() { afterEach(function () {
converse_api.user.logout();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
converse.rosterview.model.reset();
converse.connection._changeConnectStatus(Strophe.Status.CONNECTED);
}); });
xit("adds hundreds of contacts to the roster", $.proxy(function() { xit("adds hundreds of contacts to the roster", mock.initConverse(function(converse) {
converse.roster_groups = false; converse.roster_groups = false;
expect(this.roster.pluck('jid').length).toBe(0); expect(this.roster.pluck('jid').length).toBe(0);
var stanza = $iq({ var stanza = $iq({
...@@ -41,9 +31,9 @@ ...@@ -41,9 +31,9 @@
}); });
this.roster.onReceivedFromServer(stanza.tree()); this.roster.onReceivedFromServer(stanza.tree());
// expect(this.roster.pluck('jid').length).toBe(400); // expect(this.roster.pluck('jid').length).toBe(400);
}, converse)); }));
xit("adds hundreds of contacts to the roster, with roster groups", $.proxy(function() { xit("adds hundreds of contacts to the roster, with roster groups", mock.initConverse(function(converse) {
// converse.show_only_online_users = true; // converse.show_only_online_users = true;
converse.roster_groups = true; converse.roster_groups = true;
expect(this.roster.pluck('jid').length).toBe(0); expect(this.roster.pluck('jid').length).toBe(0);
...@@ -65,6 +55,6 @@ ...@@ -65,6 +55,6 @@
}); });
this.roster.onReceivedFromServer(stanza.tree()); this.roster.onReceivedFromServer(stanza.tree());
//expect(this.roster.pluck('jid').length).toBe(400); //expect(this.roster.pluck('jid').length).toBe(400);
}, converse)); }));
}); });
})); }));
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
afterEach(function () { afterEach(function () {
converse_api.user.logout(); converse_api.user.logout();
converse_api.listen.not();
test_utils.clearBrowserStorage(); test_utils.clearBrowserStorage();
}); });
......
/*global converse */
(function (root, factory) { (function (root, factory) {
define(["jquery", "mock", "test_utils"], factory); define(["mock", "test_utils"], factory);
} (this, function ($, mock, test_utils) { } (this, function (mock, test_utils) {
var $ = converse_api.env.jQuery;
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
var original_connection = converse.connection;
describe("The Registration Panel", function () { describe("The Registration Panel", function () {
beforeEach(function () {
test_utils.closeControlBox();
converse._tearDown();
converse.initialized_plugins = [];
});
afterEach(function () { afterEach(function () {
test_utils.closeControlBox(); converse_api.user.logout();
converse.connection = original_connection; converse_api.listen.not();
converse._initialize(); test_utils.clearBrowserStorage();
}); });
it("is not available unless allow_registration=true", function () { it("is not available unless allow_registration=true", mock.initConverse(function (converse) {
converse.initialize({ runs(test_utils.openControlBox);
i18n: window.locales.en, waits(50);
bosh_service_url: 'localhost', runs(function () {
allow_registration: true,
auto_subscribe: false,
animate: false,
connection: mock.mock_connection,
no_trimming: true,
debug: false
}, function (converse) {
test_utils.closeControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
expect(cbview.$('#controlbox-tabs li').length).toBe(1); expect(cbview.$('#controlbox-tabs li').length).toBe(1);
expect(cbview.$('#controlbox-tabs li').text().trim()).toBe("Sign in"); expect(cbview.$('#controlbox-tabs li').text().trim()).toBe("Sign in");
cbview = converse.chatboxviews.get('controlbox');
expect(cbview.$el.find('#controlbox-tabs li').length).toBe(2);
expect(cbview.$('#controlbox-tabs li').first().text().trim()).toBe("Sign in");
expect(cbview.$('#controlbox-tabs li').last().text().trim()).toBe("Register");
});
}); });
}, { auto_login: false,
allow_registration: false,
}));
it("can be opened by clicking on the registration tab", function () { it("can be opened by clicking on the registration tab", mock.initConverse(function (converse) {
converse.initialize({
i18n: window.locales.en,
bosh_service_url: 'localhost',
allow_registration: true,
auto_subscribe: false,
animate: false,
connection: mock.mock_connection,
no_trimming: true,
debug: false
}, function (converse) {
test_utils.closeControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
runs(test_utils.openControlBox);
waits(50);
runs(function () {
var $tabs = cbview.$('#controlbox-tabs'); var $tabs = cbview.$('#controlbox-tabs');
var $panels = cbview.$('.controlbox-panes'); var $panels = cbview.$('.controlbox-panes');
var $login = $panels.children().first(); var $login = $panels.children().first();
...@@ -68,20 +43,11 @@ ...@@ -68,20 +43,11 @@
expect($registration.is(':visible')).toBe(true); expect($registration.is(':visible')).toBe(true);
expect(cbview.switchTab).toHaveBeenCalled(); expect(cbview.switchTab).toHaveBeenCalled();
}); });
}); }, { auto_login: false,
it("allows the user to choose an XMPP provider's domain", function () {
converse.initialize({
i18n: window.locales.en,
bosh_service_url: 'localhost',
allow_registration: true, allow_registration: true,
auto_subscribe: false, }));
animate: false,
connection: mock.mock_connection, it("allows the user to choose an XMPP provider's domain", mock.initConverse(function (converse) {
no_trimming: true,
debug: false
}, function (converse) {
test_utils.closeControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
var registerview = cbview.registerpanel; var registerview = cbview.registerpanel;
spyOn(registerview, 'onProviderChosen').andCallThrough(); spyOn(registerview, 'onProviderChosen').andCallThrough();
...@@ -103,20 +69,11 @@ ...@@ -103,20 +69,11 @@
$form.find('input[type=submit]').click(); $form.find('input[type=submit]').click();
expect(registerview.onProviderChosen).toHaveBeenCalled(); expect(registerview.onProviderChosen).toHaveBeenCalled();
expect(converse.connection.connect).toHaveBeenCalled(); expect(converse.connection.connect).toHaveBeenCalled();
}); }, { auto_login: false,
});
it("will render a registration form as received from the XMPP provider", function () {
converse.initialize({
i18n: window.locales.en,
bosh_service_url: 'localhost',
allow_registration: true, allow_registration: true,
auto_subscribe: false, }));
animate: false,
connection: mock.mock_connection, it("will render a registration form as received from the XMPP provider", mock.initConverse(function (converse) {
no_trimming: true,
debug: false
}, function (converse) {
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab
var registerview = converse.chatboxviews.get('controlbox').registerpanel; var registerview = converse.chatboxviews.get('controlbox').registerpanel;
...@@ -161,21 +118,11 @@ ...@@ -161,21 +118,11 @@
expect(registerview.$('input').length).toBe(5); expect(registerview.$('input').length).toBe(5);
expect(registerview.$('input[type=submit]').length).toBe(1); expect(registerview.$('input[type=submit]').length).toBe(1);
expect(registerview.$('input[type=button]').length).toBe(1); expect(registerview.$('input[type=button]').length).toBe(1);
}); }, { auto_login: false,
});
it("will set form_type to legacy and submit it as legacy", function () {
converse.initialize({
i18n: window.locales.en,
bosh_service_url: 'localhost',
allow_registration: true, allow_registration: true,
auto_subscribe: false, }));
animate: false,
connection: mock.mock_connection, it("will set form_type to legacy and submit it as legacy", mock.initConverse(function (converse) {
no_trimming: true,
debug: false
}, function (converse) {
test_utils.closeControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab
var registerview = converse.chatboxviews.get('controlbox').registerpanel; var registerview = converse.chatboxviews.get('controlbox').registerpanel;
...@@ -220,21 +167,11 @@ ...@@ -220,21 +167,11 @@
var $stanza = $(converse.connection.send.argsForCall[0][0].tree()); var $stanza = $(converse.connection.send.argsForCall[0][0].tree());
expect($stanza.children('query').children().length).toBe(3); expect($stanza.children('query').children().length).toBe(3);
expect($stanza.children('query').children()[0].tagName).toBe('username'); expect($stanza.children('query').children()[0].tagName).toBe('username');
}); }, { auto_login: false,
});
it("will set form_type to xform and submit it as xform", function () {
converse.initialize({
i18n: window.locales.en,
bosh_service_url: 'localhost',
allow_registration: true, allow_registration: true,
auto_subscribe: false, }));
animate: false,
connection: mock.mock_connection, it("will set form_type to xform and submit it as xform", mock.initConverse(function (converse) {
no_trimming: true,
debug: false
}, function (converse) {
test_utils.closeControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab cbview.$('#controlbox-tabs').find('li').last().find('a').click(); // Click the Register tab
var registerview = converse.chatboxviews.get('controlbox').registerpanel; var registerview = converse.chatboxviews.get('controlbox').registerpanel;
...@@ -282,7 +219,8 @@ ...@@ -282,7 +219,8 @@
expect($stanza.children('query').children().length).toBe(1); expect($stanza.children('query').children().length).toBe(1);
expect($stanza.children('query').children().children().length).toBe(3); expect($stanza.children('query').children().children().length).toBe(3);
expect($stanza.children('query').children().children()[0].tagName).toBe('field'); expect($stanza.children('query').children().children()[0].tagName).toBe('field');
}); }, { auto_login: false,
}); allow_registration: true,
}));
}); });
})); }));
/*global converse */
(function (root, factory) { (function (root, factory) {
define([ define(["mock", "test_utils"], factory);
"jquery", } (this, function (mock, test_utils) {
"mock", var $ = converse_api.env.jQuery;
"test_utils"
], function ($, mock, test_utils) { return describe("The XMPPStatus model", function() {
return factory($, mock, test_utils); afterEach(function () {
} converse_api.user.logout();
); test_utils.clearBrowserStorage();
} (this, function ($, mock, test_utils) { });
return describe("The XMPPStatus model", $.proxy(function(mock, test_utils) {
beforeEach($.proxy(function () { it("won't send <show>online when setting a custom status message", mock.initConverse(function (converse) {
window.localStorage.clear(); converse.xmppstatus.save({'status': 'online'});
window.sessionStorage.clear(); spyOn(converse.xmppstatus, 'setStatusMessage').andCallThrough();
}, converse));
it("won't send <show>online when setting a custom status message", $.proxy(function () {
this.xmppstatus.save({'status': 'online'});
spyOn(this.xmppstatus, 'setStatusMessage').andCallThrough();
spyOn(converse.connection, 'send'); spyOn(converse.connection, 'send');
this.xmppstatus.setStatusMessage("I'm also happy!"); converse.xmppstatus.setStatusMessage("I'm also happy!");
runs (function () { runs (function () {
expect(converse.connection.send).toHaveBeenCalled(); expect(converse.connection.send).toHaveBeenCalled();
var $stanza = $(converse.connection.send.argsForCall[0][0].tree()); var $stanza = $(converse.connection.send.argsForCall[0][0].tree());
expect($stanza.children().length).toBe(1); expect($stanza.children().length).toBe(1);
expect($stanza.children('show').length).toBe(0); expect($stanza.children('show').length).toBe(0);
}); });
}, converse)); }));
}, converse, mock, test_utils)); });
})); }));
...@@ -164,11 +164,13 @@ ...@@ -164,11 +164,13 @@
callback = options; callback = options;
errback = callback; errback = callback;
} }
/*
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.log('This server does not support XEP-0313, Message Archive Management'); converse.log('This server does not support XEP-0313, Message Archive Management');
errback(null); errback(null);
return; return;
} }
*/
var queryid = converse.connection.getUniqueId(); var queryid = converse.connection.getUniqueId();
var attrs = {'type':'set'}; var attrs = {'type':'set'};
if (typeof options !== "undefined" && options.groupchat) { if (typeof options !== "undefined" && options.groupchat) {
......
...@@ -10,6 +10,14 @@ ...@@ -10,6 +10,14 @@
<link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" /> <link type="text/css" rel="stylesheet" media="screen" href="css/converse.css" />
<script src="config.js"></script> <script src="config.js"></script>
<script data-main="tests/main" src="node_modules/requirejs/require.js"></script> <script data-main="tests/main" src="node_modules/requirejs/require.js"></script>
<style>
body {
background-color: lightgoldenrodyellow;
}
h2 {
color: darkgray;
}
</style>
</head> </head>
<body> <body>
......
...@@ -50,13 +50,10 @@ require([ ...@@ -50,13 +50,10 @@ require([
//"spec/transcripts", //"spec/transcripts",
"spec/utils", "spec/utils",
"spec/converse", "spec/converse",
/*
"spec/bookmarks", "spec/bookmarks",
"spec/headline", "spec/headline",
"spec/disco", "spec/disco",
*/
"spec/protocol", "spec/protocol",
/*
"spec/mam", "spec/mam",
"spec/otr", "spec/otr",
"spec/eventemitter", "spec/eventemitter",
...@@ -68,8 +65,7 @@ require([ ...@@ -68,8 +65,7 @@ require([
"spec/profiling", "spec/profiling",
"spec/ping", "spec/ping",
"spec/register", "spec/register",
"spec/xmppstatus", "spec/xmppstatus"
*/
], function () { ], function () {
// Jasmine stuff // Jasmine stuff
var jasmineEnv = jasmine.getEnv(); var jasmineEnv = jasmine.getEnv();
......
...@@ -75,9 +75,9 @@ ...@@ -75,9 +75,9 @@
}; };
}(); }();
mock.initConverse = function (func) { mock.initConverse = function (func, settings) {
return function () { return function () {
var converse = converse_api.initialize({ var converse = converse_api.initialize(_.extend({
i18n: window.locales.en, i18n: window.locales.en,
auto_subscribe: false, auto_subscribe: false,
bosh_service_url: 'localhost', bosh_service_url: 'localhost',
...@@ -86,9 +86,8 @@ ...@@ -86,9 +86,8 @@
no_trimming: true, no_trimming: true,
auto_login: true, auto_login: true,
jid: 'dummy@localhost', jid: 'dummy@localhost',
password: 'secret', password: 'secret'
debug: true }, settings || {}));
});
converse.ChatBoxViews.prototype.trimChat = function () {}; converse.ChatBoxViews.prototype.trimChat = function () {};
return func(converse); return func(converse);
}; };
......
...@@ -79,14 +79,14 @@ ...@@ -79,14 +79,14 @@
$tabs.find('li').first().find('a').click(); $tabs.find('li').first().find('a').click();
}; };
utils.openRoomsPanel = function () { utils.openRoomsPanel = function (converse) {
utils.openControlBox(); utils.openControlBox();
var cbview = converse.chatboxviews.get('controlbox'); var cbview = converse.chatboxviews.get('controlbox');
var $tabs = cbview.$el.find('#controlbox-tabs'); var $tabs = cbview.$el.find('#controlbox-tabs');
$tabs.find('li').last().find('a').click(); $tabs.find('li').last().find('a').click();
}; };
utils.openChatBoxes = function (amount) { utils.openChatBoxes = function (converse, amount) {
var i = 0, jid, views = []; var i = 0, jid, views = [];
for (i; i<amount; i++) { for (i; i<amount; i++) {
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
...@@ -99,25 +99,25 @@ ...@@ -99,25 +99,25 @@
return converse.roster.get(jid).trigger("open"); return converse.roster.get(jid).trigger("open");
}; };
utils.openChatRoom = function (room, server, nick) { utils.openChatRoom = function (converse, room, server, nick) {
// Open a new chatroom // Open a new chatroom
this.openControlBox(); this.openControlBox(converse);
this.openRoomsPanel(); this.openRoomsPanel(converse);
var roomspanel = converse.chatboxviews.get('controlbox').roomspanel; var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
roomspanel.$el.find('input.new-chatroom-name').val(room); roomspanel.$el.find('input.new-chatroom-name').val(room);
roomspanel.$el.find('input.new-chatroom-nick').val(nick); roomspanel.$el.find('input.new-chatroom-nick').val(nick);
roomspanel.$el.find('input.new-chatroom-server').val(server); roomspanel.$el.find('input.new-chatroom-server').val(server);
roomspanel.$el.find('form').submit(); roomspanel.$el.find('form').submit();
this.closeControlBox(); this.closeControlBox(converse);
}; };
utils.openAndEnterChatRoom = function (room, server, nick) { utils.openAndEnterChatRoom = function (converse, room, server, nick) {
var IQ_id, sendIQ = converse.connection.sendIQ; var IQ_id, sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
IQ_id = sendIQ.bind(this)(iq, callback, errback); IQ_id = sendIQ.bind(this)(iq, callback, errback);
}); });
utils.openChatRoom(room, server); utils.openChatRoom(converse, room, server);
var view = converse.chatboxviews.get(room+'@'+server); var view = converse.chatboxviews.get(room+'@'+server);
// The XMPP server returns the reserved nick for this user. // The XMPP server returns the reserved nick for this user.
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
return this; return this;
}; };
utils.clearChatBoxMessages = function (jid) { utils.clearChatBoxMessages = function (converse, jid) {
var view = converse.chatboxviews.get(jid); var view = converse.chatboxviews.get(jid);
view.$el.find('.chat-content').empty(); view.$el.find('.chat-content').empty();
view.model.messages.reset(); view.model.messages.reset();
...@@ -190,7 +190,9 @@ ...@@ -190,7 +190,9 @@
requesting = false; requesting = false;
ask = null; ask = null;
} else if (type === 'all') { } else if (type === 'all') {
this.createContacts('current').createContacts('requesting').createContacts('pending'); this.createContacts(converse, 'current')
.createContacts(converse, 'requesting')
.createContacts(converse, 'pending');
return this; return this;
} else { } else {
throw "Need to specify the type of contact to create"; throw "Need to specify the type of contact to create";
...@@ -214,7 +216,7 @@ ...@@ -214,7 +216,7 @@
return this; return this;
}; };
utils.createGroupedContacts = function () { utils.createGroupedContacts = function (converse) {
/* Create grouped contacts /* Create grouped contacts
*/ */
var i=0, j=0; var i=0, j=0;
......
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