more unit tests
This commit is contained in:
+17
-22
@@ -203,15 +203,14 @@ module.exports = (options) => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {{g:Object[]}} data
|
* @param {Node2} node
|
||||||
* @param {Object<string,Node2>} nodes
|
|
||||||
*/
|
*/
|
||||||
renderNodes: (data, nodes) => {
|
renderNode: (node) => {
|
||||||
Object.values(nodes).forEach(node => {
|
|
||||||
const icon = self.getIcon(node.icon);
|
const icon = self.getIcon(node.icon);
|
||||||
if (icon) {
|
if (!icon)
|
||||||
|
return null;
|
||||||
const scale = (node['scale'] || options['icons']['scale']) * DEFAULT_SCALE;
|
const scale = (node['scale'] || options['icons']['scale']) * DEFAULT_SCALE;
|
||||||
const group = {
|
return {
|
||||||
'_attributes': {
|
'_attributes': {
|
||||||
'transform': `translate(${(node.x + 0.5) * options['h-spacing']} ${node.y + 0.5})`,
|
'transform': `translate(${(node.x + 0.5) * options['h-spacing']} ${node.y + 0.5})`,
|
||||||
},
|
},
|
||||||
@@ -228,18 +227,13 @@ module.exports = (options) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
data['g'].push(group);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {{g:Object[]}} data
|
|
||||||
* @param {Object<string,Node2>} nodes
|
* @param {Object<string,Node2>} nodes
|
||||||
* @param {Link2[]} links
|
* @param {Link2} link
|
||||||
*/
|
*/
|
||||||
renderLinks: (data, nodes, links) => {
|
renderLink: (nodes, link) => {
|
||||||
links.forEach(link => {
|
|
||||||
const src = nodes[link.from];
|
const src = nodes[link.from];
|
||||||
const dst = nodes[link.to];
|
const dst = nodes[link.to];
|
||||||
|
|
||||||
@@ -265,9 +259,9 @@ module.exports = (options) => {
|
|||||||
const path = self.getLinkPath(link.type, size);
|
const path = self.getLinkPath(link.type, size);
|
||||||
|
|
||||||
if (!path)
|
if (!path)
|
||||||
return;
|
return null;
|
||||||
|
|
||||||
const group = {
|
return {
|
||||||
'_attributes': {
|
'_attributes': {
|
||||||
'transform': `translate(${posX} ${posY}) rotate(${angle})`
|
'transform': `translate(${posX} ${posY}) rotate(${angle})`
|
||||||
},
|
},
|
||||||
@@ -284,8 +278,6 @@ module.exports = (options) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
data['g'].push(group);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -322,25 +314,28 @@ module.exports = (options) => {
|
|||||||
*/
|
*/
|
||||||
compute: (nodes, links) => {
|
compute: (nodes, links) => {
|
||||||
|
|
||||||
|
const data = {'g': []};
|
||||||
|
|
||||||
Object.keys(nodes).forEach(key => {
|
Object.keys(nodes).forEach(key => {
|
||||||
const res = utils.isValid(nodes[key], NODE_DEF);
|
const res = utils.isValid(nodes[key], NODE_DEF);
|
||||||
if (res)
|
if (res)
|
||||||
throw `Node '${key}' is invalid at key '${res}'`;
|
throw `Node '${key}' is invalid at key '${res}'`;
|
||||||
|
const group = self.renderNode(nodes[key]);
|
||||||
|
if (group)
|
||||||
|
data['g'].push(group);
|
||||||
});
|
});
|
||||||
|
|
||||||
links.forEach((link, i) => {
|
links.forEach((link, i) => {
|
||||||
const res = utils.isValid(link, LINK_DEF);
|
const res = utils.isValid(link, LINK_DEF);
|
||||||
if (res)
|
if (res)
|
||||||
throw `Link ${i} (${link.from}->${link.to}) is invalid at key '${res}'`;
|
throw `Link ${i} (${link.from}->${link.to}) is invalid at key '${res}'`;
|
||||||
|
const group = self.renderLink(nodes, link);
|
||||||
|
if (group)
|
||||||
|
data['g'].push(group);
|
||||||
});
|
});
|
||||||
|
|
||||||
const bounds = self.getBounds(nodes);
|
const bounds = self.getBounds(nodes);
|
||||||
|
|
||||||
const data = {'g': []};
|
|
||||||
|
|
||||||
self.renderNodes(data, nodes);
|
|
||||||
self.renderLinks(data, nodes, links);
|
|
||||||
|
|
||||||
return self.toXML(data, bounds);
|
return self.toXML(data, bounds);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+41
-19
@@ -5,6 +5,28 @@ const fs = require('fs');
|
|||||||
const solidCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z';
|
const solidCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z';
|
||||||
const regularCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z';
|
const regularCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z';
|
||||||
|
|
||||||
|
let linkPaths = {};
|
||||||
|
|
||||||
|
/*test('write data', () => {
|
||||||
|
const data = {};
|
||||||
|
['default', 'line', 'double', 'split-double', 'dashed', 'dashed-line', 'dashed-double', 'dashed-split-double'].forEach(type => {
|
||||||
|
data[type] = {};
|
||||||
|
[1, 1.5, 2].forEach(width => {
|
||||||
|
data[type][width] = rendering().getLinkPath(type, width);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
fs.writeFileSync('test/link_paths.json', JSON.stringify(data), {encoding: 'utf-8'});
|
||||||
|
});*/
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
|
||||||
|
fs.readFile('test/link_paths.json', {encoding: 'utf-8'}, (err, fileData) => {
|
||||||
|
if (!err)
|
||||||
|
linkPaths = JSON.parse(fileData);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('resources fail', () => {
|
describe('resources fail', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
fs.renameSync('resources.json', 'resources.tmp.json');
|
fs.renameSync('resources.json', 'resources.tmp.json');
|
||||||
@@ -99,29 +121,12 @@ describe('getIcon', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/*test('write data', () => {
|
|
||||||
const data = {};
|
|
||||||
['default', 'line', 'double', 'split-double', 'dashed', 'dashed-line', 'dashed-double', 'dashed-split-double'].forEach(type => {
|
|
||||||
data[type] = {};
|
|
||||||
[1, 1.5, 2].forEach(width => {
|
|
||||||
data[type][width] = rendering().getLinkPath(type, width);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
fs.writeFileSync('test/link_path_data.json', JSON.stringify(data), {encoding: 'utf-8'});
|
|
||||||
});*/
|
|
||||||
|
|
||||||
describe('getLinkPath (non-regression)', () => {
|
describe('getLinkPath (non-regression)', () => {
|
||||||
let data = {};
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
data = JSON.parse(fs.readFileSync('test/link_path_data.json', {encoding: 'utf-8'}));
|
|
||||||
});
|
|
||||||
|
|
||||||
['default', 'line', 'double', 'split-double', 'dashed', 'dashed-line', 'dashed-double', 'dashed-split-double'].forEach(type => {
|
['default', 'line', 'double', 'split-double', 'dashed', 'dashed-line', 'dashed-double', 'dashed-split-double'].forEach(type => {
|
||||||
data[type] = {};
|
data[type] = {};
|
||||||
[1, 1.5, 2].forEach(width => {
|
[1, 1.5, 2].forEach(width => {
|
||||||
test(type + ' ' + width, () => {
|
test(type + ' ' + width, () => {
|
||||||
expect(rendering().getLinkPath(type, width)).toEqual(data[type][width]);
|
expect(rendering().getLinkPath(type, width)).toEqual(linkPaths[type][width]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -150,6 +155,10 @@ describe('getBounds', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('renderNode', () => {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('toXML', () => {
|
describe('toXML', () => {
|
||||||
test('no data', () => {
|
test('no data', () => {
|
||||||
const res = rendering({scale: 20, 'h-spacing': 1}).toXML({}, {w: 0, h: 0});
|
const res = rendering({scale: 20, 'h-spacing': 1}).toXML({}, {w: 0, h: 0});
|
||||||
@@ -174,6 +183,19 @@ describe('compute', () => {
|
|||||||
const res = rendering({beautify: true, 'h-spacing': 1.2, scale: 20}).compute({}, []);
|
const res = rendering({beautify: true, 'h-spacing': 1.2, scale: 20}).compute({}, []);
|
||||||
expect(res).toEqual('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0" width="0" height="0" stroke="black" fill="black">\n</svg>');
|
expect(res).toEqual('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 0 0" width="0" height="0" stroke="black" fill="black">\n</svg>');
|
||||||
});
|
});
|
||||||
|
test('only invisible things', () => {
|
||||||
|
const res = rendering({beautify: true, 'h-spacing': 1, scale: 20}).compute({'a': {name: 'a', icon: '', x: 0, y: 0}, 'b': {name: 'b', icon: '', x: 1, y: 0}}, [{from: 'a', to: 'b', type: 'none'}]);
|
||||||
|
expect(res).toEqual('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 1" width="100" height="50" stroke="black" fill="black">\n</svg>');
|
||||||
|
});
|
||||||
|
test('simple output', () => {
|
||||||
|
const res = rendering({beautify: true, 'h-spacing': 1, scale: 20}).compute({'a': {name: 'a', icon: 'circle', x: 0, y: 0}, 'b': {name: 'b', icon: 'circle', x: 1, y: 0}}, [{from: 'a', to: 'b'}]);
|
||||||
|
|
||||||
|
expect(res.split(solidCirclePath).length).toBe(3); //2 times circle path
|
||||||
|
expect(res.includes(linkPaths['default'][1])).toBe(true); // contains simple arrow of width 1
|
||||||
|
expect(res.split('</g>').length).toBe(7); //6 groups definitions
|
||||||
|
|
||||||
|
expect(res.indexOf('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 1" width="100" height="50" stroke="black" fill="black">')).toBe(0);
|
||||||
|
});
|
||||||
test('invalid node', () => {
|
test('invalid node', () => {
|
||||||
try {
|
try {
|
||||||
rendering().compute({
|
rendering().compute({
|
||||||
@@ -186,7 +208,7 @@ describe('compute', () => {
|
|||||||
});
|
});
|
||||||
test('invalid link', () => {
|
test('invalid link', () => {
|
||||||
try {
|
try {
|
||||||
rendering().compute({}, [{from: 'a', to: 'b'}, {from: 'a', to: 'b', type: 5}]);
|
rendering().compute({'a': {name: 'a', icon: '', x: 0, y: 0}, 'b': {name: 'b', icon: '', x: 0, y: 0}}, [{from: 'a', to: 'b'}, {from: 'a', to: 'b', type: 5}]);
|
||||||
fail('no error thrown');
|
fail('no error thrown');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(err).toBe('Link 1 (a->b) is invalid at key \'type\'');
|
expect(err).toBe('Link 1 (a->b) is invalid at key \'type\'');
|
||||||
|
|||||||
Reference in New Issue
Block a user