From 7e1ec95f9a70314f215c05d5035d44d3a33c28a0 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 27 Dec 2019 04:32:03 +0000 Subject: [PATCH] test(svg): add unit test --- test/filter.test.js | 110 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/test/filter.test.js b/test/filter.test.js index acc1fd3..662ae60 100644 --- a/test/filter.test.js +++ b/test/filter.test.js @@ -266,3 +266,113 @@ describe('js', () => { expect(result).toBe(input) }) }) + +describe('svg', () => { + const { svgDefault } = require('../index') + const s = require('../lib/filter').minifySvg.bind(hexo) + const Svgo = require('svgo') + const path = 'foo.svg' + const input = '' + + beforeEach(() => { + hexo.config.minify.svg = Object.assign({}, svgDefault) + hexo.route.set(path, input) + }) + + afterEach(() => { + const routeList = hexo.route.list() + routeList.forEach((path) => hexo.route.remove(path)) + }) + + test('default', async () => { + await s() + const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) + + const output = hexo.route.get(path) + let result = '' + output.on('data', (chunk) => (result += chunk)) + output.on('end', () => { + expect(result).toBe(data) + }) + }) + + test('disable', async () => { + hexo.config.minify.svg.enable = false + const result = await s() + expect(result).toBeUndefined() + }) + + test('option', async () => { + const customOpt = [{ cleanupIDs: false }] + hexo.config.minify.svg.plugins = customOpt + await s() + const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) + + const output = hexo.route.get(path) + let result = '' + output.on('data', (chunk) => (result += chunk)) + output.on('end', () => { + expect(result).toBe(data) + expect(result).toContain('id="a"') + }) + }) + + test('invalid svg', async () => { + const input = '{}' + hexo.route.set(path, input) + let expected + try { + await new Svgo(hexo.config.minify.svg).optimize(input) + } catch (err) { + expected = err + } + try { + await s() + } catch (err) { + expect(err.message).toContain(expected) + } + }) + + test('include - exclude *.min.svg by default', async () => { + const path = 'foo.min.svg' + hexo.route.set(path, input) + await s() + + const output = hexo.route.get(path) + let result = '' + output.on('data', (chunk) => (result += chunk)) + output.on('end', () => { + expect(result).toBe(input) + }) + }) + + test('include - basename', async () => { + hexo.config.minify.svg.include = 'bar.svg' + const fooPath = 'foo/bar.svg' + hexo.route.set(fooPath, input) + await s() + const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) + + const output = hexo.route.get(fooPath) + let result = '' + output.on('data', (chunk) => (result += chunk)) + output.on('end', () => { + expect(result).toBe(data) + }) + }) + + test('include - slash in pattern', async () => { + hexo.config.minify.svg.include = '**/foo/*.svg' + const fooPath = 'blog/site/example/foo/bar.svg' + hexo.route.set(fooPath, input) + await s() + const { data } = await new Svgo(hexo.config.minify.svg).optimize(input) + + const output = hexo.route.get(fooPath) + let result = '' + output.on('data', (chunk) => (result += chunk)) + output.on('end', () => { + expect(result).toBe(data) + }) + }) +})