From d6da643a7fa40d681c1bed0b2ba80414209be9fa Mon Sep 17 00:00:00 2001 From: JC Brand Date: Fri, 2 Feb 2018 21:54:06 +0100 Subject: [PATCH] Add an (incomplete) test for a received spoiler message --- spec/spoilers.js | 40 ++++++++++++++++++++++++++++++++++++++++ src/converse-spoilers.js | 35 +++++++++++++---------------------- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/spec/spoilers.js b/spec/spoilers.js index 64beac8df..cdae162ff 100644 --- a/spec/spoilers.js +++ b/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'; + + /* + * And at the end of the story, both of them die! It is so tragic! + * Love story end + * + */ + 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'], {}, diff --git a/src/converse-spoilers.js b/src/converse-spoilers.js index 229afacfb..92aae5ba2 100644 --- a/src/converse-spoilers.js +++ b/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++; - } + 'ChatBox': { + + 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; } } },