DiscussionViewer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. document.body.innerHTML = (
  31. `<div id="discussion">
  32. <h4>Discussion</h4>
  33. <div id="commentcontainer"></div>
  34. </div>
  35. <div id="templates">
  36. <article id="commenttemplate" class="comment">
  37. <div class="commentmeta">
  38. <span class="nickname">name</span>
  39. <span class="commentdate">0000-00-00</span>
  40. </div>
  41. <div class="commentdata">c</div>
  42. <button class="btn btn-default btn-sm">Reply</button>
  43. </article>
  44. <p id="commenttailtemplate" class="comment">
  45. <button class="btn btn-default btn-sm">Add comment</button>
  46. </p>
  47. <div id="replytemplate" class="reply hidden">
  48. <input type="text" id="nickname" class="form-control" title="Optional nickname…"
  49. placeholder="Optional nickname…" />
  50. <textarea id="replymessage" class="replymessage form-control" cols="80" rows="7"></textarea>
  51. <br />
  52. <div id="replystatus" role="alert" class="statusmessage hidden alert">
  53. <span class="glyphicon" aria-hidden="true"></span>
  54. </div>
  55. <button id="replybutton" class="btn btn-default btn-sm">Post comment</button>
  56. </div>
  57. </div>
  58. `
  59. );
  60. PrivateBin.Model.init();
  61. PrivateBin.DiscussionViewer.init();
  62. results.push(
  63. !document.getElementById('discussion').classList.contains('hidden')
  64. );
  65. PrivateBin.DiscussionViewer.prepareNewDiscussion();
  66. results.push(
  67. document.getElementById('discussion').classList.contains('hidden')
  68. );
  69. comments.forEach(function (comment) {
  70. comment.id = comment.idArray.join('');
  71. comment.parentid = comment.parentidArray.join('');
  72. PrivateBin.DiscussionViewer.addComment(PrivateBin.Helper.CommentFactory(comment), comment.data, comment.meta.nickname);
  73. });
  74. results.push(
  75. document.getElementById('discussion').classList.contains('hidden')
  76. );
  77. PrivateBin.DiscussionViewer.finishDiscussion();
  78. results.push(
  79. !document.getElementById('discussion').classList.contains('hidden') &&
  80. comments.length + 1 >= document.getElementById('commentcontainer').children.length
  81. );
  82. if (comments.length > 0) {
  83. if (commentKey >= comments.length) {
  84. commentKey = commentKey % comments.length;
  85. }
  86. PrivateBin.DiscussionViewer.highlightComment(comments[commentKey].id, fadeOut);
  87. results.push(
  88. document.getElementById('comment_' + comments[commentKey].id).classList.contains('highlight')
  89. );
  90. }
  91. // clicking "Add comment" button should open the reply form
  92. document.getElementById('commentcontainer').querySelector('button').click();
  93. results.push(
  94. !document.getElementById('reply').classList.contains('hidden')
  95. );
  96. document.querySelector('#reply #nickname').value = nickname;
  97. document.querySelector('#reply #replymessage').value = message;
  98. PrivateBin.DiscussionViewer.getReplyCommentId();
  99. results.push(
  100. PrivateBin.DiscussionViewer.getReplyNickname() === document.querySelector('#reply #nickname').value &&
  101. PrivateBin.DiscussionViewer.getReplyMessage() === document.querySelector('#reply #replymessage').value
  102. );
  103. var notificationResult = PrivateBin.DiscussionViewer.handleNotification(alertType === 'other' ? alert : alertType);
  104. if (alertType === 'loading') {
  105. results.push(notificationResult === false);
  106. } else {
  107. results.push(
  108. alertType === 'danger' ? (
  109. notificationResult.classList.contains('alert-danger') &&
  110. !notificationResult.classList.contains('alert-info')
  111. ) : (
  112. !notificationResult.classList.contains('alert-danger') &&
  113. notificationResult.classList.contains('alert-info')
  114. )
  115. );
  116. }
  117. clean();
  118. return results.every(element => element);
  119. }
  120. ));
  121. });
  122. });
  123. });