(function (root, factory) { define([ "mock" ], function (mock_connection) { return factory(mock_connection); } ); } (this, function (mock_connection) { return describe("Converse.js", function() { // Names from http://www.fakenamegenerator.com/ var req_names = [ 'Louw Spekman', 'Mohamad Stet', 'Dominik Beyer' ]; var pend_names = [ 'Suleyman van Beusichem', 'Nanja van Yperen', 'Nicole Diederich' ]; var cur_names = [ 'Max Frankfurter', 'Candice van der Knijff', 'Irini Vlastuin', 'Rinse Sommer', 'Annegreet Gomez', 'Robin Schook', 'Marcel Eberhardt', 'Simone Brauer', 'Asmaa Haakman', 'Felix Amsel', 'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers' ]; var num_contacts = req_names.length + pend_names.length + cur_names.length; var open_controlbox; closeAllChatBoxes = function () { var i, chatbox, num_chatboxes = converse.chatboxes.models.length; for (i=num_chatboxes-1; i>-1; i--) { chatbox = converse.chatboxes.models[i]; converse.chatboxesview.views[chatbox.get('id')].closeChat(); } }; openControlBox = function () { if (!$("div#controlbox").is(':visible')) { $('.toggle-online-users').click(); } }; describe("The Control Box", $.proxy(function () { it("is not shown by default", $.proxy(function () { expect(this.rosterview.$el.is(':visible')).toEqual(false); }, converse)); open_controlbox = $.proxy(function () { // This spec will only pass if the controlbox is not currently // open yet. expect($("div#controlbox").is(':visible')).toBe(false); spyOn(this, 'toggleControlBox').andCallThrough(); spyOn(this, 'showControlBox').andCallThrough(); $('.toggle-online-users').click(); expect(this.toggleControlBox).toHaveBeenCalled(); expect(this.showControlBox).toHaveBeenCalled(); expect($("div#controlbox").is(':visible')).toBe(true); }, converse); it("can be opened by clicking a DOM element with class 'toggle-online-users'", open_controlbox); describe("The Status Widget", $.proxy(function () { it("shows the user's chat status, which is online by default", $.proxy(function () { var view = this.xmppstatusview; expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online'); }, converse)); it("can be used to set the current user's chat status", $.proxy(function () { var view = this.xmppstatusview; spyOn(view, 'toggleOptions').andCallThrough(); spyOn(view, 'setStatus').andCallThrough(); view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called runs(function () { view.$el.find('a.choose-xmpp-status').click(); expect(view.toggleOptions).toHaveBeenCalled(); }); waits(250); runs(function () { spyOn(view, 'updateStatusUI').andCallThrough(); view.initialize(); // Rebind events for spy $(view.$el.find('.dropdown dd ul li a')[1]).click(); expect(view.setStatus).toHaveBeenCalled(); }); waits(250); runs($.proxy(function () { expect(view.updateStatusUI).toHaveBeenCalled(); expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false); expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy'); }, converse)); }, converse)); it("can be used to set a custom status message", $.proxy(function () { var view = this.xmppstatusview; this.xmppstatus.save({'status': 'online'}); spyOn(view, 'setStatusMessage').andCallThrough(); spyOn(view, 'renderStatusChangeForm').andCallThrough(); view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called view.$el.find('a.change-xmpp-status-message').click(); expect(view.renderStatusChangeForm).toHaveBeenCalled(); // The async testing here is used only to provide time for // visual feedback var msg = 'I am happy'; runs (function () { view.$el.find('form input.custom-xmpp-status').val(msg); }); waits(250); runs (function () { view.$el.find('form#set-custom-xmpp-status').submit(); expect(view.setStatusMessage).toHaveBeenCalled(); expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true); expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg); }); }, converse)); }, converse)); }, converse)); describe("The Contacts Roster", $.proxy(function () { describe("Pending Contacts", $.proxy(function () { beforeEach(function () { if (!$("div#controlbox").is(':visible')) { $('.toggle-online-users').click(); } }); it("do not have a heading if there aren't any", $.proxy(function () { expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none'); }, converse)); it("can be added to the roster", $.proxy(function () { spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.xmppstatus, 'sendPresence'); this.roster.create({ jid: pend_names[0].replace(' ','.').toLowerCase() + '@localhost', subscription: 'none', ask: 'subscribe', fullname: pend_names[0], is_last: true }); expect(this.rosterview.$el.is(':visible')).toEqual(true); expect(this.xmppstatus.sendPresence).toHaveBeenCalled(); expect(this.rosterview.render).toHaveBeenCalled(); }, converse)); it("can be removed by the user", $.proxy(function () { var view = _.toArray(this.rosterview.rosteritemviews).pop(); spyOn(window, 'confirm').andReturn(true); spyOn(this.connection.roster, 'remove').andCallThrough(); spyOn(this.connection.roster, 'unauthorize'); spyOn(this.rosterview.model, 'remove').andCallThrough(); //spyOn(view, 'removeContact').andCallThrough(); runs($.proxy(function () { view.$el.find('.remove-xmpp-contact').click(); }, converse)); waits(500); runs($.proxy(function () { expect(window.confirm).toHaveBeenCalled(); //expect(view.removeContact).toHaveBeenCalled(); expect(this.connection.roster.remove).toHaveBeenCalled(); expect(this.connection.roster.unauthorize).toHaveBeenCalled(); expect(this.rosterview.model.remove).toHaveBeenCalled(); // The element must now be detached from the DOM. expect(view.$el.closest('html').length).toBeFalsy(); }, converse)); }, converse)); it("will lose their own heading once the last one has been removed", $.proxy(function () { expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy(); }, converse)); it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () { var i, t, is_last; spyOn(this.rosterview, 'render').andCallThrough(); spyOn(this.xmppstatus, 'sendPresence'); for (i=0; i