From ad1e419d42e5b0f64406adfeb554601ad8a32b53 Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Wed, 5 Apr 2017 20:48:03 -0400 Subject: [PATCH] Add tests for svgToImg --- lib/svg-to-img.js | 4 ++++ package.json | 4 +++- test/svg-to-img.spec.js | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/svg-to-img.spec.js diff --git a/lib/svg-to-img.js b/lib/svg-to-img.js index 7d7e975..266d86a 100644 --- a/lib/svg-to-img.js +++ b/lib/svg-to-img.js @@ -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'); diff --git a/package.json b/package.json index 89be141..72897b2 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/svg-to-img.spec.js b/test/svg-to-img.spec.js new file mode 100644 index 0000000..3740518 --- /dev/null +++ b/test/svg-to-img.spec.js @@ -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(); + }); + }); + }); + }); +});