AttachmentViewer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 'string',
  13. 'string',
  14. 'string',
  15. 'string',
  16. function (mimeType, rawdata, filename, prefix, postfix) {
  17. var clean = jsdom(),
  18. data = 'data:' + mimeType + ';base64,' + btoa(rawdata),
  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. // mock createObjectURL for jsDOM
  36. if (typeof window.URL.createObjectURL === 'undefined') {
  37. Object.defineProperty(
  38. window.URL,
  39. 'createObjectURL',
  40. {value: function(blob) {
  41. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  42. }}
  43. )
  44. }
  45. $.PrivateBin.AttachmentViewer.init();
  46. results.push(
  47. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  48. $('#attachment').hasClass('hidden') &&
  49. $('#attachmentPreview').hasClass('hidden')
  50. );
  51. if (filename.length) {
  52. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  53. } else {
  54. $.PrivateBin.AttachmentViewer.setAttachment(data);
  55. }
  56. // beyond this point we will get the blob URL instead of the data
  57. data = window.URL.createObjectURL(data);
  58. var attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  59. results.push(
  60. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  61. $('#attachment').hasClass('hidden') &&
  62. $('#attachmentPreview').hasClass('hidden') &&
  63. attachment[0] === data &&
  64. attachment[1] === filename
  65. );
  66. $.PrivateBin.AttachmentViewer.showAttachment();
  67. results.push(
  68. !$('#attachment').hasClass('hidden') &&
  69. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  70. );
  71. $.PrivateBin.AttachmentViewer.hideAttachment();
  72. results.push(
  73. $('#attachment').hasClass('hidden') &&
  74. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  75. );
  76. if (previewSupported) {
  77. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  78. results.push($('#attachmentPreview').hasClass('hidden'));
  79. }
  80. $.PrivateBin.AttachmentViewer.showAttachment();
  81. results.push(
  82. !$('#attachment').hasClass('hidden') &&
  83. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  84. );
  85. var element = $('<div></div>');
  86. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  87. if (filename.length) {
  88. results.push(
  89. element.children()[0].href === data &&
  90. element.children()[0].getAttribute('download') === filename &&
  91. element.children()[0].text === prefix + filename + postfix
  92. );
  93. } else {
  94. results.push(element.children()[0].href === data);
  95. }
  96. $.PrivateBin.AttachmentViewer.removeAttachment();
  97. results.push(
  98. $('#attachment').hasClass('hidden') &&
  99. $('#attachmentPreview').hasClass('hidden')
  100. );
  101. clean();
  102. return results.every(element => element);
  103. }
  104. );
  105. });
  106. });