From 75dd510a6e5685cbdf16774c2c8a63d2e0a4e2b4 Mon Sep 17 00:00:00 2001 From: Klemek Date: Sat, 13 Jul 2019 14:41:24 +0200 Subject: [PATCH] more unit tests --- src/placing.js | 8 ++++++-- test/placing.test.js | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/placing.js b/src/placing.js index e878038..5ba9862 100644 --- a/src/placing.js +++ b/src/placing.js @@ -17,7 +17,7 @@ const newmap = (w, h, fill) => new Array(w).fill(0).map(() => new Array(h).fill( */ const DEFAULT_OPTIONS = { - 'max-link-length': 2, + 'max-link-length': 3, 'diagonals': true, }; @@ -179,9 +179,11 @@ module.exports = (options = DEFAULT_OPTIONS) => { //check overlapping const list = Object.values(nodes).filter(n => n.x !== undefined); 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) return false; + } + for (let li = 0; li < links.length; li++) { link = links[li]; @@ -190,6 +192,8 @@ module.exports = (options = DEFAULT_OPTIONS) => { if (src.x !== undefined && dst.x !== undefined) { if (self.nodeBetween(nodes, link.from, link.to)) 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) { case 'up': case 'top': diff --git a/test/placing.test.js b/test/placing.test.js index fbdcd5e..e9ec256 100644 --- a/test/placing.test.js +++ b/test/placing.test.js @@ -286,6 +286,14 @@ describe('isValid', () => { ]); 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', () => { const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2}