Quellcode durchsuchen

Add an (incomplete) test for a received spoiler message

JC Brand vor 7 Jahren
Ursprung
Commit
d6da643a7f
2 geänderte Dateien mit 53 neuen und 22 gelöschten Zeilen
  1. 40 0
      spec/spoilers.js
  2. 13 22
      src/converse-spoilers.js

+ 40 - 0
spec/spoilers.js

@@ -9,9 +9,49 @@
 } (this, function (jasmine, utils, mock, converse, test_utils) {
 
     var _ = converse.env._;
+    var $msg = converse.env.$msg;
 
     return describe("A spoiler message", function () {
 
+        it("can be received with 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>
+             */
+            const spoiler_hint = "Love story end"
+            const 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',
+                    }).t(spoiler_hint)
+                .tree();
+            _converse.chatboxes.onMessage(msg);
+
+            var chatboxview = _converse.chatboxviews.get(sender_jid);
+            var message_content = chatboxview.el.querySelector('.chat-message .chat-msg-content');
+
+            // 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();
+            done();
+        }));
+
         it("can be sent without a hint",
             mock.initConverseWithPromises(
                 null, ['rosterGroupsFetched'], {},

+ 13 - 22
src/converse-spoilers.js

@@ -173,9 +173,9 @@
                     const msg = this.__super__.renderMessage.apply(this, arguments);
                     console.log(msg);
 
-                    //Spoiler logic
-                    //The value of the "spoiler" attribute, corresponds to the spoiler's hint.
-                    if ("spoiler" in attrs) {
+                    // Spoiler logic
+                    // The value of the "spoiler" attribute, corresponds to the spoiler's hint.
+                    if (attrs.is_spoiler) {
                         console.log('Spoiler in attrs \n');
                         const button = document.createElement("button");
                         const container = document.createElement("div");
@@ -184,7 +184,7 @@
                         const contentHidden = document.createElement("div");
                         const messageContent = msg.querySelector(".chat-msg-content");
 
-                        hint.appendChild(document.createTextNode(attrs.spoiler));
+                        hint.appendChild(document.createTextNode(attrs.spoiler_hint));
 
                         for (var i = 0; i < messageContent.childNodes.length; i++){
                             contentHidden.append(messageContent.childNodes[i]);
@@ -218,30 +218,21 @@
                         messageContent.append(document.createElement("br"));
                         messageContent.append(container);
                     }
-
                     return msg;
                 }
             },
+
             'ChatBox': {
-                'getMessageAttributes': function (message, delay, original_stanza) {
-                    const { _converse } = this.__super__,
-                          { __ } = _converse;
-                    const messageAttributes = this.__super__.getMessageAttributes.apply(this, arguments);
-                    console.log(arguments);
-                    //Check if message is spoiler
-                    let spoiler = null, i = 0, found = false;
-
-                    while (i < message.childNodes.length && !found) {
-                        if (message.childNodes[i].nodeName == "spoiler") {
-                            spoiler = message.childNodes[i];
-                            found = true;
-                        }
-                        i++;
-                    }
+
+                getMessageAttributes (message, delay, original_stanza) {
+                    const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
+                    const spoiler = message.querySelector(`spoiler[xmlns="${Strophe.NS.SPOILER}"]`)
                     if (spoiler) {
-                        messageAttributes.spoiler = spoiler.textContent.length > 0 ? spoiler.textContent : __('Spoiler');
+                        const { __ } = this.__super__._converse;
+                        attrs.is_spoiler = true;
+                        attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : __('Spoiler');
                     }
-                    return messageAttributes;
+                    return attrs;
                 }
             }
         },