Add tests for svgToImg

This commit is contained in:
Paul Melnikow 2017-04-05 20:48:03 -04:00 committed by Thaddee Tyl
parent 3905424d1c
commit ad1e419d42
3 changed files with 50 additions and 1 deletions

View File

@ -11,6 +11,7 @@ module.exports = function (svg, format, out, cb) {
if (imgCache.has(cacheIndex)) {
// We own a cache for this svg conversion.
(new DataStream(imgCache.get(cacheIndex))).pipe(out);
cb && cb();
return;
}
@ -28,6 +29,9 @@ module.exports = function (svg, format, out, cb) {
});
};
// To simplify testing.
module.exports._imgCache = imgCache;
// Fake stream from the cache.
var Readable = require('stream').Readable;
var util = require('util');

View File

@ -58,6 +58,8 @@
"eslint": "^3.18.0",
"is-png": "^1.0.0",
"is-svg": "^2.1.0",
"mocha": "^3.2.0"
"memory-streams": "^0.1.2",
"mocha": "^3.2.0",
"sinon": "^2.1.0"
}
}

43
test/svg-to-img.spec.js Normal file
View File

@ -0,0 +1,43 @@
const assert = require('assert');
const sinon = require('sinon');
const WritableStream = require('memory-streams').WritableStream;
const isPng = require('is-png');
const badge = require('../lib/badge');
const svg2img = require('../lib/svg-to-img.js');
describe('The rasterizer', function () {
let cacheGet;
beforeEach(function () { cacheGet = sinon.spy(svg2img._imgCache, 'get'); });
afterEach(function () { cacheGet.restore(); });
it('should produce PNG', function(done) {
badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => {
const out = new WritableStream();
svg2img(svg, 'png', out, () => {
assert.ok(isPng(out.toBuffer()));
done();
});
});
});
it('should cache its results', function(done) {
badge({ text: ['will-this', 'be-cached?'], format: 'svg' }, svg => {
const out1 = new WritableStream();
const out2 = new WritableStream();
svg2img(svg, 'png', out1, () => {
assert.ok(isPng(out1.toBuffer()));
assert.equal(cacheGet.called, false);
svg2img(svg, 'png', out2, () => {
assert.ok(isPng(out2.toBuffer()));
assert.ok(cacheGet.calledOnce);
done();
});
});
});
});
});