images.tests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var assert = require("assert");
  2. var hamjest = require("hamjest");
  3. var assertThat = hamjest.assertThat;
  4. var contains = hamjest.contains;
  5. var equalTo = hamjest.equalTo;
  6. var hasProperties = hamjest.hasProperties;
  7. var mammoth = require("../");
  8. var documents = require("../lib/documents");
  9. var promises = require("../lib/promises");
  10. var test = require("./test")(module);
  11. test('mammoth.images.inline() should be an alias of mammoth.images.imgElement()', function() {
  12. assert.ok(mammoth.images.inline === mammoth.images.imgElement);
  13. });
  14. test('mammoth.images.dataUri() encodes images in base64', function() {
  15. var imageBuffer = new Buffer("abc");
  16. var image = new documents.Image({
  17. readImage: function(encoding) {
  18. return promises.when(imageBuffer.toString(encoding));
  19. },
  20. contentType: "image/jpeg"
  21. });
  22. return mammoth.images.dataUri(image).then(function(result) {
  23. assertThat(result, contains(
  24. hasProperties({tag: hasProperties({attributes: {"src": "data:image/jpeg;base64,YWJj"}})})
  25. ));
  26. });
  27. });
  28. test('mammoth.images.imgElement()', {
  29. 'when element does not have alt text then alt attribute is not set': function() {
  30. var imageBuffer = new Buffer("abc");
  31. var image = new documents.Image({
  32. readImage: function(encoding) {
  33. return promises.when(imageBuffer.toString(encoding));
  34. },
  35. contentType: "image/jpeg"
  36. });
  37. var result = mammoth.images.imgElement(function(image) {
  38. return {src: "<src>"};
  39. })(image);
  40. return result.then(function(result) {
  41. assertThat(result, contains(
  42. hasProperties({
  43. tag: hasProperties({
  44. attributes: equalTo({src: "<src>"})
  45. })
  46. })
  47. ));
  48. });
  49. },
  50. 'when element has alt text then alt attribute is set': function() {
  51. var imageBuffer = new Buffer("abc");
  52. var image = new documents.Image({
  53. readImage: function(encoding) {
  54. return promises.when(imageBuffer.toString(encoding));
  55. },
  56. contentType: "image/jpeg",
  57. altText: "<alt>"
  58. });
  59. var result = mammoth.images.imgElement(function(image) {
  60. return {src: "<src>"};
  61. })(image);
  62. return result.then(function(result) {
  63. assertThat(result, contains(
  64. hasProperties({
  65. tag: hasProperties({
  66. attributes: equalTo({alt: "<alt>", src: "<src>"})
  67. })
  68. })
  69. ));
  70. });
  71. },
  72. 'image alt text can be overridden by alt attribute returned from function': function() {
  73. var imageBuffer = new Buffer("abc");
  74. var image = new documents.Image({
  75. readImage: function(encoding) {
  76. return promises.when(imageBuffer.toString(encoding));
  77. },
  78. contentType: "image/jpeg",
  79. altText: "<alt>"
  80. });
  81. var result = mammoth.images.imgElement(function(image) {
  82. return {alt: "<alt override>", src: "<src>"};
  83. })(image);
  84. return result.then(function(result) {
  85. assertThat(result, contains(
  86. hasProperties({
  87. tag: hasProperties({
  88. attributes: equalTo({alt: "<alt override>", src: "<src>"})
  89. })
  90. })
  91. ));
  92. });
  93. }
  94. });