Add closure get/set methods for rosteritem views

This commit is contained in:
JC Brand 2014-03-04 15:48:16 +02:00
parent d20ca10638
commit 1567b212c3
4 changed files with 44 additions and 18 deletions

View File

@ -2925,9 +2925,14 @@
this.RosterView = Backbone.View.extend({ this.RosterView = Backbone.View.extend({
tagName: 'dl', tagName: 'dl',
id: 'converse-roster', id: 'converse-roster',
rosteritemviews: {},
initialize: function () { initialize: function () {
var views = {};
this.get = function (id) {
return views[id];
};
this.set = function (id, view) { views[id] = view; };
this.model.on("add", function (item) { this.model.on("add", function (item) {
this.addRosterItemView(item).render(item); this.addRosterItemView(item).render(item);
if (!item.get('vcard_updated')) { if (!item.get('vcard_updated')) {
@ -2983,15 +2988,15 @@
addRosterItemView: function (item) { addRosterItemView: function (item) {
var view = new converse.RosterItemView({model: item}); var view = new converse.RosterItemView({model: item});
this.rosteritemviews[item.id] = view; this.set(item.id, view);
return this; return this;
}, },
removeRosterItemView: function (item) { removeRosterItemView: function (item) {
var view = this.rosteritemviews[item.id]; var view = this.get(item.id);
if (view) { if (view) {
view.$el.remove(); view.$el.remove();
delete this.rosteritemviews[item.id]; delete this.get(item.id);
this.render(); this.render();
} }
return this; return this;
@ -3018,7 +3023,7 @@
$count, changed_presence; $count, changed_presence;
if (item) { if (item) {
var jid = item.id, var jid = item.id,
view = this.rosteritemviews[item.id], view = this.get(item.id),
ask = item.get('ask'), ask = item.get('ask'),
subscription = item.get('subscription'), subscription = item.get('subscription'),
requesting = item.get('requesting'), requesting = item.get('requesting'),

View File

@ -39,7 +39,7 @@
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(' ','.').toLowerCase() + '@localhost'; jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'openChat').andCallThrough(); spyOn(view, 'openChat').andCallThrough();
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
$el.click(); $el.click();

View File

@ -9,6 +9,14 @@
} (this, function (mock, utils) { } (this, function (mock, utils) {
describe("The Control Box", $.proxy(function (mock, utils) { describe("The Control Box", $.proxy(function (mock, utils) {
beforeEach(function () {
runs(function () {
utils.openControlBox();
});
waits(250);
runs(function () {});
});
it("can be opened by clicking a DOM element with class 'toggle-online-users'", $.proxy(function () { it("can be opened by clicking a DOM element with class 'toggle-online-users'", $.proxy(function () {
runs(function () { runs(function () {
utils.closeControlBox(); utils.closeControlBox();
@ -35,6 +43,15 @@
}, converse)); }, converse));
describe("The Status Widget", $.proxy(function () { describe("The Status Widget", $.proxy(function () {
beforeEach(function () {
runs(function () {
utils.openControlBox();
});
waits(250);
runs(function () {});
});
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", $.proxy(function () {
var view = this.xmppstatusview; var view = this.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);
@ -96,6 +113,9 @@
}, converse, mock, utils)); }, converse, mock, utils));
describe("The Contacts Roster", $.proxy(function (mock, utils) { describe("The Contacts Roster", $.proxy(function (mock, utils) {
// FIXME: These tests are dependent on being run in order and cannot be
// run independently
describe("Pending Contacts", $.proxy(function () { describe("Pending Contacts", $.proxy(function () {
beforeEach(function () { beforeEach(function () {
runs(function () { runs(function () {
@ -134,7 +154,8 @@
}, 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 jid = mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.get(jid);
spyOn(window, 'confirm').andReturn(true); spyOn(window, 'confirm').andReturn(true);
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.connection.roster, 'remove').andCallThrough(); spyOn(this.connection.roster, 'remove').andCallThrough();
@ -234,7 +255,7 @@
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';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough(); spyOn(view, 'render').andCallThrough();
item = view.model; item = view.model;
item.set('chat_status', 'online'); item.set('chat_status', 'online');
@ -253,7 +274,7 @@
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';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough(); spyOn(view, 'render').andCallThrough();
item = view.model; item = view.model;
item.set('chat_status', 'dnd'); item.set('chat_status', 'dnd');
@ -272,7 +293,7 @@
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';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough(); spyOn(view, 'render').andCallThrough();
item = view.model; item = view.model;
item.set('chat_status', 'away'); item.set('chat_status', 'away');
@ -291,7 +312,7 @@
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';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough(); spyOn(view, 'render').andCallThrough();
item = view.model; item = view.model;
item.set('chat_status', 'xa'); item.set('chat_status', 'xa');
@ -310,7 +331,7 @@
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';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
spyOn(view, 'render').andCallThrough(); spyOn(view, 'render').andCallThrough();
item = view.model; item = view.model;
item.set('chat_status', 'unavailable'); item.set('chat_status', 'unavailable');
@ -388,7 +409,7 @@
// actually not accepted/authorized because of // actually not accepted/authorized because of
// mock_connection. // mock_connection.
var jid = mock.req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost'; var jid = mock.req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
var view = this.rosterview.rosteritemviews[jid]; var view = this.rosterview.get(jid);
spyOn(this.connection.roster, 'authorize'); spyOn(this.connection.roster, 'authorize');
spyOn(view, 'acceptRequest').andCallThrough(); spyOn(view, 'acceptRequest').andCallThrough();
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
@ -400,7 +421,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.get(jid);
spyOn(converse, 'emit'); spyOn(converse, 'emit');
spyOn(this.connection.roster, 'unauthorize'); spyOn(this.connection.roster, 'unauthorize');
spyOn(this.rosterview, 'removeRosterItemView').andCallThrough(); spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
@ -451,7 +472,7 @@
// we make some online now // we make some online now
for (i=0; i<5; i++) { for (i=0; i<5; i++) {
jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
view = this.rosterview.rosteritemviews[jid]; view = this.rosterview.get(jid);
view.model.set('chat_status', 'online'); view.model.set('chat_status', 'online');
} }
}, converse)); }, converse));

View File

@ -81,13 +81,13 @@
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(' ','.').toLowerCase() + '@localhost'; jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
views[i] = converse.rosterview.rosteritemviews[jid].openChat(mock.event); views[i] = converse.rosterview.get(jid).openChat(mock.event);
} }
return views; return views;
}; };
utils.openChatBoxFor = function (jid) { utils.openChatBoxFor = function (jid) {
converse.rosterview.rosteritemviews[jid].openChat(mock.event); converse.rosterview.get(jid).openChat(mock.event);
}; };
utils.clearChatBoxMessages = function (jid) { utils.clearChatBoxMessages = function (jid) {