'use strict'; /** * @typedef {import('../lib/types').Plugin} Plugin */ const { optimize } = require('./svgo.js'); test('allow to setup default preset', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { plugins: ['preset-default'], js2svg: { pretty: true, indent: 2 }, }); expect(data).toMatchInlineSnapshot(` " " `); }); test('allow to disable and customize plugins in preset', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { plugins: [ { name: 'preset-default', params: { overrides: { removeXMLProcInst: false, removeDesc: { removeAny: false, }, }, }, }, ], js2svg: { pretty: true, indent: 2 }, }); expect(data).toMatchInlineSnapshot(` " Not standard description " `); }); test('warn when user tries enable plugins in preset', () => { const svg = ` `; const warn = jest.spyOn(console, 'warn'); optimize(svg, { plugins: [ { name: 'preset-default', params: { overrides: { cleanupListOfValues: true, }, }, }, ], js2svg: { pretty: true, indent: 2 }, }); expect(warn) .toBeCalledWith(`You are trying to enable cleanupListOfValues which is not part of preset. Try to put it before or after preset, for example plugins: [ { name: 'preset-default', }, 'cleanupListOfValues' ] `); warn.mockRestore(); }); describe('allow to configure EOL', () => { test('should respect EOL set to LF', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { eol: 'lf', pretty: true, indent: 2 }, }); // using toEqual because line endings matter in these tests expect(data).toEqual( '\n \n\n' ); }); test('should respect EOL set to CRLF', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { eol: 'crlf', pretty: true, indent: 2 }, }); // using toEqual because line endings matter in these tests expect(data).toEqual( '\r\n \r\n\r\n' ); }); test('should default to LF line break for any other EOL values', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { eol: 'invalid', pretty: true, indent: 2 }, }); // using toEqual because line endings matter in these tests expect(data).toEqual( '\n \n\n' ); }); }); describe('allow to configure final newline', () => { test('should not add final newline when unset', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { eol: 'lf' } }); // using toEqual because line endings matter in these tests expect(data).toEqual( '' ); }); test('should add final newline when set', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { finalNewline: true, eol: 'lf' }, }); // using toEqual because line endings matter in these tests expect(data).toEqual( '\n' ); }); test('should not add extra newlines when using pretty: true', () => { const svg = ` Not standard description `; const { data } = optimize(svg, { js2svg: { finalNewline: true, pretty: true, indent: 2, eol: 'lf' }, }); // using toEqual because line endings matter in these tests expect(data).toEqual( '\n \n\n' ); }); }); test('allow to customize precision for preset', () => { const svg = ` `; const { data } = optimize(svg, { plugins: [ { name: 'preset-default', params: { floatPrecision: 4, }, }, ], js2svg: { pretty: true, indent: 2 }, }); expect(data).toMatchInlineSnapshot(` " " `); }); test('plugin precision should override preset precision', () => { const svg = ` `; const { data } = optimize(svg, { plugins: [ { name: 'preset-default', params: { floatPrecision: 4, overrides: { cleanupNumericValues: { floatPrecision: 5, }, }, }, }, ], js2svg: { pretty: true, indent: 2 }, }); expect(data).toMatchInlineSnapshot(` " " `); }); test('provides informative error in result', () => { const svg = ` `; const { modernError: error } = optimize(svg, { path: 'test.svg' }); expect(error.name).toEqual('SvgoParserError'); expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value'); expect(error.reason).toEqual('Unquoted attribute value'); expect(error.line).toEqual(2); expect(error.column).toEqual(33); expect(error.source).toEqual(svg); }); test('provides code snippet in rendered error', () => { const svg = ` `; const { modernError: error } = optimize(svg, { path: 'test.svg' }); expect(error.toString()) .toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value 1 | > 2 | | ^ 3 | 4 | `); }); test('supports errors without path', () => { const svg = ` `; const { modernError: error } = optimize(svg); expect(error.toString()) .toEqual(`SvgoParserError: :11:29: Unquoted attribute value 9 | 10 | > 11 | | ^ 12 | 13 | `); }); test('slices long line in error code snippet', () => { const svg = ` `; const { modernError: error } = optimize(svg); expect(error.toString()) .toEqual(`SvgoParserError: :2:211: Invalid attribute name 1 | …-0.dtd" viewBox="0 0 230 120"> > 2 | …7.334 250.955 222.534t579.555 155.292z stroke-width="1.5" fill="red" strok… | ^ 3 | 4 | `); }); test('provides legacy error message', () => { const svg = ` `; const { error } = optimize(svg, { path: 'test.svg' }); expect(error) .toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value 1 | > 2 | | ^ 3 | 4 | `); }); test('multipass option should trigger plugins multiple times', () => { const svg = ``; const list = []; /** * @type {Plugin} */ const testPlugin = { type: 'visitor', name: 'testPlugin', fn: (_root, _params, info) => { list.push(info.multipassCount); return { element: { enter: (node) => { node.attributes.id = node.attributes.id.slice(1); }, }, }; }, }; const { data } = optimize(svg, { multipass: true, plugins: [testPlugin] }); expect(list).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); expect(data).toEqual(``); });