(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 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)); }));