Browse Source

Expand the spoiler tests

JC Brand 7 years ago
parent
commit
20706cb617
4 changed files with 160 additions and 38 deletions
  1. 121 12
      spec/spoilers.js
  2. 1 1
      src/converse-chatboxes.js
  3. 22 15
      src/converse-chatview.js
  4. 16 10
      src/converse-otr.js

+ 121 - 12
spec/spoilers.js

@@ -10,6 +10,7 @@
 
     var _ = converse.env._;
     var $msg = converse.env.$msg;
+    var u = converse.env.utils;
 
     return describe("A spoiler message", function () {
 
@@ -26,8 +27,8 @@
              *      <spoiler xmlns='urn:xmpp:spoiler:0'>Love story end</spoiler>
              *  </message>
              */
-            const spoiler_hint = "Love story end"
-            const spoiler = "And at the end of the story, both of them die! It is so tragic!";
+            var spoiler_hint = "Love story end"
+            var spoiler = "And at the end of the story, both of them die! It is so tragic!";
             var msg = $msg({
                     'xmlns': 'jabber:client',
                     'to': _converse.bare_jid,
@@ -40,15 +41,46 @@
                 .tree();
             _converse.chatboxes.onMessage(msg);
 
-            var chatboxview = _converse.chatboxviews.get(sender_jid);
-            var message_content = chatboxview.el.querySelector('.chat-message .chat-msg-content');
+            var view = _converse.chatboxviews.get(sender_jid);
+            var message_content = view.el.querySelector('.chat-msg-content');
+            expect(message_content.textContent).toBe(spoiler);
 
-            // TODO add better assertions, currently only checks whether the
-            // text is in the DOM, not whether the spoiler is shown or
-            // not. Before updating this the spoiler rendering code needs
-            // improvement.
-            expect(_.includes(message_content.outerHTML, spoiler_hint)).toBeTruthy();
-            expect(_.includes(message_content.outerHTML, spoiler)).toBeTruthy();
+            var spoiler_hint_el = view.el.querySelector('.spoiler-hint');
+            expect(spoiler_hint_el.textContent).toBe(spoiler_hint);
+            done();
+        }));
+
+        it("can be received without a hint",
+            mock.initConverseWithPromises(
+                null, ['rosterGroupsFetched'], {},
+                function (done, _converse) {
+
+            test_utils.createContacts(_converse, 'current');
+            var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+
+            /* <message to='romeo@montague.net/orchard' from='juliet@capulet.net/balcony' id='spoiler2'>
+             *      <body>And at the end of the story, both of them die! It is so tragic!</body>
+             *      <spoiler xmlns='urn:xmpp:spoiler:0'>Love story end</spoiler>
+             *  </message>
+             */
+            var spoiler = "And at the end of the story, both of them die! It is so tragic!";
+            var msg = $msg({
+                    'xmlns': 'jabber:client',
+                    'to': _converse.bare_jid,
+                    'from': sender_jid,
+                    'type': 'chat'
+                }).c('body').t(spoiler).up()
+                  .c('spoiler', {
+                      'xmlns': 'urn:xmpp:spoiler:0',
+                    }).tree();
+            _converse.chatboxes.onMessage(msg);
+
+            var view = _converse.chatboxviews.get(sender_jid);
+            var message_content = view.el.querySelector('.chat-msg-content');
+            expect(message_content.textContent).toBe(spoiler);
+
+            var spoiler_hint_el = view.el.querySelector('.spoiler-hint');
+            expect(spoiler_hint_el.textContent).toBe('');
             done();
         }));
 
@@ -65,6 +97,7 @@
 
             var view = _converse.chatboxviews.get(contact_jid);
             spyOn(view, 'onMessageSubmitted').and.callThrough();
+            spyOn(_converse.connection, 'send');
 
             var spoiler_toggle = view.el.querySelector('.toggle-spoiler-edit');
             spoiler_toggle.click();
@@ -77,6 +110,39 @@
                 keyCode: 13
             });
             expect(view.onMessageSubmitted).toHaveBeenCalled();
