Used debounce ineffectually. Fixed now.

The ChatBox.show() method was being called for every invocation, 100ms after
the fact, instead of it being called only once.

Fixed that and also changed to call it at the start.
This commit is contained in:
JC Brand 2016-02-15 14:55:34 +00:00
parent e94904e4a1
commit da0c858fe0
3 changed files with 193 additions and 167 deletions

View File

@ -104,24 +104,23 @@
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(this.chatboxes.length).toEqual(1); expect(converse.chatboxes.length).toEqual(1);
chatbox = test_utils.openChatBoxFor(contact_jid); chatbox = test_utils.openChatBoxFor(contact_jid);
chatboxview = this.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
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(300); // ChatBox.show() is debounced for 250ms
runs(function () { runs(function () {
$el = this.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")'); $el = converse.rosterview.$el.find('a.open-chat:contains("'+chatbox.get('fullname')+'")');
jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost'; jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
$el.click(); $el.click();
expect(this.chatboxes.length).toEqual(2); expect(converse.chatboxes.length).toEqual(2);
expect(chatboxview.focus).toHaveBeenCalled(); expect(chatboxview.focus).toHaveBeenCalled();
}.bind(this)); });
}.bind(converse)); });
it("can be saved to, and retrieved from, browserStorage", function () { it("can be saved to, and retrieved from, browserStorage", function () {
spyOn(converse, 'emit'); spyOn(converse, 'emit');
@ -270,18 +269,19 @@
}.bind(converse)); }.bind(converse));
it("contains a button for inserting emoticons", function () { 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;
waits(300); // ChatBox.show() is debounced for 250ms
runs(function () {
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(contact_jid);
var view = this.chatboxviews.get(contact_jid); view = converse.chatboxviews.get(contact_jid);
var $toolbar = view.$el.find('ul.chat-toolbar'); $toolbar = view.$el.find('ul.chat-toolbar');
var $textarea = view.$el.find('textarea.chat-textarea'); $textarea = view.$el.find('textarea.chat-textarea');
expect($toolbar.children('li.toggle-smiley').length).toBe(1); expect($toolbar.children('li.toggle-smiley').length).toBe(1);
// Register spies // Register spies
spyOn(view, 'toggleEmoticonMenu').andCallThrough(); spyOn(view, 'toggleEmoticonMenu').andCallThrough();
spyOn(view, 'insertEmoticon').andCallThrough(); spyOn(view, 'insertEmoticon').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
runs(function () {
$toolbar.children('li.toggle-smiley').click(); $toolbar.children('li.toggle-smiley').click();
}); });
waits(250); waits(250);
@ -325,7 +325,7 @@
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 ');
}); });
}.bind(converse)); });
it("contains a button for starting an encrypted chat session", function () { 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...
@ -812,17 +812,20 @@
describe("An active notification", function () { describe("An active notification", function () {
it("is sent when the user opens a chat box", function () { it("is sent when the user opens a chat box", function () {
waits(300); // ChatBox.show() is debounced for 250ms
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(contact_jid);
var view = this.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();
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().length).toBe(1); expect($stanza.children().length).toBe(1);
expect($stanza.children().prop('tagName')).toBe('active'); expect($stanza.children().prop('tagName')).toBe('active');
}.bind(converse)); });
});
it("is sent when the user maximizes a minimized a chat box", function () { 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';
@ -843,18 +846,20 @@
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 () { it("is sent as soon as the user starts typing a message which is not a command", function () {
waits(300); // ChatBox.show() is debounced for 250ms
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(contact_jid);
var view = this.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(this.connection, 'send'); spyOn(converse.connection, 'send');
view.keyPressed({ view.keyPressed({
target: view.$el.find('textarea.chat-textarea'), target: view.$el.find('textarea.chat-textarea'),
keyCode: 1 keyCode: 1
}); });
expect(view.model.get('chat_state')).toBe('composing'); expect(view.model.get('chat_state')).toBe('composing');
expect(this.connection.send).toHaveBeenCalled(); expect(converse.connection.send).toHaveBeenCalled();
var $stanza = $(this.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().length).toBe(1); expect($stanza.children().length).toBe(1);
expect($stanza.children().prop('tagName')).toBe('composing'); expect($stanza.children().prop('tagName')).toBe('composing');
@ -866,7 +871,8 @@
}); });
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);
}.bind(converse)); });
});
it("will be shown if received", function () { 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
@ -893,13 +899,15 @@
describe("A paused notification", function () { describe("A paused notification", function () {
it("is sent if the user has stopped typing since 30 seconds", function () { it("is sent if the user has stopped typing since 30 seconds", function () {
this.TIMEOUTS.PAUSED = 200; // Make the timeout shorter so that we can test var view, contact_jid;
var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; converse.TIMEOUTS.PAUSED = 200; // Make the timeout shorter so that we can test
waits(300); // ChatBox.show() is debounced for 250ms
runs(function () {
contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(contact_jid); test_utils.openChatBoxFor(contact_jid);
var view = this.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();
runs(function () {
expect(view.model.get('chat_state')).toBe('active'); expect(view.model.get('chat_state')).toBe('active');
view.keyPressed({ view.keyPressed({
target: view.$el.find('textarea.chat-textarea'), target: view.$el.find('textarea.chat-textarea'),
@ -940,7 +948,7 @@
runs(function () { runs(function () {
expect(view.model.get('chat_state')).toBe('composing'); expect(view.model.get('chat_state')).toBe('composing');
}); });
}.bind(converse)); });
it("will be shown if received", function () { 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
@ -1010,9 +1018,11 @@
}.bind(converse)); }.bind(converse));
it("is sent if the user closes a chat box", function () { it("is sent if the user closes a chat box", function () {
waits(300); // ChatBox.show() is debounced for 250ms
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(contact_jid);
var view = this.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');
view.close(); view.close();
@ -1022,7 +1032,8 @@
expect($stanza.attr('to')).toBe(contact_jid); expect($stanza.attr('to')).toBe(contact_jid);
expect($stanza.children().length).toBe(1); expect($stanza.children().length).toBe(1);
expect($stanza.children().prop('tagName')).toBe('inactive'); expect($stanza.children().prop('tagName')).toBe('inactive');
}.bind(converse)); });
});
it("will clear any other chat status notifications if its received", function () { 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

View File

@ -265,15 +265,15 @@
test_utils.createContacts('current'); test_utils.createContacts('current');
}, converse)); }, converse));
it("has a method 'get' which returns a wrapped chat box", $.proxy(function () { it("has a method 'get' which returns a wrapped chat box", function () {
// Test on chat that doesn't exist. // Test on chat that doesn't exist.
expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy(); expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy();
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
// Test on chat that's not open // Test on chat that's not open
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var box = converse_api.chats.get(jid); var box = converse_api.chats.get(jid);
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
var chatboxview = this.chatboxviews.get(jid); var chatboxview = converse.chatboxviews.get(jid);
expect(chatboxview.$el.is(':visible')).toBeFalsy(); expect(chatboxview.$el.is(':visible')).toBeFalsy();
// Test for single JID // Test for single JID
@ -281,9 +281,8 @@
box = converse_api.chats.get(jid); box = converse_api.chats.get(jid);
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(b64_sha1(jid)); expect(box.get('box_id')).toBe(b64_sha1(jid));
chatboxview = this.chatboxviews.get(jid); chatboxview = converse.chatboxviews.get(jid);
expect(chatboxview.$el.is(':visible')).toBeTruthy(); expect(chatboxview.$el.is(':visible')).toBeTruthy();
// Test for multiple JIDs // Test for multiple JIDs
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(jid2); test_utils.openChatBoxFor(jid2);
@ -291,13 +290,15 @@
expect(Array.isArray(list)).toBeTruthy(); expect(Array.isArray(list)).toBeTruthy();
expect(list[0].get('box_id')).toBe(b64_sha1(jid)); expect(list[0].get('box_id')).toBe(b64_sha1(jid));
expect(list[1].get('box_id')).toBe(b64_sha1(jid2)); expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
}, converse)); });
it("has a method 'open' which opens and returns a wrapped chat box", $.proxy(function () { it("has a method 'open' which opens and returns a wrapped chat box", function () {
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var chatboxview;
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// Test on chat that doesn't exist. // Test on chat that doesn't exist.
expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy(); expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy();
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
var box = converse_api.chats.open(jid); var box = converse_api.chats.open(jid);
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(b64_sha1(jid)); expect(box.get('box_id')).toBe(b64_sha1(jid));
@ -305,16 +306,16 @@
Object.keys(box), Object.keys(box),
['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set'] ['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
); );
var chatboxview = this.chatboxviews.get(jid); chatboxview = converse.chatboxviews.get(jid);
expect(chatboxview.$el.is(':visible')).toBeTruthy(); expect(chatboxview.$el.is(':visible')).toBeTruthy();
// Test for multiple JIDs // Test for multiple JIDs
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var list = converse_api.chats.open([jid, jid2]); var list = converse_api.chats.open([jid, jid2]);
expect(Array.isArray(list)).toBeTruthy(); expect(Array.isArray(list)).toBeTruthy();
expect(list[0].get('box_id')).toBe(b64_sha1(jid)); expect(list[0].get('box_id')).toBe(b64_sha1(jid));
expect(list[1].get('box_id')).toBe(b64_sha1(jid2)); expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
}, converse)); });
});
}, converse)); }, converse));
describe("The \"rooms\" API", function () { describe("The \"rooms\" API", function () {
@ -326,6 +327,8 @@
}); });
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)", function () {
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
test_utils.openChatRoom('lounge', 'localhost', 'dummy'); test_utils.openChatRoom('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);
@ -334,19 +337,23 @@
var chatroomview = converse.chatboxviews.get(jid); var chatroomview = converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
chatroomview.close(); chatroomview.close();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// Test with mixed case // Test with mixed case
test_utils.openChatRoom('Leisure', 'localhost', 'dummy'); test_utils.openChatRoom('Leisure', 'localhost', 'dummy');
jid = 'Leisure@localhost'; var jid = 'Leisure@localhost';
room = converse_api.rooms.get(jid); var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
chatroomview = converse.chatboxviews.get(jid.toLowerCase()); var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
});
jid = 'leisure@localhost'; waits('300'); // ChatBox.show() is debounced for 250ms
room = converse_api.rooms.get(jid); runs(function () {
var jid = 'leisure@localhost';
var room = converse_api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
chatroomview = converse.chatboxviews.get(jid.toLowerCase()); var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
jid = 'leiSure@localhost'; jid = 'leiSure@localhost';
@ -361,23 +368,30 @@
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", function () {
// Test on chat room that doesn't exist. var chatroomview;
var jid = 'lounge@localhost'; var jid = 'lounge@localhost';
var room = converse_api.rooms.open(jid); var room = converse_api.rooms.open(jid);
runs(function () {
// Test on chat room that doesn't exist.
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
expect(room.is_chatroom).toBeTruthy(); expect(room.is_chatroom).toBeTruthy();
var chatroomview = converse.chatboxviews.get(jid); chatroomview = converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// Test again, now that the room exists. // Test again, now that the room exists.
room = converse_api.rooms.open(jid); room = converse_api.rooms.open(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
expect(room.is_chatroom).toBeTruthy(); expect(room.is_chatroom).toBeTruthy();
chatroomview = converse.chatboxviews.get(jid); chatroomview = converse.chatboxviews.get(jid);
expect(chatroomview.$el.is(':visible')).toBeTruthy(); expect(chatroomview.$el.is(':visible')).toBeTruthy();
});
waits('300'); // ChatBox.show() is debounced for 250ms
runs(function () {
// Test with mixed case in JID // Test with mixed case in JID
jid = 'Leisure@localhost'; jid = 'Leisure@localhost';
room = converse_api.rooms.open(jid); room = converse_api.rooms.open(jid);
@ -399,6 +413,7 @@
chatroomview.close(); chatroomview.close();
}); });
}); });
});
describe("The \"settings\" API", $.proxy(function() { describe("The \"settings\" API", $.proxy(function() {
beforeEach($.proxy(function () { beforeEach($.proxy(function () {

View File

@ -1429,7 +1429,7 @@
converse.incrementMsgCounter(); converse.incrementMsgCounter();
} }
if (!this.model.get('minimized') && !this.$el.is(':visible')) { if (!this.model.get('minimized') && !this.$el.is(':visible')) {
_.debounce(this.show.bind(this), 100)(); this.show();
} }
}, },
@ -1830,7 +1830,7 @@
return this; return this;
}, },
show: function (focus) { show: _.debounce(function (callback) {
if (this.$el.is(':visible') && this.$el.css('opacity') === "1") { if (this.$el.is(':visible') && this.$el.css('opacity') === "1") {
if (focus) { this.focus(); } if (focus) { this.focus(); }
return this; return this;
@ -1849,7 +1849,7 @@
} }
}.bind(this)); }.bind(this));
return this; return this;
}, }, 250, true),
scrollDownMessageHeight: function ($message) { scrollDownMessageHeight: function ($message) {
if (this.$content.is(':visible')) { if (this.$content.is(':visible')) {