lundi 2 mars 2015

Nodejs test image resizing with mocha

I've used graphicmagick to resize a image in my nodejs app.


The problem is when writing the unit tests, I cant seem to find any direction or examples on this. Does it make sense that I test the image resizing, seeing that I'm using a third-party module? If yes, how could I write a test for my code?



// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true });
var util = require('util');

// get reference to S3 client
var s3 = new AWS.S3();

var _800px = {
width: 800,
destinationPath: "large"
};

var _500px = {
width: 500,
destinationPath: "medium"
};

var _200px = {
width: 200,
destinationPath: "small"
};

var _45px = {
width: 45,
destinationPath: "thumbnail"
};

var _sizesArray = [_800px, _500px, _200px, _45px];

var len = _sizesArray.length;

// handler for dev environment

exports.GruntHandler = function (filepath) {

console.log("Path to file is: " + filepath);

// get the file name
var srcFile = filepath.split("/").pop();

var dstnFile = "dst";

// Infer the image type.
var typeMatch = srcFile.match(/\.([^.]*)$/);
if (!typeMatch) {
console.error('unable to infer image type for key ' + srcFile);
return;
}
var imageType = typeMatch[1];
if (imageType != "jpg" && imageType != "png") {
console.log('skipping non-image ' + srcFile);
return;
}

for (var i = 0; i<len; i++) {

// Transform the image buffer in memory.
gm(filepath)
.resize(_sizesArray[i].width)
.write(dstnFile + "/" + _sizesArray[i].destinationPath + "/" + srcFile, function (err) {
if (err) {
console.log(err);
}
});
}


console.log(" grunt handler called!");
};

Aucun commentaire:

Enregistrer un commentaire