+
+            /* Test the XML stanza 
+             *
+             * <message from="dummy@localhost/resource"
+             *          to="max.frankfurter@localhost"
+             *          type="chat"
+             *          id="4547c38b-d98b-45a5-8f44-b4004dbc335e"
+             *          xmlns="jabber:client">
+             *    <body>This is the spoiler</body>
+             *    <active xmlns="http://jabber.org/protocol/chatstates"/>
+             *    <spoiler xmlns="urn:xmpp:spoiler:0"/>
+             * </message>"
+             */
+            var stanza = _converse.connection.send.calls.argsFor(0)[0].tree();
+            var spoiler_el = stanza.querySelector('spoiler[xmlns="urn:xmpp:spoiler:0"]');
+            expect(_.isNull(spoiler_el)).toBeFalsy();
+            expect(spoiler_el.textContent).toBe('');
+
+            var body_el = stanza.querySelector('body');
+            expect(body_el.textContent).toBe('This is the spoiler');
+
+            /* Test the HTML spoiler message */
+            var spoiler_msg_el = view.el.querySelector('.chat-msg-content.spoiler');
+            expect(spoiler_msg_el.textContent).toBe('This is the spoiler');
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
+
+            spoiler_toggle = view.el.querySelector('.toggle-spoiler');
+            expect(spoiler_toggle.textContent).toBe('Show spoiler');
+            spoiler_toggle.click();
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeFalsy();
+            expect(spoiler_toggle.textContent).toBe('Hide spoiler');
+            spoiler_toggle.click();
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
             done();
         }));
 
@@ -96,10 +162,53 @@
             var spoiler_toggle = view.el.querySelector('.toggle-spoiler-edit');
             spoiler_toggle.click();
 
-            var hint_input = view.el.querySelector('.chat-textarea-hint');
+            spyOn(view, 'onMessageSubmitted').and.callThrough();
+            spyOn(_converse.connection, 'send');
+
+            var textarea = view.el.querySelector('.chat-textarea');
+            textarea.value = 'This is the spoiler';
+            var hint_input = view.el.querySelector('.spoiler-hint');
+            hint_input.value = 'This is the hint';
+
+            view.keyPressed({
+                target: textarea,
+                preventDefault: _.noop,
+                keyCode: 13
+            });
+            expect(view.onMessageSubmitted).toHaveBeenCalled();
+
+            /* Test the XML stanza 
+             *
+             * <message from="dummy@localhost/resource"
+             *          to="max.frankfurter@localhost"
+             *          type="chat"
+             *          id="4547c38b-d98b-45a5-8f44-b4004dbc335e"
+             *          xmlns="jabber:client">
+             *    <body>This is the spoiler</body>
+             *    <active xmlns="http://jabber.org/protocol/chatstates"/>
+             *    <spoiler xmlns="urn:xmpp:spoiler:0">This is the hint</spoiler>
+             * </message>"
+             */
+            var stanza = _converse.connection.send.calls.argsFor(0)[0].tree();
+            var spoiler_el = stanza.querySelector('spoiler[xmlns="urn:xmpp:spoiler:0"]');
+            expect(_.isNull(spoiler_el)).toBeFalsy();
+            expect(spoiler_el.textContent).toBe('This is the hint');
+
+            var body_el = stanza.querySelector('body');
+            expect(body_el.textContent).toBe('This is the spoiler');
 
-            // TODO
+            /* Test the HTML spoiler message */
+            var spoiler_msg_el = view.el.querySelector('.chat-msg-content.spoiler');
+            expect(spoiler_msg_el.textContent).toBe('This is the spoiler');
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
 
+            spoiler_toggle = view.el.querySelector('.toggle-spoiler');
+            expect(spoiler_toggle.textContent).toBe('Show spoiler');
+            spoiler_toggle.click();
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeFalsy();
+            expect(spoiler_toggle.textContent).toBe('Hide spoiler');
+            spoiler_toggle.click();
+            expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
             done();
         }));
     });

+ 1 - 1
src/converse-chatboxes.js

@@ -171,7 +171,7 @@
                         'is_spoiler': !_.isNull(spoiler)
                     };
                     if (spoiler) {
-                        attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : __('Spoiler');
+                        attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
                     }
                     return attrs;
                 },

+ 22 - 15
src/converse-chatview.js

@@ -853,6 +853,25 @@
                     }
                 },
 
+                parseMessageForCommands (text) {
+                    const match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/);
+                    if (match) {
+                        if (match[1] === "clear") {
+                            this.clearMessages();
+                            return true;
+                        }
+                        else if (match[1] === "help") {
+                            const msgs = [
+                                `<strong>/clear</strong>: ${__('Remove messages')}`,
+                                `<strong>/me</strong>: ${__('Write in the third person')}`,
+                                `<strong>/help</strong>: ${__('Show this menu')}`
+                                ];
+                            this.showHelpMessages(msgs);
+                            return true;
+                        }
+                    }
+                },
+
                 onMessageSubmitted (text, spoiler_hint) {
                     /* This method gets called once the user has typed a message
                      * and then pressed enter in a chat box.
@@ -869,20 +888,8 @@
                             'error'
                         );
                     }
-                    const match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/);
-                    if (match) {
-                        if (match[1] === "clear") {
-                            return this.clearMessages();
-                        }
-                        else if (match[1] === "help") {
-                            const msgs = [
-                                `<strong>/clear</strong>: ${__('Remove messages')}`,
-                                `<strong>/me</strong>: ${__('Write in the third person')}`,
-                                `<strong>/help</strong>: ${__('Show this menu')}`
-                                ];
-                            this.showHelpMessages(msgs);
-                            return;
-                        }
+                    if (this.parseMessageForCommands(text)) {
+                        return;
                     }
                     const attrs = this.getOutgoingMessageAttributes(text, spoiler_hint)
                     const message = this.model.messages.create(attrs);
@@ -962,7 +969,7 @@
                           message = textarea.value;
 
                     let spoiler_hint;
-                    if (this.model.get('sending_spoiler')) {
+                    if (this.message_form_view.model.get('sending_spoiler')) {
                         const hint_el = this.el.querySelector('form.sendXMPPMessage input.spoiler-hint');
                         spoiler_hint = hint_el.value;
                         hint_el.value = '';

+ 16 - 10
src/converse-otr.js

@@ -283,23 +283,29 @@
                     return stanza;
                 },
 
-                onMessageSubmitted (text) {
+                parseMessageForCommands (text) {
                     const { _converse } = this.__super__;
-                    if (!_converse.connection.authenticated) {
-                        return this.showHelpMessages(
-                            ['Sorry, the connection has been lost, '+
-                              'and your message could not be sent'],
-                            'error'
-                        );
-                    }
                     const match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/);
                     if (match) {
                         if ((_converse.allow_otr) && (match[1] === "endotr")) {
-                            return this.endOTR();
+                            this.endOTR();
+                            return true;
                         } else if ((_converse.allow_otr) && (match[1] === "otr")) {
-                            return this.model.initiateOTR();
+                            this.model.initiateOTR();
+                            return true;
                         }
                     }
+                    return this.__super__.parseMessageForCommands.apply(this, arguments);
+                },
+
+                onMessageSubmitted (text, spoiler_hint) {
+                    const { _converse } = this.__super__;
+                    if (!_converse.connection.authenticated) {
+                        this.__super__.onMessageSubmitted.apply(this, arguments);
+                    }
+                    if (this.parseMessageForCommands(text)) {
+                        return;
+                    }
                     if (_.includes([UNVERIFIED, VERIFIED], this.model.get('otr_status'))) {
                         // Off-the-record encryption is active
                         this.model.otr.sendMsg(text);