(function (root, factory) { define([ "mock", "utils" ], function (mock, utils) { return factory(mock, utils); } ); } (this, function (mock, utils) { return describe("Chatboxes", $.proxy(function(mock, utils) { describe("A Chatbox", $.proxy(function () { beforeEach(function () { utils.closeAllChatBoxes(); utils.removeControlBox(); converse.roster.localStorage._clear(); utils.initConverse(); utils.createCurrentContacts(); utils.openControlBox(); utils.openContactsPanel(); }); it("is created when you click on a roster item", $.proxy(function () { var i, $el, click, jid, view; // openControlBox was called earlier, so the controlbox is // visible, but no other chat boxes have been created. expect(this.chatboxes.length).toEqual(1); var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat'); for (i=0; i element, with the required // props. var $time = $chat_content.find('time'); var message_date = new Date(); message_date.setUTCHours(0,0,0,0); expect($time.length).toEqual(1); expect($time.attr('class')).toEqual('chat-date'); expect($time.attr('datetime')).toEqual(converse.toISOString(message_date)); expect($time.text()).toEqual(message_date.toString().substring(0,15)); // Normal checks for the 2nd message expect(chatbox.messages.length).toEqual(2); msg_obj = chatbox.messages.models[1]; expect(msg_obj.get('message')).toEqual(message); expect(msg_obj.get('fullname')).toEqual(contact_name.split(' ')[0]); expect(msg_obj.get('sender')).toEqual('them'); expect(msg_obj.get('delayed')).toEqual(false); msg_txt = $chat_content.find('.chat-message').last().find('.chat-message-content').text(); expect(msg_txt).toEqual(message); sender_txt = $chat_content.find('span.chat-message-them').last().text(); expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy(); }, converse)); it("can be sent from a chatbox, and will appear inside it", $.proxy(function () { var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; utils.openChatBoxFor(contact_jid); var view = this.chatboxesview.views[contact_jid]; var message = 'This message is sent from this chatbox'; spyOn(view, 'sendMessage').andCallThrough(); view.$el.find('.chat-textarea').text(message); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); expect(view.sendMessage).toHaveBeenCalled(); expect(view.model.messages.length, 2); var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text(); expect(txt).toEqual(message); }, converse)); }, converse)); }, converse)); describe("Special Messages", $.proxy(function () { it("'/clear' can be used to clear messages in a conversation", $.proxy(function () { var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; var view = this.chatboxesview.views[contact_jid]; var message = 'This message is another sent from this chatbox'; // Lets make sure there is at least one message already // (e.g for when this test is run on its own). view.$el.find('.chat-textarea').val(message).text(message); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); expect(view.model.messages.length > 0).toBeTruthy(); expect(view.model.messages.localStorage.records.length > 0).toBeTruthy(); message = '/clear'; var old_length = view.model.messages.length; spyOn(view, 'sendMessage').andCallThrough(); view.$el.find('.chat-textarea').val(message).text(message); view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13})); expect(view.sendMessage).toHaveBeenCalled(); expect(view.model.messages.length, 0); // The messages must be removed from the modal expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage }, converse)); }, converse)); describe("A Message Counter", $.proxy(function () { beforeEach($.proxy(function () { converse.clearMsgCounter(); }, converse)); it("is incremented when the message is received and the window is not focused", $.proxy(function () { expect(this.msg_counter).toBe(0); spyOn(converse, 'incrementMsgCounter').andCallThrough(); $(window).trigger('blur'); var message = 'This message will increment the message counter'; var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; msg = $msg({ from: sender_jid, to: this.connection.jid, type: 'chat', id: (new Date()).getTime() }).c('body').t(message).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); this.chatboxes.messageReceived(msg); expect(converse.incrementMsgCounter).toHaveBeenCalled(); expect(this.msg_counter).toBe(1); }, converse)); it("is cleared when the window is focused", $.proxy(function () { spyOn(converse, 'clearMsgCounter').andCallThrough(); runs(function () { $(window).trigger('focus'); }); waits(50); runs(function () { expect(converse.clearMsgCounter).toHaveBeenCalled(); }); }, converse)); it("is not incremented when the message is received and the window is focused", $.proxy(function () { expect(this.msg_counter).toBe(0); spyOn(converse, 'incrementMsgCounter').andCallThrough(); $(window).trigger('focus'); var message = 'This message will not increment the message counter'; var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; msg = $msg({ from: sender_jid, to: this.connection.jid, type: 'chat', id: (new Date()).getTime() }).c('body').t(message).up() .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); this.chatboxes.messageReceived(msg); expect(converse.incrementMsgCounter).not.toHaveBeenCalled(); expect(this.msg_counter).toBe(0); }, converse)); }, converse)); }, converse, mock, utils)); }));