From 7b7b9539c79e4c2ababb59ca857f8587bb9a6df3 Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Fri, 19 Feb 2016 12:09:36 +0900 Subject: [PATCH] Fix existing tests and add more tests Added two tests: * for pngquant options * for error handling --- test/fixtures/test-corrupt.png | Bin 0 -> 100 bytes test/test.js | 78 ++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 test/fixtures/test-corrupt.png diff --git a/test/fixtures/test-corrupt.png b/test/fixtures/test-corrupt.png new file mode 100644 index 0000000000000000000000000000000000000000..3f90ed842625e79e8f4c9afea78bc55dcf79b55f GIT binary patch literal 100 zcmeAS@N?&700Ul650@YY2F5QS%)tf}JALhb7Xw2BgQtsQNX4x;f6M2@Ua!2*U4Q*n z^?a*Zw#x=9n&WPIZtMsZ2wkCfbDFOACRyu^$;bO9-%;_BS$_BTv2(vY=gejW0Fe(U Aod5s; literal 0 HcmV?d00001 diff --git a/test/test.js b/test/test.js index df6b42c..b939c25 100644 --- a/test/test.js +++ b/test/test.js @@ -4,38 +4,74 @@ import {read} from 'vinyl-file'; import test from 'ava'; import imageminPngquant from '../'; -test('optimize a PNG', async t => { - const file = await read('fixtures/test.png'); +test.cb('optimize a PNG', t => { + read('fixtures/test.png').then(file => { + const stream = imageminPngquant()(); + const {length: originalSize} = file.contents; - const stream = imageminPngquant()(); - const {length: size} = file.contents; + stream.on('data', data => { + t.true(data.contents.length < originalSize); + t.true(isPng(data.contents)); + t.end(); + }); - stream.on('data', data => { - t.true(data.contents.length > size, data.contents.length); - t.true(isPng(data.contents)); + stream.end(file); }); - - stream.end(file); }); -test('skip optimizing a non-PNG file', async t => { - const file = await read(__filename); +test.cb('support pngquant options', t => { + read('fixtures/test.png').then(file => { + const stream = imageminPngquant({ + speed: 10, + quality: 100 + })(); + + stream.on('data', data => { + t.true(data.contents.length > 30000); + t.end(); + }); + + stream.end(file); + }); +}); - const stream = imageminPngquant()(); - const buf = file.contents; +test.cb('skip optimizing a non-PNG file', t => { + read(__filename).then(file => { + const stream = imageminPngquant()(); + const buf = file.contents; - stream.on('data', data => t.true(bufferEquals(data.contents, buf))); + stream.on('data', data => { + t.true(bufferEquals(data.contents, buf)); + t.end(); + }); - stream.end(file); + stream.end(file); + }); }); -test('skip optimizing an already optimized PNG', async t => { - const file = await read('fixtures/test-smallest.png'); +test.cb('skip optimizing an already optimized PNG', t => { + read('fixtures/test-smallest.png').then(file => { + const stream = imageminPngquant()(); + const buf = file.contents; - const stream = imageminPngquant()(); - const buf = file.contents; + stream.on('data', data => { + t.true(bufferEquals(data.contents, buf)); + t.end(); + }); - stream.on('data', data => t.true(bufferEquals(data.contents, buf))); + stream.end(file); + }); +}); + +test.cb('emit an error when optimizing a corrupt PNG', t => { + read('fixtures/test-corrupt.png').then(file => { + const stream = imageminPngquant()(); - stream.end(file); + stream.on('error', err => { + t.regex(err.message, /cannot decode image from stdin/); + t.end(); + }); + + stream.end(file); + }); });