AttachmentViewer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. jsc.property(
  7. 'displays & hides data as requested',
  8. common.jscMimeTypes(),
  9. 'string',
  10. 'string',
  11. 'string',
  12. 'string',
  13. function (mimeType, rawdata, filename, prefix, postfix) {
  14. var clean = jsdom(),
  15. data = 'data:' + mimeType + ';base64,' + btoa(rawdata),
  16. previewSupported = (
  17. mimeType.substring(0, 6) === 'image/' ||
  18. mimeType.substring(0, 6) === 'audio/' ||
  19. mimeType.substring(0, 6) === 'video/' ||
  20. mimeType.match(/\/pdf/i)
  21. ),
  22. results = [];
  23. prefix = prefix.replace(/%(s|d)/g, '%%');
  24. postfix = postfix.replace(/%(s|d)/g, '%%');
  25. $('body').html(
  26. '<div id="attachment" role="alert" class="hidden alert ' +
  27. 'alert-info"><span class="glyphicon glyphicon-download-' +
  28. 'alt" aria-hidden="true"></span> <a class="alert-link">' +
  29. 'Download attachment</a></div><div id="attachmentPrevie' +
  30. 'w" class="hidden"></div>'
  31. );
  32. // mock createObjectURL for jsDOM
  33. if (typeof window.URL.createObjectURL === 'undefined') {
  34. Object.defineProperty(
  35. window.URL,
  36. 'createObjectURL',
  37. {value: function(blob) {
  38. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  39. }}
  40. )
  41. }
  42. $.PrivateBin.AttachmentViewer.init();
  43. results.push(
  44. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  45. $('#attachment').hasClass('hidden') &&
  46. $('#attachmentPreview').hasClass('hidden')
  47. );
  48. if (filename.length) {
  49. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  50. } else {
  51. $.PrivateBin.AttachmentViewer.setAttachment(data);
  52. }
  53. // beyond this point we will get the blob URL instead of the data
  54. data = window.URL.createObjectURL(data);
  55. var attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  56. results.push(
  57. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  58. $('#attachment').hasClass('hidden') &&
  59. $('#attachmentPreview').hasClass('hidden') &&
  60. attachment[0] === data &&
  61. attachment[1] === filename
  62. );
  63. $.PrivateBin.AttachmentViewer.showAttachment();
  64. results.push(
  65. !$('#attachment').hasClass('hidden') &&
  66. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  67. );
  68. $.PrivateBin.AttachmentViewer.hideAttachment();
  69. results.push(
  70. $('#attachment').hasClass('hidden') &&
  71. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  72. );
  73. if (previewSupported) {
  74. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  75. results.push($('#attachmentPreview').hasClass('hidden'));
  76. }
  77. $.PrivateBin.AttachmentViewer.showAttachment();
  78. results.push(
  79. !$('#attachment').hasClass('hidden') &&
  80. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  81. );
  82. var element = $('<div></div>');
  83. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  84. if (filename.length) {
  85. results.push(
  86. element.children()[0].href === data &&
  87. element.children()[0].getAttribute('download') === filename &&
  88. element.children()[0].text === prefix + filename + postfix
  89. );
  90. } else {
  91. results.push(element.children()[0].href === data);
  92. }
  93. $.PrivateBin.AttachmentViewer.removeAttachment();
  94. results.push(
  95. $('#attachment').hasClass('hidden') &&
  96. $('#attachmentPreview').hasClass('hidden')
  97. );
  98. clean();
  99. return results.every(element => element);
  100. }
  101. );
  102. });
  103. });