From 513de5dd974ea2da0af90cfb4670f2d8c3eed15d Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 12 Jul 2019 09:21:18 +0200 Subject: [PATCH] more unit tests --- src/index.js | 1 - test/index.test.js | 25 +++++++++++++ test/placing.test.js | 86 ++++++++++++++++++++++---------------------- 3 files changed, 68 insertions(+), 44 deletions(-) create mode 100644 test/index.test.js diff --git a/src/index.js b/src/index.js index a035aa8..9bb0d45 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,6 @@ const DEFAULT_OPTIONS = { const self = { options: DEFAULT_OPTIONS, - debug: false, compute: (data) => { const options = merge(DEFAULT_OPTIONS, data['options']); diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..dcd7443 --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,25 @@ +/* jshint -W117 */ +const faDiagrams = require('../src/index'); + +test('test fail placing', () => { + const data = { + options: { + 'diagonals': false, + }, + nodes: [ + {name: '1', icon: ''}, + {name: '2', icon: ''}, + {name: '3', icon: ''}, + ], + links: [ + {from: '1', to: '2', direction: 'left'}, + {from: '1', to: '3', direction: 'left'}, + ] + }; + try { + faDiagrams.compute(data); + fail('no error thrown'); + } catch (err) { + expect(err).toBe('Failed to place nodes'); + } +}); \ No newline at end of file diff --git a/test/placing.test.js b/test/placing.test.js index adcddb2..fbdcd5e 100644 --- a/test/placing.test.js +++ b/test/placing.test.js @@ -3,35 +3,35 @@ const placing = require('../src/placing'); describe('getBounds', () => { test('no nodes', () => { - const res = placing({debug: true}).getBounds({}); + const res = placing({}).getBounds({}); expect(res).toEqual({x: 0, y: 0, w: 0, h: 0}); }); test('no placed nodes', () => { - const res = placing({debug: true}).getBounds({ + const res = placing({}).getBounds({ 'a': {}, 'b': {} }); expect(res).toEqual({x: 0, y: 0, w: 0, h: 0}); }); test('first node', () => { - const res = placing({debug: true}).getBounds({ + const res = placing({}).getBounds({ 'a': {x: 0, y: 0}, 'b': {} }); expect(res).toEqual({x: 0, y: 0, w: 1, h: 1}); }); test('one node not 0,0', () => { - const res = placing({debug: true}).getBounds({ + const res = placing({}).getBounds({ 'a': {x: 5, y: 6}, 'b': {} }); expect(res).toEqual({x: 5, y: 6, w: 1, h: 1}); }); test('2 nodes', () => { - const res = placing({debug: true}).getBounds({ + const res = placing({}).getBounds({ 'a': {x: 0, y: 0}, 'b': {x: 1, y: 1}, 'c': {} }); expect(res).toEqual({x: 0, y: 0, w: 2, h: 2}); }); test('2 nodes special', () => { - const res = placing({debug: true}).getBounds({ + const res = placing({}).getBounds({ 'a': {x: 1, y: 2}, 'b': {x: -5, y: 6}, 'c': {} }); expect(res).toEqual({x: -5, y: 2, w: 7, h: 5}); @@ -40,29 +40,29 @@ describe('getBounds', () => { describe('getNewPos', () => { test('no nodes', () => { - const res = placing({debug: true}).getNewPos({}); + const res = placing({}).getNewPos({}); expect(res).toEqual({x: 0, y: 0}); }); test('no placed nodes', () => { - const res = placing({debug: true}).getNewPos({ + const res = placing({}).getNewPos({ 'a': {}, 'b': {} }); expect(res).toEqual({x: 0, y: 0}); }); test('one node', () => { - const res = placing({debug: true}).getNewPos({ + const res = placing({}).getNewPos({ 'a': {x: 0, y: 0}, 'b': {} }); expect(res).toEqual({x: 1, y: 0}); }); test('one node not 0,0', () => { - const res = placing({debug: true}).getNewPos({ + const res = placing({}).getNewPos({ 'a': {x: 5, y: 6}, 'b': {} }); expect(res).toEqual({x: 6, y: 6}); }); test('2 nodes', () => { - const res = placing({debug: true}).getNewPos({ + const res = placing({}).getNewPos({ 'a': {x: 0, y: 0}, 'b': {x: 1, y: 1} }); expect(res).toEqual({x: 1, y: 0}); @@ -71,43 +71,43 @@ describe('getNewPos', () => { describe('nodeBetween', () => { test('only 2 nodes', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 1, y: 0} }, 'a', 'b'); expect(res).toBe(false); }); test('other node not between', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 1}, 'c': {name: 'c', x: 1, y: 0} }, 'a', 'b'); expect(res).toBe(false); }); test('between aligned h', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0} }, 'a', 'b'); expect(res).toBe(true); }); test('between aligned v', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 2}, 'c': {name: 'c', x: 0, y: 1} }, 'a', 'b'); expect(res).toBe(true); }); test('between diagonal', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2}, 'c': {name: 'c', x: 1, y: 1} }, 'a', 'b'); expect(res).toBe(true); }); test('between diagonal 2', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 1}, 'c': {name: 'c', x: 1, y: 1} }, 'a', 'b'); expect(res).toBe(true); }); test('between diagonal 3', () => { - const res = placing({debug: true}).nodeBetween({ + const res = placing({}).nodeBetween({ '1': {'name': '1', 'x': 1, 'y': 0}, '2': {'name': '2', 'x': 2, 'y': 0}, '3': {'name': '3', 'x': 1, 'y': 1}, @@ -121,7 +121,7 @@ describe('nodeBetween', () => { describe('getPosition', () => { test('free node', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -135,7 +135,7 @@ describe('getPosition', () => { expect(res).toEqual({x: null, y: null, free: true}); }); test('constrained to another not placed', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -149,7 +149,7 @@ describe('getPosition', () => { expect(res).toEqual({x: null, y: null, free: true}); }); test('constrained to another left', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -163,7 +163,7 @@ describe('getPosition', () => { expect(res).toEqual({x: 1, y: 0, free: false}); }); test('constrained to another right', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: ['b'], @@ -177,7 +177,7 @@ describe('getPosition', () => { expect(res).toEqual({x: -1, y: 0, free: false}); }); test('constrained to another up', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -191,7 +191,7 @@ describe('getPosition', () => { expect(res).toEqual({x: 0, y: 1, free: false}); }); test('constrained to another down', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -205,7 +205,7 @@ describe('getPosition', () => { expect(res).toEqual({x: 0, y: -1, free: false}); }); test('double constrained diagonal', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -220,7 +220,7 @@ describe('getPosition', () => { expect(res).toEqual({x: 1, y: 0, free: false}); }); test('double constrained no diagonal', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -235,7 +235,7 @@ describe('getPosition', () => { expect(res).toEqual({x: 2, y: 0, free: false}); }); test('double constrained impossible', () => { - const res = placing({debug: true, 'max-link-length': 2}).getPosition({ + const res = placing({'max-link-length': 2}).getPosition({ 'a': { const: { afterX: [], @@ -253,11 +253,11 @@ describe('getPosition', () => { describe('isValid', () => { test('no nodes', () => { - const res = placing({debug: true, diagonals: true}).isValid({}, []); + const res = placing({diagonals: true}).isValid({}, []); expect(res).toBe(true); }); test('no placed nodes', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {}, 'b': {} }, [ {from: 'a', to: 'b'} @@ -265,7 +265,7 @@ describe('isValid', () => { expect(res).toBe(true); }); test('one nodes', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {} }, [ {from: 'a', to: 'b'} @@ -273,13 +273,13 @@ describe('isValid', () => { expect(res).toBe(true); }); test('overlapping nodes', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 0, y: 0} }, []); expect(res).toBe(false); }); test('in between node', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 0}, 'c': {name: 'c', x: 1, y: 0} }, [ {from: 'a', to: 'b'} @@ -287,7 +287,7 @@ describe('isValid', () => { expect(res).toBe(false); }); test('invalid right link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2} }, [ {from: 'a', to: 'b', direction: 'right'} @@ -295,7 +295,7 @@ describe('isValid', () => { expect(res).toBe(false); }); test('invalid left link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2} }, [ {from: 'a', to: 'b', direction: 'left'} @@ -303,7 +303,7 @@ describe('isValid', () => { expect(res).toBe(false); }); test('invalid up link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2} }, [ {from: 'a', to: 'b', direction: 'up'} @@ -311,7 +311,7 @@ describe('isValid', () => { expect(res).toBe(false); }); test('invalid down link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2} }, [ {from: 'a', to: 'b', direction: 'down'} @@ -319,7 +319,7 @@ describe('isValid', () => { expect(res).toBe(false); }); test('valid right link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2} }, [ {from: 'a', to: 'b', direction: 'right'} @@ -327,7 +327,7 @@ describe('isValid', () => { expect(res).toBe(true); }); test('valid left link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: -2, y: 2} }, [ {from: 'a', to: 'b', direction: 'left'} @@ -335,7 +335,7 @@ describe('isValid', () => { expect(res).toBe(true); }); test('valid up link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: -2} }, [ {from: 'a', to: 'b', direction: 'up'} @@ -343,7 +343,7 @@ describe('isValid', () => { expect(res).toBe(true); }); test('valid down link', () => { - const res = placing({debug: true, diagonals: true}).isValid({ + const res = placing({diagonals: true}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2} }, [ {from: 'a', to: 'b', direction: 'down'} @@ -351,7 +351,7 @@ describe('isValid', () => { expect(res).toBe(true); }); test('invalid diagonal link', () => { - const res = placing({debug: true, diagonals: false}).isValid({ + const res = placing({diagonals: false}).isValid({ 'a': {name: 'a', x: 0, y: 0}, 'b': {name: 'b', x: 2, y: 2} }, [ {from: 'a', to: 'b'} @@ -363,7 +363,7 @@ describe('isValid', () => { describe('correctPlacing', () => { test('no nodes', () => { const nodes = {}; - placing({debug: true}).correctPlacing(nodes); + placing({}).correctPlacing(nodes); expect(nodes).toEqual({}); }); test('several nodes', () => { @@ -372,7 +372,7 @@ describe('correctPlacing', () => { 'b': {x: -2, y: 5}, 'c': {x: -4, y: 3} }; - placing({debug: true}).correctPlacing(nodes); + placing({}).correctPlacing(nodes); expect(nodes).toEqual({ 'a': {x: 7, y: 0}, 'b': {x: 2, y: 3},