AttachmentViewer.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. isImage = mimeType.substring(0, 6) === 'image/',
  20. results = [];
  21. prefix = prefix.replace(/%(s|d)/g, '%%');
  22. postfix = postfix.replace(/%(s|d)/g, '%%');
  23. $('body').html(
  24. '<div id="attachment" role="alert" class="hidden alert ' +
  25. 'alert-info"><span class="glyphicon glyphicon-download-' +
  26. 'alt" aria-hidden="true"></span> <a class="alert-link">' +
  27. 'Download attachment</a></div><div id="attachmentPrevie' +
  28. 'w" class="hidden"></div>'
  29. );
  30. $.PrivateBin.AttachmentViewer.init();
  31. results.push(
  32. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  33. $('#attachment').hasClass('hidden') &&
  34. $('#attachmentPreview').hasClass('hidden')
  35. );
  36. if (filename.length) {
  37. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  38. } else {
  39. $.PrivateBin.AttachmentViewer.setAttachment(data);
  40. }
  41. var attachement = $.PrivateBin.AttachmentViewer.getAttachment()
  42. results.push(
  43. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  44. $('#attachment').hasClass('hidden') &&
  45. $('#attachmentPreview').hasClass('hidden') &&
  46. attachement[0] === data &&
  47. attachement[1] === filename
  48. );
  49. $.PrivateBin.AttachmentViewer.showAttachment();
  50. results.push(
  51. !$('#attachment').hasClass('hidden') &&
  52. (isImage ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  53. );
  54. $.PrivateBin.AttachmentViewer.hideAttachment();
  55. results.push(
  56. $('#attachment').hasClass('hidden') &&
  57. (isImage ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  58. );
  59. if (isImage) {
  60. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  61. results.push($('#attachmentPreview').hasClass('hidden'));
  62. }
  63. $.PrivateBin.AttachmentViewer.showAttachment();
  64. results.push(
  65. !$('#attachment').hasClass('hidden') &&
  66. (isImage ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  67. );
  68. var element = $('<div></div>');
  69. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  70. if (filename.length) {
  71. results.push(
  72. element.children()[0].href === data &&
  73. element.children()[0].getAttribute('download') === filename &&
  74. element.children()[0].text === prefix + filename + postfix
  75. );
  76. } else {
  77. results.push(element.children()[0].href === data);
  78. }
  79. $.PrivateBin.AttachmentViewer.removeAttachment();
  80. results.push(
  81. $('#attachment').hasClass('hidden') &&
  82. $('#attachmentPreview').hasClass('hidden')
  83. );
  84. clean();
  85. return results.every(element => element);
  86. }
  87. );
  88. });
  89. });