Răsfoiți Sursa

Refactor other tests. All tests pass now again. updates #307

JC Brand 10 ani în urmă
părinte
comite
6c31f76406
3 a modificat fișierele cu 58 adăugiri și 24 ștergeri
  1. 5 4
      converse.js
  2. 44 20
      spec/chatroom.js
  3. 9 0
      tests/mock.js

+ 5 - 4
converse.js

@@ -2523,9 +2523,10 @@
             },
             },
 
 
             leave: function(exit_msg) {
             leave: function(exit_msg) {
+                var presenceid = converse.connection.getUniqueId();
                 var presence = $pres({
                 var presence = $pres({
                     type: "unavailable",
                     type: "unavailable",
-                    id: converse.connection.getUniqueId(),
+                    id: presenceid,
                     from: converse.connection.jid,
                     from: converse.connection.jid,
                     to: this.getRoomJID()
                     to: this.getRoomJID()
                 });
                 });
@@ -2772,10 +2773,10 @@
                 return this.scrollDown();
                 return this.scrollDown();
             },
             },
 
 
-            showErrorMessage: function ($error, room) {
+            showErrorMessage: function ($error) {
                 // We didn't enter the room, so we must remove it from the MUC
                 // We didn't enter the room, so we must remove it from the MUC
                 // add-on
                 // add-on
-                delete converse.connection.muc[room.name];
+                delete converse.connection.muc[this.model.get('jid')]; // XXX: Still needed?
                 if ($error.attr('type') == 'auth') {
                 if ($error.attr('type') == 'auth') {
                     if ($error.find('not-authorized').length) {
                     if ($error.find('not-authorized').length) {
                         this.renderPasswordForm();
                         this.renderPasswordForm();
@@ -2810,7 +2811,7 @@
                 var nick = this.model.get('nick');
                 var nick = this.model.get('nick');
                 if ($presence.attr('type') === 'error') {
                 if ($presence.attr('type') === 'error') {
                     this.model.set('connected', false);
                     this.model.set('connected', false);
-                    this.showErrorMessage($presence.find('error'), room);
+                    this.showErrorMessage($presence.find('error'));
                 } else {
                 } else {
                     is_self = ($presence.find("status[code='110']").length) ||
                     is_self = ($presence.find("status[code='110']").length) ||
                         ($presence.attr('from') == this.model.get('id')+'/'+Strophe.escapeNode(nick));
                         ($presence.attr('from') == this.model.get('id')+'/'+Strophe.escapeNode(nick));

+ 44 - 20
spec/chatroom.js

@@ -23,30 +23,54 @@
 
 
             it("shows users currently present in the room", $.proxy(function () {
             it("shows users currently present in the room", $.proxy(function () {
                 test_utils.openChatRoom('lounge', 'localhost', 'dummy');
                 test_utils.openChatRoom('lounge', 'localhost', 'dummy');
-                var chatroomview = this.chatboxviews.get('lounge@localhost'),
-                    $participant_list;
-                var roster = {}, room = {}, i;
-                for (i=0; i<mock.chatroom_names.length-1; i++) {
-                    roster[mock.chatroom_names[i]] = {};
-                    chatroomview.onChatRoomRoster(roster, room);
-                    $participant_list = chatroomview.$el.find('.participant-list');
-                    expect($participant_list.find('li').length).toBe(1+i);
-                    expect($($participant_list.find('li')[i]).text()).toBe(mock.chatroom_names[i]);
+                var name;
+                var view = this.chatboxviews.get('lounge@localhost'),
+                    $participants = view.$('.participant-list');
+                spyOn(view, 'onChatRoomPresence').andCallThrough();
+                var room = {}, i, role;
+                for (i=0; i<mock.chatroom_names.length; i++) {
+                    name = mock.chatroom_names[i];
+                    console.log(name);
+                    role = mock.chatroom_roles[name].role;
+                    // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
+                    var presence = $pres({
+                            to:'dummy@localhost/pda',
+                            from:'lounge@localhost/'+name
+                    }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
+                    .c('item').attrs({
+                        affiliation: mock.chatroom_roles[name].affiliation,
+                        jid: 'dummy@localhost/pda',
+                        role: role
+                    }).up()
+                    .c('status').attrs({code:'110'}).nodeTree;
+
+                    this.connection._dataRecv(test_utils.createRequest(presence));
+                    expect(view.onChatRoomPresence).toHaveBeenCalled();
+                    expect($participants.find('li').length).toBe(1+i);
+                    expect($($participants.find('li')[i]).text()).toBe(mock.chatroom_names[i]);
+                    expect($($participants.find('li')[i]).hasClass('moderator')).toBe(role === "moderator");
                 }
                 }
-                roster[converse.bare_jid] = {};
-                chatroomview.onChatRoomRoster(roster, room);
             }, converse));
             }, converse));
 
 
             it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
             it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
                 test_utils.openChatRoom('lounge', 'localhost', 'dummy');
                 test_utils.openChatRoom('lounge', 'localhost', 'dummy');
-                var chatroomview = this.chatboxviews.get('lounge@localhost');
-                var roster = {}, idx = mock.chatroom_names.length-1;
-                roster[mock.chatroom_names[idx]] = {};
-                roster[mock.chatroom_names[idx]].role = 'moderator';
-                chatroomview.onChatRoomRoster(roster, {});
-                var occupant = chatroomview.$el.find('.participant-list').find('li');
+                var view = this.chatboxviews.get('lounge@localhost');
+
+                var presence = $pres({
+                        to:'dummy@localhost/pda',
+                        from:'lounge@localhost/moderatorman'
+                }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
+                .c('item').attrs({
+                    affiliation: 'admin',
+                    jid: 'dummy@localhost/pda',
+                    role: 'moderator',
+                }).up()
+                .c('status').attrs({code:'110'}).nodeTree;
+
+                this.connection._dataRecv(test_utils.createRequest(presence));
+                var occupant = view.$el.find('.participant-list').find('li');
                 expect(occupant.length).toBe(1);
                 expect(occupant.length).toBe(1);
-                expect($(occupant).text()).toBe(mock.chatroom_names[idx]);
+                expect($(occupant).text()).toBe("moderatorman");
                 expect($(occupant).attr('class')).toBe('moderator');
                 expect($(occupant).attr('class')).toBe('moderator');
                 expect($(occupant).attr('title')).toBe('This user is a moderator');
                 expect($(occupant).attr('title')).toBe('This user is a moderator');
             }, converse));
             }, converse));
@@ -371,7 +395,7 @@
                 var view = this.chatboxviews.get('lounge@localhost'), chatroom = view.model, $el;
                 var view = this.chatboxviews.get('lounge@localhost'), chatroom = view.model, $el;
                 spyOn(view, 'close').andCallThrough();
                 spyOn(view, 'close').andCallThrough();
                 spyOn(converse, 'emit');
                 spyOn(converse, 'emit');
-                spyOn(converse.connection.muc, 'leave');
+                spyOn(view, 'leave');
                 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 () {
                 runs(function () {
                     view.$el.find('.close-chatbox-button').click();
                     view.$el.find('.close-chatbox-button').click();
@@ -379,7 +403,7 @@
                 waits(50);
                 waits(50);
                 runs(function () {
                 runs(function () {
                     expect(view.close).toHaveBeenCalled();
                     expect(view.close).toHaveBeenCalled();
-                    expect(this.connection.muc.leave).toHaveBeenCalled();
+                    expect(view.leave).toHaveBeenCalled();
                     expect(this.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
                     expect(this.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
                 }.bind(converse));
                 }.bind(converse));
             }, converse));
             }, converse));

+ 9 - 0
tests/mock.js

@@ -33,6 +33,15 @@
     mock.chatroom_names = [
     mock.chatroom_names = [
         'Dyon van de Wege', 'Thomas Kalb', 'Dirk Theissen', 'Felix Hofmann', 'Ka Lek', 'Anne Ebersbacher'
         'Dyon van de Wege', 'Thomas Kalb', 'Dirk Theissen', 'Felix Hofmann', 'Ka Lek', 'Anne Ebersbacher'
     ];
     ];
+    // TODO: need to also test other roles and affiliations
+    mock.chatroom_roles = {
+        'Anne Ebersbacher': { affiliation: "owner", role: "moderator" },
+        'Dirk Theissen': { affiliation: "admin", role: "moderator" },
+        'Dyon van de Wege': { affiliation: "member", role: "participant" },
+        'Felix Hofmann': { affiliation: "member", role: "participant" },
+        'Ka Lek': { affiliation: "member", role: "participant" },
+        'Thomas Kalb': { affiliation: "member", role: "participant" }
+    };
 
 
     mock.event = {
     mock.event = {
         'preventDefault': function () {}
         'preventDefault': function () {}