DiscussionViewer.js 6.5 KB

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