DiscussionViewer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. var common = require('../common');
  3. describe('DiscussionViewer', function () {
  4. describe('handleNotification, prepareNewDiscussion, addComment, finishDiscussion, getReplyMessage, getReplyNickname, getReplyCommentId & highlightComment', function () {
  5. this.timeout(30000);
  6. jsc.property(
  7. 'displays & hides comments as requested',
  8. jsc.array(
  9. jsc.record({
  10. idArray: jsc.nearray(common.jscAlnumString()),
  11. parentidArray: jsc.nearray(common.jscAlnumString()),
  12. data: jsc.string,
  13. meta: jsc.record({
  14. nickname: jsc.string,
  15. postdate: jsc.nat,
  16. vizhash: jsc.string
  17. })
  18. })
  19. ),
  20. 'nat',
  21. 'bool',
  22. 'string',
  23. 'string',
  24. jsc.elements(['loading', 'danger', 'other']),
  25. 'nestring',
  26. function (comments, commentKey, fadeOut, nickname, message, alertType, alert) {
  27. var clean = globalThis.cleanup(),
  28. results = [];
  29. document.body.innerHTML = (
  30. `<div id="discussion">
  31. <h4>Discussion</h4>
  32. <div id="commentcontainer"></div>
  33. </div>
  34. <div id="templates">
  35. <article id="commenttemplate" class="comment">
  36. <div class="commentmeta">
  37. <span class="nickname">name</span>
  38. <span class="commentdate">0000-00-00</span>
  39. </div>
  40. <div class="commentdata">c</div>
  41. <button class="btn btn-default btn-sm">Reply</button>
  42. </article>
  43. <p id="commenttailtemplate" class="comment">
  44. <button class="btn btn-default btn-sm">Add comment</button>
  45. </p>
  46. <div id="replytemplate" class="reply hidden">
  47. <input type="text" id="nickname" class="form-control" title="Optional nickname…"
  48. placeholder="Optional nickname…" />
  49. <textarea id="replymessage" class="replymessage form-control" cols="80" rows="7"></textarea>
  50. <br />
  51. <div id="replystatus" role="alert" class="statusmessage hidden alert">
  52. <span class="glyphicon" aria-hidden="true"></span>
  53. </div>
  54. <button id="replybutton" class="btn btn-default btn-sm">Post comment</button>
  55. </div>
  56. </div>
  57. `
  58. );
  59. PrivateBin.Model.init();
  60. PrivateBin.DiscussionViewer.init();
  61. results.push(
  62. !document.getElementById('discussion').classList.contains('hidden')
  63. );
  64. PrivateBin.DiscussionViewer.prepareNewDiscussion();
  65. results.push(
  66. document.getElementById('discussion').classList.contains('hidden')
  67. );
  68. comments.forEach(function (comment) {
  69. comment.id = comment.idArray.join('');
  70. comment.parentid = comment.parentidArray.join('');
  71. PrivateBin.DiscussionViewer.addComment(PrivateBin.Helper.CommentFactory(comment), comment.data, comment.meta.nickname);
  72. });
  73. results.push(
  74. document.getElementById('discussion').classList.contains('hidden')
  75. );
  76. PrivateBin.DiscussionViewer.finishDiscussion();
  77. results.push(
  78. !document.getElementById('discussion').classList.contains('hidden') &&
  79. comments.length + 1 >= document.getElementById('commentcontainer').children.length
  80. );
  81. if (comments.length > 0) {
  82. if (commentKey >= comments.length) {
  83. commentKey = commentKey % comments.length;
  84. }
  85. PrivateBin.DiscussionViewer.highlightComment(comments[commentKey].id, fadeOut);
  86. results.push(
  87. document.getElementById('comment_' + comments[commentKey].id).classList.contains('highlight')
  88. );
  89. }
  90. // clicking "Add comment" button should open the reply form
  91. document.getElementById('commentcontainer').querySelector('button').click();
  92. results.push(
  93. !document.getElementById('reply').classList.contains('hidden')
  94. );
  95. document.querySelector('#reply #nickname').value = nickname;
  96. document.querySelector('#reply #replymessage').value = message;
  97. PrivateBin.DiscussionViewer.getReplyCommentId();
  98. results.push(
  99. PrivateBin.DiscussionViewer.getReplyNickname() === document.querySelector('#reply #nickname').value &&
  100. PrivateBin.DiscussionViewer.getReplyMessage() === document.querySelector('#reply #replymessage').value
  101. );
  102. var notificationResult = PrivateBin.DiscussionViewer.handleNotification(alertType === 'other' ? alert : alertType);
  103. if (alertType === 'loading') {
  104. results.push(notificationResult === false);
  105. } else {
  106. results.push(
  107. alertType === 'danger' ? (
  108. notificationResult.classList.contains('alert-danger') &&
  109. !notificationResult.classList.contains('alert-info')
  110. ) : (
  111. !notificationResult.classList.contains('alert-danger') &&
  112. notificationResult.classList.contains('alert-info')
  113. )
  114. );
  115. }
  116. clean();
  117. return results.every(element => element);
  118. }
  119. );
  120. });
  121. });