AttachmentViewer.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. var common = require('../common');
  3. describe('AttachmentViewer', function () {
  4. describe('setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo', function () {
  5. this.timeout(30000);
  6. before(function () {
  7. cleanup();
  8. });
  9. jsc.property(
  10. 'displays & hides data as requested',
  11. common.jscMimeTypes(),
  12. jsc.nearray(common.jscBase64String()),
  13. 'string',
  14. 'string',
  15. 'string',
  16. function (mimeType, base64, filename, prefix, postfix) {
  17. var clean = jsdom(),
  18. data = 'data:' + mimeType + ';base64,' + base64.join(''),
  19. previewSupported = (
  20. mimeType.substring(0, 6) === 'image/' ||
  21. mimeType.substring(0, 6) === 'audio/' ||
  22. mimeType.substring(0, 6) === 'video/' ||
  23. mimeType.match(/\/pdf/i)
  24. ),
  25. results = [];
  26. prefix = prefix.replace(/%(s|d)/g, '%%');
  27. postfix = postfix.replace(/%(s|d)/g, '%%');
  28. $('body').html(
  29. '<div id="attachment" role="alert" class="hidden alert ' +
  30. 'alert-info"><span class="glyphicon glyphicon-download-' +
  31. 'alt" aria-hidden="true"></span> <a class="alert-link">' +
  32. 'Download attachment</a></div><div id="attachmentPrevie' +
  33. 'w" class="hidden"></div>'
  34. );
  35. $.PrivateBin.AttachmentViewer.init();
  36. results.push(
  37. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  38. $('#attachment').hasClass('hidden') &&
  39. $('#attachmentPreview').hasClass('hidden')
  40. );
  41. if (filename.length) {
  42. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  43. } else {
  44. $.PrivateBin.AttachmentViewer.setAttachment(data);
  45. }
  46. var attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  47. results.push(
  48. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  49. $('#attachment').hasClass('hidden') &&
  50. $('#attachmentPreview').hasClass('hidden') &&
  51. attachment[0] === data &&
  52. attachment[1] === filename
  53. );
  54. $.PrivateBin.AttachmentViewer.showAttachment();
  55. results.push(
  56. !$('#attachment').hasClass('hidden') &&
  57. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  58. );
  59. $.PrivateBin.AttachmentViewer.hideAttachment();
  60. results.push(
  61. $('#attachment').hasClass('hidden') &&
  62. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  63. );
  64. if (previewSupported) {
  65. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  66. results.push($('#attachmentPreview').hasClass('hidden'));
  67. }
  68. $.PrivateBin.AttachmentViewer.showAttachment();
  69. results.push(
  70. !$('#attachment').hasClass('hidden') &&
  71. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  72. );
  73. var element = $('<div></div>');
  74. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  75. if (filename.length) {
  76. results.push(
  77. element.children()[0].href === data &&
  78. element.children()[0].getAttribute('download') === filename &&
  79. element.children()[0].text === prefix + filename + postfix
  80. );
  81. } else {
  82. results.push(element.children()[0].href === data);
  83. }
  84. $.PrivateBin.AttachmentViewer.removeAttachment();
  85. results.push(
  86. $('#attachment').hasClass('hidden') &&
  87. $('#attachmentPreview').hasClass('hidden')
  88. );
  89. clean();
  90. return results.every(element => element);
  91. }
  92. );
  93. });
  94. });