Commit 3422a281 by Edgar HIPP

Add test for auto resize

parent 520fff6b
...@@ -8,6 +8,7 @@ const JSZip = require("jszip"); ...@@ -8,6 +8,7 @@ const JSZip = require("jszip");
const ImageModule = require("./index.js"); const ImageModule = require("./index.js");
const testutils = require("docxtemplater/js/tests/utils"); const testutils = require("docxtemplater/js/tests/utils");
const shouldBeSame = testutils.shouldBeSame; const shouldBeSame = testutils.shouldBeSame;
const sizeOf = require('image-size');
const fileNames = [ const fileNames = [
"imageExample.docx", "imageExample.docx",
...@@ -27,12 +28,13 @@ const fileNames = [ ...@@ -27,12 +28,13 @@ const fileNames = [
"expectedTagImage.pptx", "expectedTagImage.pptx",
"tagImageCentered.pptx", "tagImageCentered.pptx",
"expectedTagImageCentered.pptx", "expectedTagImageCentered.pptx",
"expectedInlineResize.docx"
]; ];
beforeEach(function () { beforeEach(function () {
this.opts = { this.opts = {
getImage: function (tagValue) { getImage: function (tagValue) {
return fs.readFileSync(tagValue, "binary"); return fs.readFileSync(tagValue);
}, },
getSize: function () { getSize: function () {
return [150, 150]; return [150, 150];
...@@ -41,10 +43,8 @@ beforeEach(function () { ...@@ -41,10 +43,8 @@ beforeEach(function () {
}; };
this.loadAndRender = function () { this.loadAndRender = function () {
const fileType = testutils.pptX[this.name] ? "pptx" : "docx"; const file = testutils.createDoc(this.name)
const file = fileType === "pptx" ? testutils.pptX[this.name] : testutils.docX[this.name];
this.doc = new Docxtemplater(); this.doc = new Docxtemplater();
this.doc.setOptions({fileType});
const inputZip = new JSZip(file.loadedContent); const inputZip = new JSZip(file.loadedContent);
this.doc.loadZip(inputZip).setData(this.data); this.doc.loadZip(inputZip).setData(this.data);
const imageModule = new ImageModule(this.opts); const imageModule = new ImageModule(this.opts);
...@@ -122,6 +122,17 @@ function testStart() { ...@@ -122,6 +122,17 @@ function testStart() {
this.loadAndRender(); this.loadAndRender();
}); });
it("should work with auto resize", function () {
this.name = "imageInlineExample.docx";
this.expectedName = "expectedInlineResize.docx";
this.opts.getSize = function (img, tagValue, tagName) {
const sizeObj = sizeOf(img);
return [sizeObj.width, sizeObj.height];
}
this.data = {firefox: "examples/image.png"};
this.loadAndRender();
});
it("should work with base64 data", function () { it("should work with base64 data", function () {
const base64Image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QIJBywfp3IOswAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAkUlEQVQY052PMQqDQBREZ1f/d1kUm3SxkeAF/FdIjpOcw2vpKcRWCwsRPMFPsaIQSIoMr5pXDGNUFd9j8TOn7kRW71fvO5HTq6qqtnWtzh20IqE3YXtL0zyKwAROQLQ5l/c9gHjfKK6wMZjADE6s49Dver4/smEAc2CuqgwAYI5jU9NcxhHEy60sni986H9+vwG1yDHfK1jitgAAAABJRU5ErkJggg=="; const base64Image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QIJBywfp3IOswAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAkUlEQVQY052PMQqDQBREZ1f/d1kUm3SxkeAF/FdIjpOcw2vpKcRWCwsRPMFPsaIQSIoMr5pXDGNUFd9j8TOn7kRW71fvO5HTq6qqtnWtzh20IqE3YXtL0zyKwAROQLQ5l/c9gHjfKK6wMZjADE6s49Dver4/smEAc2CuqgwAYI5jU9NcxhHEy60sni986H9+vwG1yDHfK1jitgAAAABJRU5ErkJggg==";
this.name = "imageExample.docx"; this.name = "imageExample.docx";
...@@ -155,7 +166,6 @@ function testStart() { ...@@ -155,7 +166,6 @@ function testStart() {
testutils.setExamplesDirectory(path.resolve(__dirname, "..", "examples")); testutils.setExamplesDirectory(path.resolve(__dirname, "..", "examples"));
testutils.setStartFunction(testStart); testutils.setStartFunction(testStart);
fileNames.forEach(function (filename) { fileNames.forEach(function (filename) {
const loader = /\.pptx$/.test(filename) ? testutils.loadPptx : testutils.loadDocx; testutils.loadFile(filename, testutils.loadDocument);
testutils.loadFile(filename, loader);
}); });
testutils.start(); testutils.start();
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
"scripts": { "scripts": {
"test:coverage": "istanbul cover _mocha -- es6/test.js", "test:coverage": "istanbul cover _mocha -- es6/test.js",
"compile": "rimraf js && mkdirp js && babel es6 --out-dir js", "compile": "rimraf js && mkdirp js && babel es6 --out-dir js",
"preversion": "npm test && npm run compile && npm run browserify && npm run uglify", "preversion": "npm test && npm run browserify && npm run uglify",
"test:compiled": "mocha js/test.js", "test:compiled": "mocha js/test.js",
"test:es6": "mocha es6/test.js", "test:es6": "mocha es6/test.js",
"lint": "eslint .", "lint": "eslint .",
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
"chai": "^3.4.1", "chai": "^3.4.1",
"docxtemplater": "^3.0.0", "docxtemplater": "^3.0.0",
"eslint": "^3.15.0", "eslint": "^3.15.0",
"image-size": "^0.5.1",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"jszip": "^2.6.1", "jszip": "^2.6.1",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
......
...@@ -28,7 +28,7 @@ var ImageModule=require('docxtemplater-image-module') ...@@ -28,7 +28,7 @@ var ImageModule=require('docxtemplater-image-module')
var opts = {} var opts = {}
opts.centered = false; opts.centered = false;
opts.getImage=function(tagValue, tagName) { opts.getImage=function(tagValue, tagName) {
return fs.readFileSync(tagValue,'binary'); return fs.readFileSync(tagValue);
} }
opts.getSize=function(img,tagValue, tagName) { opts.getSize=function(img,tagValue, tagName) {
...@@ -131,10 +131,9 @@ Building in the browser ...@@ -131,10 +131,9 @@ Building in the browser
You can build a release for the browser with the following commands You can build a release for the browser with the following commands
``` ```
npm install -g gulp jasmine-node uglify-js browserify
npm install npm install
npm run preversion
npm run compile npm run compile
mkdir build -p
browserify -r ./js/index.js -s ImageModule > build/imagemodule.js
uglifyjs build/imagemodule.js > build/imagemodule.min.js # Optional
``` ```
You will have build/imagemodule.js.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment