From f299f7d96e0dfc692657dc5069e067ccc9afa0fc Mon Sep 17 00:00:00 2001 From: Klemek Date: Tue, 16 Jul 2019 11:40:33 +0200 Subject: [PATCH] more unit tests --- src/rendering.js | 62 +++++++++++----------- test/rendering.test.js | 114 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 33 deletions(-) diff --git a/src/rendering.js b/src/rendering.js index 300128b..ce98e12 100644 --- a/src/rendering.js +++ b/src/rendering.js @@ -216,7 +216,7 @@ module.exports = (options) => { }, 'g': { '_attributes': { - 'transform': `scale(${scale / resources.height} ${scale / resources.height}) translate(${-icon.width / 2} ${-256})`, + 'transform': `scale(${scale / resources.height} ${scale / resources.height}) translate(${-icon.width / 2} ${-resources.height / 2})`, 'stroke': (node['color'] || options['icons']['color'] || undefined), 'fill': (node['color'] || options['icons']['color'] || undefined) }, @@ -234,50 +234,50 @@ module.exports = (options) => { * @param {Link2} link */ renderLink: (nodes, link) => { - const src = nodes[link.from]; - const dst = nodes[link.to]; + const src = nodes[link.from]; + const dst = nodes[link.to]; - const posX = ((src.x + dst.x) / 2 + 0.5) * options['h-spacing']; - const posY = (src.y + dst.y) / 2 + 0.5; + const posX = ((src.x + dst.x) / 2 + 0.5) * options['h-spacing']; + const posY = (src.y + dst.y) / 2 + 0.5; - const angle = Math.atan2(dst.y - src.y, (dst.x - src.x) * options['h-spacing']) * 180 / Math.PI; - const scale = (link['scale'] || options['links']['scale']) * DEFAULT_SCALE; + const angle = Math.atan2(dst.y - src.y, (dst.x - src.x) * options['h-spacing']) * 180 / Math.PI; + const scale = (link['scale'] || options['links']['scale']) * DEFAULT_SCALE; - let size = link['size'] || options['links']['size']; + let size = link['size'] || options['links']['size']; - if (!size) { - let dx = Math.abs(dst.x - src.x) * options['h-spacing']; - if (dx > 0) - dx -= LINK_MARGIN * 2; - let dy = Math.abs(dst.y - src.y); - if (dy > 0) - dy -= LINK_MARGIN * 2; + if (!size) { + let dx = Math.abs(dst.x - src.x) * options['h-spacing']; + if (dx > 0) + dx -= LINK_MARGIN * 2; + let dy = Math.abs(dst.y - src.y); + if (dy > 0) + dy -= LINK_MARGIN * 2; - size = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)) / scale; - } + size = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)) / scale; + } - const path = self.getLinkPath(link.type, size); + const path = self.getLinkPath(link.type, size); - if (!path) - return null; + if (!path) + return null; return { + '_attributes': { + 'transform': `translate(${posX} ${posY}) rotate(${angle})` + }, + 'g': { '_attributes': { - 'transform': `translate(${posX} ${posY}) rotate(${angle})` + 'transform': `scale(${scale / 512} ${scale / 512}) translate(${(-256 * size)} ${-256})`, + 'stroke': (link['color'] || options['links']['color'] || undefined), + 'fill': (link['color'] || options['links']['color'] || undefined) }, - 'g': { + 'path': { '_attributes': { - 'transform': `scale(${scale / 512} ${scale / 512}) translate(${(-256 * size)} ${-256})`, - 'stroke': (link['color'] || options['links']['color'] || undefined), - 'fill': (link['color'] || options['links']['color'] || undefined) - }, - 'path': { - '_attributes': { - 'd': path - } + 'd': path } } - }; + } + }; }, /** diff --git a/test/rendering.test.js b/test/rendering.test.js index 745608d..08ad41b 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -130,7 +130,6 @@ describe('getLinkPath (non-regression)', () => { }); }); }); - test('none', () => { expect(rendering().getLinkPath('none', 1)).toEqual(null); }); @@ -156,7 +155,118 @@ describe('getBounds', () => { }); describe('renderNode', () => { - + test('no icon', () => { + const res = rendering({'h-spacing': 1}).renderNode({ + icon: '', + x: 2, + y: 1 + }); + expect(res).toBe(null); + }); + test('simple icon', () => { + const res = rendering({'h-spacing': 1}).renderNode({ + icon: 'circle', + x: 2, + y: 1 + }); + expect(res).toEqual({ + '_attributes': {'transform': 'translate(2.5 1.5)'}, + 'g': { + '_attributes': { + 'transform': 'scale(0.00078125 0.00078125) translate(-256 -256)', + 'stroke': undefined, + 'fill': undefined + }, + 'path': {'_attributes': {'d': solidCirclePath}} + } + }); + }); + test('icon recolor global', () => { + const res = rendering({ + 'h-spacing': 1, + 'icons': {'color': 'green'} + }).renderNode({ + icon: 'circle', + x: 2, + y: 1 + }); + expect(res).toEqual({ + '_attributes': {'transform': 'translate(2.5 1.5)'}, + 'g': { + '_attributes': { + 'transform': 'scale(0.00078125 0.00078125) translate(-256 -256)', + 'stroke': 'green', + 'fill': 'green' + }, + 'path': {'_attributes': {'d': solidCirclePath}} + } + }); + }); + test('icon recolor local', () => { + const res = rendering({ + 'h-spacing': 1, + 'icons': {'color': 'green'} + }).renderNode({ + icon: 'circle', + x: 2, + y: 1, + color: 'red' + }); + expect(res).toEqual({ + '_attributes': {'transform': 'translate(2.5 1.5)'}, + 'g': { + '_attributes': { + 'transform': 'scale(0.00078125 0.00078125) translate(-256 -256)', + 'stroke': 'red', + 'fill': 'red' + }, + 'path': {'_attributes': {'d': solidCirclePath}} + } + }); + }); + test('icon scale global', () => { + const res = rendering({ + 'h-spacing': 1, + 'icons': {'scale': 512 / 0.4} + }).renderNode({ + icon: 'circle', + x: 2, + y: 1 + }); + expect(res).toEqual({ + '_attributes': {'transform': 'translate(2.5 1.5)'}, + 'g': { + '_attributes': { + 'transform': 'scale(1 1) translate(-256 -256)', + 'stroke': undefined, + 'fill': undefined + }, + 'path': {'_attributes': {'d': solidCirclePath}} + } + }); + }); + test('icon scale local', () => { + const res = rendering({ + 'h-spacing': 1, + 'icons': {'scale': 512 / 0.4} + }).renderNode({ + icon: 'circle', + x: 2, + y: 1, + scale: 1024 / 0.4 + }); + expect(res).toEqual({ + '_attributes': {'transform': 'translate(2.5 1.5)'}, + 'g': { + '_attributes': { + 'transform': 'scale(2 2) translate(-256 -256)', + 'stroke': undefined, + 'fill': undefined + }, + 'path': {'_attributes': {'d': solidCirclePath}} + } + }); + }); }); describe('toXML', () => {