Skip to content

Commit

Permalink
Test mammoth.images.imgElement directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Aug 31, 2022
1 parent 93eb384 commit d67ddaf
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion test/images.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var assert = require("assert");
var hamjest = require("hamjest");
var assertThat = hamjest.assertThat;
var contains = hamjest.contains;
var equalTo = hamjest.equalTo;
var hasProperties = hamjest.hasProperties;

var mammoth = require("../");
Expand All @@ -25,10 +26,62 @@ test('mammoth.images.dataUri() encodes images in base64', function() {
},
contentType: "image/jpeg"
});

return mammoth.images.dataUri(image).then(function(result) {
assertThat(result, contains(
hasProperties({tag: hasProperties({attributes: {"src": "data:image/jpeg;base64,YWJj"}})})
));
});
});


test('mammoth.images.imgElement()', {
'when element does not have alt text then alt attribute is not set': function() {
var imageBuffer = new Buffer("abc");
var image = new documents.Image({
readImage: function(encoding) {
return promises.when(imageBuffer.toString(encoding));
},
contentType: "image/jpeg"
});

var result = mammoth.images.imgElement(function(image) {
return {src: "<src>"};
})(image);

return result.then(function(result) {
assertThat(result, contains(
hasProperties({
tag: hasProperties({
attributes: equalTo({src: "<src>"})
})
})
));
});
},

'when element has alt text then alt attribute is set': function() {
var imageBuffer = new Buffer("abc");
var image = new documents.Image({
readImage: function(encoding) {
return promises.when(imageBuffer.toString(encoding));
},
contentType: "image/jpeg",
altText: "<alt>"
});

var result = mammoth.images.imgElement(function(image) {
return {src: "<src>"};
})(image);

return result.then(function(result) {
assertThat(result, contains(
hasProperties({
tag: hasProperties({
attributes: equalTo({alt: "<alt>", src: "<src>"})
})
})
));
});
}
});

0 comments on commit d67ddaf

Please sign in to comment.