more unit tests

This commit is contained in:
Klemek
2019-07-16 11:40:33 +02:00
parent 5876a0d345
commit f299f7d96e
2 changed files with 143 additions and 33 deletions
+31 -31
View File
@@ -216,7 +216,7 @@ module.exports = (options) => {
}, },
'g': { 'g': {
'_attributes': { '_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), 'stroke': (node['color'] || options['icons']['color'] || undefined),
'fill': (node['color'] || options['icons']['color'] || undefined) 'fill': (node['color'] || options['icons']['color'] || undefined)
}, },
@@ -234,50 +234,50 @@ module.exports = (options) => {
* @param {Link2} link * @param {Link2} link
*/ */
renderLink: (nodes, link) => { renderLink: (nodes, link) => {
const src = nodes[link.from]; const src = nodes[link.from];
const dst = nodes[link.to]; const dst = nodes[link.to];
const posX = ((src.x + dst.x) / 2 + 0.5) * options['h-spacing']; const posX = ((src.x + dst.x) / 2 + 0.5) * options['h-spacing'];
const posY = (src.y + dst.y) / 2 + 0.5; 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 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 scale = (link['scale'] || options['links']['scale']) * DEFAULT_SCALE;
let size = link['size'] || options['links']['size']; let size = link['size'] || options['links']['size'];
if (!size) { if (!size) {
let dx = Math.abs(dst.x - src.x) * options['h-spacing']; let dx = Math.abs(dst.x - src.x) * options['h-spacing'];
if (dx > 0) if (dx > 0)
dx -= LINK_MARGIN * 2; dx -= LINK_MARGIN * 2;
let dy = Math.abs(dst.y - src.y); let dy = Math.abs(dst.y - src.y);
if (dy > 0) if (dy > 0)
dy -= LINK_MARGIN * 2; 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) if (!path)
return null; return null;
return { return {
'_attributes': {
'transform': `translate(${posX} ${posY}) rotate(${angle})`
},
'g': {
'_attributes': { '_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': { '_attributes': {
'transform': `scale(${scale / 512} ${scale / 512}) translate(${(-256 * size)} ${-256})`, 'd': path
'stroke': (link['color'] || options['links']['color'] || undefined),
'fill': (link['color'] || options['links']['color'] || undefined)
},
'path': {
'_attributes': {
'd': path
}
} }
} }
}; }
};
}, },
/** /**
+112 -2
View File
@@ -130,7 +130,6 @@ describe('getLinkPath (non-regression)', () => {
}); });
}); });
}); });
test('none', () => { test('none', () => {
expect(rendering().getLinkPath('none', 1)).toEqual(null); expect(rendering().getLinkPath('none', 1)).toEqual(null);
}); });
@@ -156,7 +155,118 @@ describe('getBounds', () => {
}); });
describe('renderNode', () => { 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', () => { describe('toXML', () => {