From b96982ffacda7e233a081121130287c5d63068f1 Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Thu, 24 Aug 2017 00:47:12 +0300 Subject: [PATCH] add tests for coa --- .jshintrc | 3 +- package.json | 8 +- test/coa/_index.js | 169 ++++++++++++++++++++++++++++++ test/coa/test.svg | 1 + test/coa/testFolderWithNoSvg/test | 0 test/coa/testSvg/test.1.svg | 1 + test/coa/testSvg/test.svg | 1 + test/mocha.opts | 1 + 8 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 test/coa/_index.js create mode 100644 test/coa/test.svg create mode 100644 test/coa/testFolderWithNoSvg/test create mode 100644 test/coa/testSvg/test.1.svg create mode 100644 test/coa/testSvg/test.svg diff --git a/.jshintrc b/.jshintrc index 7a26e42f..cfe3116f 100644 --- a/.jshintrc +++ b/.jshintrc @@ -3,7 +3,8 @@ "phantom": false, "describe": false, "before": false, - "it": false + "it": false, + "beforeEach": false }, "node": true, "strict": "global", diff --git a/package.json b/package.json index 3839ed58..ef31e48a 100644 --- a/package.json +++ b/package.json @@ -48,12 +48,14 @@ "jshint": "jshint --show-non-errors ." }, "dependencies": { - "sax": "~1.2.1", "coa": "~1.0.1", - "js-yaml": "~3.7.0", "colors": "~1.1.2", + "csso": "~2.3.1", + "fs-extra": "^4.0.1", + "js-yaml": "~3.7.0", "mkdirp": "~0.5.1", - "csso": "~2.3.1" + "mock-stdin": "^0.3.1", + "sax": "~1.2.1" }, "devDependencies": { "mocha": "~3.2.0", diff --git a/test/coa/_index.js b/test/coa/_index.js new file mode 100644 index 00000000..9621cbaf --- /dev/null +++ b/test/coa/_index.js @@ -0,0 +1,169 @@ +'use strict'; + +const fs = require('fs'), + svgo = require(process.env.COVERAGE ? + '../../lib-cov/svgo/coa.js' : + '../../lib/svgo/coa.js').api, + path = require('path'), + svgPath = path.resolve(__dirname, 'test.svg'), + svgFolderPath = path.resolve(__dirname, 'testSvg'), + fse = require('fs-extra'); + +describe('coa', function() { + let output; + + beforeEach(function() { + output = ''; + + if(fs.existsSync('temp')) { + fse.removeSync('temp'); + } + + fs.mkdirSync('temp'); + }); + + function replaceConsoleLog() { + const initialConsoleLog = global.console.log; + + global.console.log = function() { + output += arguments[0]; + + initialConsoleLog.apply(console, arguments); + }; + } + + function replaceConsoleError() { + const initialConsoleError = global.console.error; + + global.console.error = function() { + output += arguments[0]; + + initialConsoleError.apply(console, arguments); + }; + } + + function calcFolderSvgWeight(folderPath) { + return fs.readdirSync(folderPath).reduce((initWeight, fileName) => { + return initWeight + + (/.svg/.test(fileName) ? fs.statSync(folderPath + '/' + fileName).size : 0); + }, 0); + } + + it('should throw an error if "config" can not be parsed', function(done) { + replaceConsoleError(); + + svgo({ input: svgPath, config: '{' }) + .then(() => done( /Error: Couldn't parse config JSON/.test(output) ? null : 'Error was not thrown' )); + }); + + it('should work properly with string input', function(done) { + svgo({ string: fs.readFileSync(svgPath, 'utf8'), output: 'temp.svg' }) + .then(done); + }); + + it('should optimize folder', function(done) { + const initWeight = calcFolderSvgWeight(svgFolderPath); + + svgo({ folder: svgFolderPath }) + .then(() => { + const optimizedWeight = calcFolderSvgWeight(svgFolderPath); + + done(initWeight <= optimizedWeight ? null : 'Folder was not optimized'); + }); + }); + + it('should optimize file', function(done) { + const initialFileLength = fs.readFileSync(path.resolve(__dirname, 'test.svg')).length; + + svgo({ input: svgPath, output: 'temp.svg' }) + .then(() => { + const optimizedFileLength = fs.readFileSync('temp.svg').length; + + done(optimizedFileLength <= initialFileLength ? null : 'File was not optimized'); + }); + }); + + it('should optimize file from process.stdin', function(done) { + const initialFile = fs.readFileSync(path.resolve(__dirname, 'test.svg')); + + const stdin = require('mock-stdin').stdin(); + + setTimeout(() => { stdin.send(initialFile, 'ascii').end(); }, 0); + + svgo({ input: '-', output: 'temp.svg', string: fs.readFileSync(svgPath, 'utf8') }) + .then(() => { + const optimizedFileLength = fs.readFileSync('temp.svg').length; + + done(optimizedFileLength <= initialFile.length ? null : 'Files were not optimized'); + }); + }); + + it('should optimize folder, when it stated in input', function(done) { + const initWeight = calcFolderSvgWeight(svgFolderPath); + + svgo({ input: svgFolderPath, output: 'temp' }) + .then(() => { + let optimizedWeight = calcFolderSvgWeight(svgFolderPath); + + done(initWeight <= optimizedWeight ? null : 'Files were not optimized'); + }); + }); + + it('should throw error when stated in input folder does not exist', function(done) { + svgo({ input: svgFolderPath + 'temp', output: 'temp' }) + .catch(err => done(/no such file or directory/.test(err) ? null : 'Error was not thrown')); + }); + + describe('stdout', function() { + it('should show file content when no output set', function(done) { + replaceConsoleLog(); + + svgo({ string: fs.readFileSync(svgPath, 'utf8'), output: '-', datauri: 'unenc' }) + .then(() => done(/www.w3.org\/2000\/svg/.test(output) ? null : 'File content was not shown')); + }); + + it('should show message when the folder is empty', function(done) { + const emptyFolderPath = path.resolve(__dirname, 'testSvgEmpty'); + if(!fs.existsSync(emptyFolderPath)) + fs.mkdirSync(emptyFolderPath); + + replaceConsoleLog(); + + svgo({ folder: emptyFolderPath }) + .then(() => done(/is empty/.test(output) ? null : 'Empty folder message was not shown')); + }); + + it('should show message when folder does not consists any svg files', function(done) { + replaceConsoleLog(); + + svgo({ folder: path.resolve(__dirname, 'testFolderWithNoSvg') }) + .then(() => done(/No SVG files have been found/.test(output) ? + null : 'Error "No SVG files have been found" was not shown')); + }); + + it('should create output directory when it does not exist', function(done) { + const initWeight = calcFolderSvgWeight(svgFolderPath); + const outputFolder = path.resolve(__dirname, 'temp'); + + replaceConsoleLog(); + + if(fs.existsSync(outputFolder)) { + fse.removeSync(outputFolder); + } + + svgo({ folder: svgFolderPath, output: outputFolder }) + .then(() => { + const optimizedWeight = calcFolderSvgWeight(outputFolder); + + done(initWeight <= optimizedWeight ? null : 'Files were not optimized'); + }); + }); + + it('should show plugins', function(done) { + replaceConsoleLog(); + + svgo({ 'show-plugins': true }) + .then(() => done(/Currently available plugins:/.test(output) ? null : 'List of plugins was not shown')); + }); + }); +}); diff --git a/test/coa/test.svg b/test/coa/test.svg new file mode 100644 index 00000000..de7975de --- /dev/null +++ b/test/coa/test.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/coa/testFolderWithNoSvg/test b/test/coa/testFolderWithNoSvg/test new file mode 100644 index 00000000..e69de29b diff --git a/test/coa/testSvg/test.1.svg b/test/coa/testSvg/test.1.svg new file mode 100644 index 00000000..de7975de --- /dev/null +++ b/test/coa/testSvg/test.1.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/coa/testSvg/test.svg b/test/coa/testSvg/test.svg new file mode 100644 index 00000000..de7975de --- /dev/null +++ b/test/coa/testSvg/test.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts index 3b56cd1a..b4f63a92 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -5,3 +5,4 @@ test/svg2js test/plugins test/jsapi test/svgo +test/coa