more unit tests
This commit is contained in:
+6
-2
@@ -17,7 +17,7 @@ const newmap = (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
'max-link-length': 2,
|
'max-link-length': 3,
|
||||||
'diagonals': true,
|
'diagonals': true,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -179,9 +179,11 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
|||||||
//check overlapping
|
//check overlapping
|
||||||
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
const list = Object.values(nodes).filter(n => n.x !== undefined);
|
||||||
for (let n1 = 0; n1 < list.length - 1; n1++)
|
for (let n1 = 0; n1 < list.length - 1; n1++)
|
||||||
for (let n2 = n1 + 1; n2 < list.length; n2++)
|
for (let n2 = n1 + 1; n2 < list.length; n2++) {
|
||||||
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y)
|
if (list[n1].x === list[n2].x && list[n1].y === list[n2].y)
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (let li = 0; li < links.length; li++) {
|
for (let li = 0; li < links.length; li++) {
|
||||||
link = links[li];
|
link = links[li];
|
||||||
@@ -190,6 +192,8 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
|||||||
if (src.x !== undefined && dst.x !== undefined) {
|
if (src.x !== undefined && dst.x !== undefined) {
|
||||||
if (self.nodeBetween(nodes, link.from, link.to))
|
if (self.nodeBetween(nodes, link.from, link.to))
|
||||||
return false;
|
return false;
|
||||||
|
if (Math.pow(dst.x - src.x, 2) + Math.pow(dst.y - src.y, 2) > Math.pow(options['max-link-length'], 2))
|
||||||
|
return false;
|
||||||
switch (link.direction) {
|
switch (link.direction) {
|
||||||
case 'up':
|
case 'up':
|
||||||
case 'top':
|
case 'top':
|
||||||
|
|||||||
@@ -286,6 +286,14 @@ describe('isValid', () => {
|
|||||||
]);
|
]);
|
||||||
expect(res).toBe(false);
|
expect(res).toBe(false);
|
||||||
});
|
});
|
||||||
|
test('too far node', () => {
|
||||||
|
const res = placing({diagonals: true, 'max-link-length': 4}).isValid({
|
||||||
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 4, y: 2}
|
||||||
|
}, [
|
||||||
|
{from: 'a', to: 'b'}
|
||||||
|
]);
|
||||||
|
expect(res).toBe(false);
|
||||||
|
});
|
||||||
test('invalid right link', () => {
|
test('invalid right link', () => {
|
||||||
const res = placing({diagonals: true}).isValid({
|
const res = placing({diagonals: true}).isValid({
|
||||||
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}
|
||||||
|
|||||||
Reference in New Issue
Block a user