DiscussionViewer.js 5.8 KB

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