소스 검색

Write a test for rooms listing.

JC Brand 10 년 전
부모
커밋
762e2bac04
2개의 변경된 파일61개의 추가작업 그리고 44개의 파일을 삭제
  1. 36 32
      converse.js
  2. 25 12
      spec/controlbox.js

+ 36 - 32
converse.js

@@ -1789,6 +1789,39 @@
                 $('input#show-rooms').show().siblings('span.spinner').remove();
             },
 
+            onRoomsFound: function (iq) {
+                /* Handle the IQ stanza returned from the server, containing
+                 * all its public rooms.
+                 */
+                var name, jid, i, fragment,
+                    that = this,
+                    $available_chatrooms = this.$el.find('#available-chatrooms');
+                this.rooms = $(iq).find('query').find('item');
+                if (this.rooms.length) {
+                    // # For translators: %1$s is a variable and will be
+                    // # replaced with the XMPP server name
+                    $available_chatrooms.html('<dt>'+__('Rooms on %1$s',this.model.get('muc_domain'))+'</dt>');
+                    fragment = document.createDocumentFragment();
+                    for (i=0; i<this.rooms.length; i++) {
+                        name = Strophe.unescapeNode($(this.rooms[i]).attr('name')||$(this.rooms[i]).attr('jid'));
+                        jid = $(this.rooms[i]).attr('jid');
+                        fragment.appendChild($(
+                            converse.templates.room_item({
+                                'name':name,
+                                'jid':jid,
+                                'open_title': __('Click to open this room'),
+                                'info_title': __('Show more information on this room')
+                                })
+                            )[0]);
+                    }
+                    $available_chatrooms.append(fragment);
+                    $('input#show-rooms').show().siblings('span.spinner').remove();
+                } else {
+                    this.informNoRoomsFound();
+                }
+                return true;
+            },
+
             updateRoomsList: function () {
                 /* Send and IQ stanza to the server asking for all rooms
                  */
@@ -1798,38 +1831,9 @@
                         from: converse.connection.jid,
                         type: "get"
                     }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS}),
-                    // Succcess Handler
-                    $.proxy(function (iq) {
-                        var name, jid, i, fragment,
-                            that = this,
-                            $available_chatrooms = this.$el.find('#available-chatrooms');
-                        this.rooms = $(iq).find('query').find('item');
-                        if (this.rooms.length) {
-                            // # For translators: %1$s is a variable and will be
-                            // # replaced with the XMPP server name
-                            $available_chatrooms.html('<dt>'+__('Rooms on %1$s',this.model.get('muc_domain'))+'</dt>');
-                            fragment = document.createDocumentFragment();
-                            for (i=0; i<this.rooms.length; i++) {
-                                name = Strophe.unescapeNode($(this.rooms[i]).attr('name')||$(this.rooms[i]).attr('jid'));
-                                jid = $(this.rooms[i]).attr('jid');
-                                fragment.appendChild($(
-                                    converse.templates.room_item({
-                                        'name':name,
-                                        'jid':jid,
-                                        'open_title': __('Click to open this room'),
-                                        'info_title': __('Show more information on this room')
-                                        })
-                                    )[0]);
-                            }
-                            $available_chatrooms.append(fragment);
-                            $('input#show-rooms').show().siblings('span.spinner').remove();
-                        } else {
-                            this.informNoRoomsFound();
-                        }
-                        return true;
-                    }, this),
-                    // Error handler
-                    $.proxy(function (iq) { this.informNoRoomsFound(); }, this));
+                    $.proxy(function (iq) { this.onRoomsFound(); }, this),
+                    $.proxy(function (iq) { this.informNoRoomsFound(); }, this)
+                );
             },
 
             showRooms: function (ev) {

+ 25 - 12
spec/controlbox.js

@@ -1076,15 +1076,10 @@
                 var $chatrooms = $panels.children().last();
                 spyOn(cbview, 'switchTab').andCallThrough();
                 cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
-                runs(function () {
-                    $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
-                });
-                waits(250);
-                runs(function () {
-                    expect($contacts.is(':visible')).toBe(false);
-                    expect($chatrooms.is(':visible')).toBe(true);
-                    expect(cbview.switchTab).toHaveBeenCalled();
-                });
+                $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
+                expect($contacts.is(':visible')).toBe(false);
+                expect($chatrooms.is(':visible')).toBe(true);
+                expect(cbview.switchTab).toHaveBeenCalled();
             }, converse));
 
             it("contains a form through which a new chatroom can be created", $.proxy(function () {
@@ -1113,11 +1108,29 @@
                 }, converse));
             }, converse));
 
-            it("lists rooms currently on the server", $.proxy(function () {
-                // TODO: test updateRoomsList
+            it("can list rooms publically available on the server", $.proxy(function () {
+                var panel = this.chatboxviews.get('controlbox').roomspanel;
+                panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab
+                panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
                 // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
+                expect($('#available-chatrooms').children('dt').length).toBe(0);
+                expect($('#available-chatrooms').children('dd').length).toBe(0);
+
+                var iq = $iq({
+                    from:'muc.localhost',
+                    to:'dummy@localhost/pda',
+                    type:'result'
+                }).c('query')
+                  .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
+                  .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
+                  .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
+                  .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
+
+                panel.onRoomsFound(iq);
+                expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
+                expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
+                expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
             }, converse));
-
         }, converse));
     }, converse, mock, test_utils));
 }));