Commit 4186bf3c authored by JC Brand's avatar JC Brand

Throw event onRosterViewUpdated whenever the roster HTML changes. Fixes #101

parent 8633d237
...@@ -3041,6 +3041,7 @@ ...@@ -3041,6 +3041,7 @@
if (!$count.is(':visible')) { if (!$count.is(':visible')) {
$count.show(); $count.show();
} }
converse.emit('onRosterViewUpdated');
return this; return this;
}, },
......
...@@ -624,6 +624,12 @@ Here are the different events that are emitted: ...@@ -624,6 +624,12 @@ Here are the different events that are emitted:
Triggered when the roster is updated. Triggered when the roster is updated.
* **onRosterViewUpdated**
``converse.on('onRosterViewUpdated', function (items) { ... });``
Triggered whenever the roster view (i.e. the rendered HTML) has changed.
* **onChatBoxFocused** * **onChatBoxFocused**
``converse.on('onChatBoxFocused', function (chatbox) { ... });`` ``converse.on('onChatBoxFocused', function (chatbox) { ... });``
......
...@@ -93,9 +93,8 @@ ...@@ -93,9 +93,8 @@
describe("The Contacts Roster", $.proxy(function (utils, mock) { describe("The Contacts Roster", $.proxy(function (utils, mock) {
describe("Pending Contacts", $.proxy(function () { describe("Pending Contacts", $.proxy(function () {
beforeEach(function () { beforeEach(function () {
if (!$("div#controlbox").is(':visible')) { utils.openControlBox();
$('.toggle-online-users').click(); utils.openContactsPanel();
}
}); });
it("do not have a heading if there aren't any", $.proxy(function () { it("do not have a heading if there aren't any", $.proxy(function () {
...@@ -103,25 +102,32 @@ ...@@ -103,25 +102,32 @@
}, converse)); }, converse));
it("can be added to the roster", $.proxy(function () { it("can be added to the roster", $.proxy(function () {
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
this.roster.create({ runs($.proxy(function () {
jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost', this.roster.create({
subscription: 'none', jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
ask: 'subscribe', subscription: 'none',
fullname: mock.pend_names[0], ask: 'subscribe',
is_last: true fullname: mock.pend_names[0],
}); is_last: true
expect(this.rosterview.$el.is(':visible')).toEqual(true); });
expect(this.rosterview.render).toHaveBeenCalled(); }, converse));
waits(300);
runs($.proxy(function () {
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
expect(this.rosterview.$el.is(':visible')).toEqual(true);
expect(this.rosterview.render).toHaveBeenCalled();
}, converse));
}, converse)); }, converse));
it("can be removed by the user", $.proxy(function () { it("can be removed by the user", $.proxy(function () {
var view = _.toArray(this.rosterview.rosteritemviews).pop(); var view = _.toArray(this.rosterview.rosteritemviews).pop();
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(converse, 'emit');
spyOn(this.connection.roster, 'remove').andCallThrough(); spyOn(this.connection.roster, 'remove').andCallThrough();
spyOn(this.connection.roster, 'unauthorize'); spyOn(this.connection.roster, 'unauthorize');
spyOn(this.rosterview.model, 'remove').andCallThrough(); spyOn(this.rosterview.model, 'remove').andCallThrough();
//spyOn(view, 'removeContact').andCallThrough();
runs($.proxy(function () { runs($.proxy(function () {
view.$el.find('.remove-xmpp-contact').click(); view.$el.find('.remove-xmpp-contact').click();
...@@ -129,12 +135,12 @@ ...@@ -129,12 +135,12 @@
waits(500); waits(500);
runs($.proxy(function () { runs($.proxy(function () {
expect(window.confirm).toHaveBeenCalled(); expect(window.confirm).toHaveBeenCalled();
//expect(view.removeContact).toHaveBeenCalled();
expect(this.connection.roster.remove).toHaveBeenCalled(); expect(this.connection.roster.remove).toHaveBeenCalled();
expect(this.connection.roster.unauthorize).toHaveBeenCalled(); expect(this.connection.roster.unauthorize).toHaveBeenCalled();
expect(this.rosterview.model.remove).toHaveBeenCalled(); expect(this.rosterview.model.remove).toHaveBeenCalled();
// The element must now be detached from the DOM. // The element must now be detached from the DOM.
expect(view.$el.closest('html').length).toBeFalsy(); expect(view.$el.closest('html').length).toBeFalsy();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
}, converse)); }, converse));
}, converse)); }, converse));
...@@ -144,6 +150,7 @@ ...@@ -144,6 +150,7 @@
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", $.proxy(function () {
var i, t, is_last; var i, t, is_last;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
for (i=0; i<mock.pend_names.length; i++) { for (i=0; i<mock.pend_names.length; i++) {
is_last = i===(mock.pend_names.length-1); is_last = i===(mock.pend_names.length-1);
...@@ -155,6 +162,7 @@ ...@@ -155,6 +162,7 @@
is_last: is_last is_last: is_last
}); });
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text(); t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').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(''));
...@@ -170,6 +178,7 @@ ...@@ -170,6 +178,7 @@
describe("Existing Contacts", $.proxy(function () { describe("Existing Contacts", $.proxy(function () {
beforeEach($.proxy(function () { beforeEach($.proxy(function () {
utils.openControlBox(); utils.openControlBox();
utils.openContactsPanel();
}, converse)); }, converse));
it("do not have a heading if there aren't any", $.proxy(function () { it("do not have a heading if there aren't any", $.proxy(function () {
...@@ -178,6 +187,7 @@ ...@@ -178,6 +187,7 @@
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", $.proxy(function () {
var i, t; var i, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
for (i=0; i<mock.cur_names.length; i++) { for (i=0; i<mock.cur_names.length; i++) {
this.roster.create({ this.roster.create({
...@@ -188,6 +198,7 @@ ...@@ -188,6 +198,7 @@
is_last: i===(mock.cur_names.length-1) is_last: i===(mock.cur_names.length-1)
}); });
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').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(''));
...@@ -200,6 +211,7 @@ ...@@ -200,6 +211,7 @@
it("can change their status to online and be sorted alphabetically", $.proxy(function () { it("can change their status to online and be sorted alphabetically", $.proxy(function () {
var item, view, jid, t; var item, view, jid, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
...@@ -209,7 +221,7 @@ ...@@ -209,7 +221,7 @@
item.set('chat_status', 'online'); item.set('chat_status', 'online');
expect(view.render).toHaveBeenCalled(); expect(view.render).toHaveBeenCalled();
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').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(''));
...@@ -218,6 +230,7 @@ ...@@ -218,6 +230,7 @@
it("can change their status to busy and be sorted alphabetically", $.proxy(function () { it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
var item, view, jid, t; var item, view, jid, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
for (i=3; i<6; i++) { for (i=3; i<6; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
...@@ -227,6 +240,7 @@ ...@@ -227,6 +240,7 @@
item.set('chat_status', 'dnd'); item.set('chat_status', 'dnd');
expect(view.render).toHaveBeenCalled(); expect(view.render).toHaveBeenCalled();
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(3,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(3,i+1).sort().join(''));
...@@ -235,6 +249,7 @@ ...@@ -235,6 +249,7 @@
it("can change their status to away and be sorted alphabetically", $.proxy(function () { it("can change their status to away and be sorted alphabetically", $.proxy(function () {
var item, view, jid, t; var item, view, jid, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
for (i=6; i<9; i++) { for (i=6; i<9; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
...@@ -244,7 +259,7 @@ ...@@ -244,7 +259,7 @@
item.set('chat_status', 'away'); item.set('chat_status', 'away');
expect(view.render).toHaveBeenCalled(); expect(view.render).toHaveBeenCalled();
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(6,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(6,i+1).sort().join(''));
...@@ -253,6 +268,7 @@ ...@@ -253,6 +268,7 @@
it("can change their status to xa and be sorted alphabetically", $.proxy(function () { it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
var item, view, jid, t; var item, view, jid, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
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';
...@@ -262,7 +278,7 @@ ...@@ -262,7 +278,7 @@
item.set('chat_status', 'xa'); item.set('chat_status', 'xa');
expect(view.render).toHaveBeenCalled(); expect(view.render).toHaveBeenCalled();
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(9,i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(9,i+1).sort().join(''));
...@@ -271,6 +287,7 @@ ...@@ -271,6 +287,7 @@
it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () { it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
var item, view, jid, t; var item, view, jid, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
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';
...@@ -280,7 +297,7 @@ ...@@ -280,7 +297,7 @@
item.set('chat_status', 'unavailable'); item.set('chat_status', 'unavailable');
expect(view.render).toHaveBeenCalled(); expect(view.render).toHaveBeenCalled();
expect(this.rosterview.render).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// Check that they are sorted alphabetically // Check that they are sorted alphabetically
t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text(); t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
expect(t).toEqual(mock.cur_names.slice(12, i+1).sort().join('')); expect(t).toEqual(mock.cur_names.slice(12, i+1).sort().join(''));
...@@ -320,6 +337,7 @@ ...@@ -320,6 +337,7 @@
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", $.proxy(function () {
var i, t; var i, t;
spyOn(converse, 'emit');
spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.rosterview, 'render').andCallThrough();
spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough(); spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
for (i=0; i<mock.req_names.length; i++) { for (i=0; i<mock.req_names.length; i++) {
...@@ -338,6 +356,7 @@ ...@@ -338,6 +356,7 @@
// When a requesting contact is added, the controlbox must // When a requesting contact is added, the controlbox must
// be opened. // be opened.
expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled(); expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
} }
}, converse)); }, converse));
...@@ -363,6 +382,7 @@ ...@@ -363,6 +382,7 @@
it("can have their requests denied by the user", $.proxy(function () { it("can have their requests denied by the user", $.proxy(function () {
var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.rosteritemviews[jid]; var view = this.rosterview.rosteritemviews[jid];
spyOn(converse, 'emit');
spyOn(this.connection.roster, 'unauthorize'); spyOn(this.connection.roster, 'unauthorize');
spyOn(this.rosterview, 'removeRosterItemView').andCallThrough(); spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
spyOn(view, 'declineRequest').andCallThrough(); spyOn(view, 'declineRequest').andCallThrough();
...@@ -372,6 +392,7 @@ ...@@ -372,6 +392,7 @@
expect(view.declineRequest).toHaveBeenCalled(); expect(view.declineRequest).toHaveBeenCalled();
expect(this.rosterview.removeRosterItemView).toHaveBeenCalled(); expect(this.rosterview.removeRosterItemView).toHaveBeenCalled();
expect(this.connection.roster.unauthorize).toHaveBeenCalled(); expect(this.connection.roster.unauthorize).toHaveBeenCalled();
expect(converse.emit).toHaveBeenCalledWith('onRosterViewUpdated');
// There should now be one less contact // There should now be one less contact
expect(this.roster.length).toEqual(mock.num_contacts-1); expect(this.roster.length).toEqual(mock.num_contacts-1);
}, converse)); }, converse));
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
); );
} (this, function (mock, utils) { } (this, function (mock, utils) {
return describe("Converse", $.proxy(function(mock, utils) { return describe("Converse", $.proxy(function(mock, utils) {
window.localStorage.clear();
it("allows you to subscribe to emitted events", function () { it("allows you to subscribe to emitted events", function () {
this.callback = function () {}; this.callback = function () {};
spyOn(this, 'callback'); spyOn(this, 'callback');
......
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