2
0

spoilers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. const _ = converse.env._;
  5. const Strophe = converse.env.Strophe;
  6. const $msg = converse.env.$msg;
  7. const $pres = converse.env.$pres;
  8. const u = converse.env.utils;
  9. describe("A spoiler message", function () {
  10. it("can be received with a hint",
  11. mock.initConverseWithPromises(
  12. null, ['rosterGroupsFetched'], {},
  13. async function (done, _converse) {
  14. test_utils.createContacts(_converse, 'current');
  15. const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  16. /* <message to='romeo@montague.net/orchard' from='juliet@capulet.net/balcony' id='spoiler2'>
  17. * <body>And at the end of the story, both of them die! It is so tragic!</body>
  18. * <spoiler xmlns='urn:xmpp:spoiler:0'>Love story end</spoiler>
  19. * </message>
  20. */
  21. const spoiler_hint = "Love story end"
  22. const spoiler = "And at the end of the story, both of them die! It is so tragic!";
  23. const msg = $msg({
  24. 'xmlns': 'jabber:client',
  25. 'to': _converse.bare_jid,
  26. 'from': sender_jid,
  27. 'type': 'chat'
  28. }).c('body').t(spoiler).up()
  29. .c('spoiler', {
  30. 'xmlns': 'urn:xmpp:spoiler:0',
  31. }).t(spoiler_hint)
  32. .tree();
  33. _converse.chatboxes.onMessage(msg);
  34. const view = _converse.chatboxviews.get(sender_jid);
  35. await test_utils.waitUntil(() => view.model.vcard.get('fullname') === 'Max Frankfurter')
  36. expect(view.el.querySelector('.chat-msg__author').textContent.trim()).toBe('Max Frankfurter');
  37. const message_content = view.el.querySelector('.chat-msg__text');
  38. expect(message_content.textContent).toBe(spoiler);
  39. const spoiler_hint_el = view.el.querySelector('.spoiler-hint');
  40. expect(spoiler_hint_el.textContent).toBe(spoiler_hint);
  41. done();
  42. }));
  43. it("can be received without a hint",
  44. mock.initConverseWithPromises(
  45. null, ['rosterGroupsFetched'], {},
  46. async function (done, _converse) {
  47. test_utils.createContacts(_converse, 'current');
  48. const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  49. /* <message to='romeo@montague.net/orchard' from='juliet@capulet.net/balcony' id='spoiler2'>
  50. * <body>And at the end of the story, both of them die! It is so tragic!</body>
  51. * <spoiler xmlns='urn:xmpp:spoiler:0'>Love story end</spoiler>
  52. * </message>
  53. */
  54. const spoiler = "And at the end of the story, both of them die! It is so tragic!";
  55. const msg = $msg({
  56. 'xmlns': 'jabber:client',
  57. 'to': _converse.bare_jid,
  58. 'from': sender_jid,
  59. 'type': 'chat'
  60. }).c('body').t(spoiler).up()
  61. .c('spoiler', {
  62. 'xmlns': 'urn:xmpp:spoiler:0',
  63. }).tree();
  64. _converse.chatboxes.onMessage(msg);
  65. const view = _converse.chatboxviews.get(sender_jid);
  66. await test_utils.waitUntil(() => view.model.vcard.get('fullname') === 'Max Frankfurter')
  67. expect(_.includes(view.el.querySelector('.chat-msg__author').textContent, 'Max Frankfurter')).toBeTruthy();
  68. const message_content = view.el.querySelector('.chat-msg__text');
  69. expect(message_content.textContent).toBe(spoiler);
  70. const spoiler_hint_el = view.el.querySelector('.spoiler-hint');
  71. expect(spoiler_hint_el.textContent).toBe('');
  72. done();
  73. }));
  74. it("can be sent without a hint",
  75. mock.initConverseWithPromises(
  76. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  77. async function (done, _converse) {
  78. test_utils.createContacts(_converse, 'current', 1);
  79. _converse.emit('rosterContactsFetched');
  80. test_utils.openControlBox();
  81. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  82. // XXX: We need to send a presence from the contact, so that we
  83. // have a resource, that resource is then queried to see
  84. // whether Strophe.NS.SPOILER is supported, in which case
  85. // the spoiler button will appear.
  86. const presence = $pres({
  87. 'from': contact_jid+'/phone',
  88. 'to': 'dummy@localhost'
  89. });
  90. _converse.connection._dataRecv(test_utils.createRequest(presence));
  91. await test_utils.openChatBoxFor(_converse, contact_jid);
  92. await test_utils.waitUntilDiscoConfirmed(_converse, contact_jid+'/phone', [], [Strophe.NS.SPOILER]);
  93. const view = _converse.chatboxviews.get(contact_jid);
  94. spyOn(view, 'onMessageSubmitted').and.callThrough();
  95. spyOn(_converse.connection, 'send');
  96. await test_utils.waitUntil(() => view.el.querySelector('.toggle-compose-spoiler'));
  97. let spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
  98. spoiler_toggle.click();
  99. const textarea = view.el.querySelector('.chat-textarea');
  100. textarea.value = 'This is the spoiler';
  101. view.keyPressed({
  102. target: textarea,
  103. preventDefault: _.noop,
  104. keyCode: 13
  105. });
  106. expect(view.onMessageSubmitted).toHaveBeenCalled();
  107. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  108. /* Test the XML stanza
  109. *
  110. * <message from="dummy@localhost/resource"
  111. * to="max.frankfurter@localhost"
  112. * type="chat"
  113. * id="4547c38b-d98b-45a5-8f44-b4004dbc335e"
  114. * xmlns="jabber:client">
  115. * <body>This is the spoiler</body>
  116. * <active xmlns="http://jabber.org/protocol/chatstates"/>
  117. * <spoiler xmlns="urn:xmpp:spoiler:0"/>
  118. * </message>"
  119. */
  120. const stanza = _converse.connection.send.calls.argsFor(0)[0].tree();
  121. const spoiler_el = stanza.querySelector('spoiler[xmlns="urn:xmpp:spoiler:0"]');
  122. expect(_.isNull(spoiler_el)).toBeFalsy();
  123. expect(spoiler_el.textContent).toBe('');
  124. const body_el = stanza.querySelector('body');
  125. expect(body_el.textContent).toBe('This is the spoiler');
  126. /* Test the HTML spoiler message */
  127. expect(view.el.querySelector('.chat-msg__author').textContent.trim()).toBe('Max Mustermann');
  128. const spoiler_msg_el = view.el.querySelector('.chat-msg__text.spoiler');
  129. expect(spoiler_msg_el.textContent).toBe('This is the spoiler');
  130. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
  131. spoiler_toggle = view.el.querySelector('.spoiler-toggle');
  132. expect(spoiler_toggle.textContent).toBe('Show more');
  133. spoiler_toggle.click();
  134. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeFalsy();
  135. expect(spoiler_toggle.textContent).toBe('Show less');
  136. spoiler_toggle.click();
  137. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
  138. done();
  139. }));
  140. it("can be sent with a hint",
  141. mock.initConverseWithPromises(
  142. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  143. async function (done, _converse) {
  144. test_utils.createContacts(_converse, 'current', 1);
  145. _converse.emit('rosterContactsFetched');
  146. test_utils.openControlBox();
  147. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  148. // XXX: We need to send a presence from the contact, so that we
  149. // have a resource, that resource is then queried to see
  150. // whether Strophe.NS.SPOILER is supported, in which case
  151. // the spoiler button will appear.
  152. const presence = $pres({
  153. 'from': contact_jid+'/phone',
  154. 'to': 'dummy@localhost'
  155. });
  156. _converse.connection._dataRecv(test_utils.createRequest(presence));
  157. await test_utils.openChatBoxFor(_converse, contact_jid);
  158. await test_utils.waitUntilDiscoConfirmed(_converse, contact_jid+'/phone', [], [Strophe.NS.SPOILER]);
  159. const view = _converse.chatboxviews.get(contact_jid);
  160. await test_utils.waitUntil(() => view.el.querySelector('.toggle-compose-spoiler'));
  161. let spoiler_toggle = view.el.querySelector('.toggle-compose-spoiler');
  162. spoiler_toggle.click();
  163. spyOn(view, 'onMessageSubmitted').and.callThrough();
  164. spyOn(_converse.connection, 'send');
  165. const textarea = view.el.querySelector('.chat-textarea');
  166. textarea.value = 'This is the spoiler';
  167. const hint_input = view.el.querySelector('.spoiler-hint');
  168. hint_input.value = 'This is the hint';
  169. view.keyPressed({
  170. target: textarea,
  171. preventDefault: _.noop,
  172. keyCode: 13
  173. });
  174. expect(view.onMessageSubmitted).toHaveBeenCalled();
  175. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  176. /* Test the XML stanza
  177. *
  178. * <message from="dummy@localhost/resource"
  179. * to="max.frankfurter@localhost"
  180. * type="chat"
  181. * id="4547c38b-d98b-45a5-8f44-b4004dbc335e"
  182. * xmlns="jabber:client">
  183. * <body>This is the spoiler</body>
  184. * <active xmlns="http://jabber.org/protocol/chatstates"/>
  185. * <spoiler xmlns="urn:xmpp:spoiler:0">This is the hint</spoiler>
  186. * </message>"
  187. */
  188. const stanza = _converse.connection.send.calls.argsFor(0)[0].tree();
  189. const spoiler_el = stanza.querySelector('spoiler[xmlns="urn:xmpp:spoiler:0"]');
  190. expect(_.isNull(spoiler_el)).toBeFalsy();
  191. expect(spoiler_el.textContent).toBe('This is the hint');
  192. const body_el = stanza.querySelector('body');
  193. expect(body_el.textContent).toBe('This is the spoiler');
  194. /* Test the HTML spoiler message */
  195. expect(view.el.querySelector('.chat-msg__author').textContent.trim()).toBe('Max Mustermann');
  196. const spoiler_msg_el = view.el.querySelector('.chat-msg__text.spoiler');
  197. expect(spoiler_msg_el.textContent).toBe('This is the spoiler');
  198. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
  199. spoiler_toggle = view.el.querySelector('.spoiler-toggle');
  200. expect(spoiler_toggle.textContent).toBe('Show more');
  201. spoiler_toggle.click();
  202. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeFalsy();
  203. expect(spoiler_toggle.textContent).toBe('Show less');
  204. spoiler_toggle.click();
  205. expect(_.includes(spoiler_msg_el.classList, 'collapsed')).toBeTruthy();
  206. done();
  207. }));
  208. });
  209. }));