g6/tests/unit/plugins/bundling-spec.ts

101 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-02-14 10:10:54 +08:00
import G6 from '../../../src';
import { data } from './data';
import { GraphData } from '../../../src/types';
2020-01-06 19:12:27 +08:00
const div = document.createElement('div');
div.id = 'force-layout';
document.body.appendChild(div);
describe('edge bundling', () => {
2020-01-06 19:12:27 +08:00
const graph = new G6.Graph({
container: div,
width: 500,
height: 500,
layout: {
2020-02-14 10:10:54 +08:00
type: 'circular',
2020-01-06 19:12:27 +08:00
},
2020-02-14 10:10:54 +08:00
defaultNode: { size: 10 },
2020-01-06 19:12:27 +08:00
});
graph.data(data);
graph.render();
it('edge bundling on circular layout with default configs', () => {
2020-02-07 19:30:58 +08:00
const bundle = new G6.Bundling();
2020-01-06 19:12:27 +08:00
bundle.initPlugin(graph);
2020-02-14 10:10:54 +08:00
const graphData = graph.save();
2020-01-06 19:12:27 +08:00
bundle.bundling(graphData);
expect(graphData.edges[0].type).toEqual('polyline');
2020-01-06 19:12:27 +08:00
expect(graphData.edges[0].controlPoints.length > 2).toEqual(true);
2020-02-14 10:10:54 +08:00
bundle.destroy();
2020-01-06 19:12:27 +08:00
});
it('bundling on circular with fixed bundleThreshold and iterations', () => {
2020-02-07 19:30:58 +08:00
const bundle = new G6.Bundling({
2020-01-06 19:12:27 +08:00
iterations: 120,
bundleThreshold: 0.1,
});
bundle.initPlugin(graph);
2020-02-14 10:10:54 +08:00
const graphData = graph.save();
2020-01-06 19:12:27 +08:00
bundle.bundling(graphData);
expect(graphData.edges[0].type).toEqual('polyline');
2020-01-06 19:12:27 +08:00
expect(graphData.edges[0].controlPoints.length > 2).toEqual(true);
2020-02-14 10:10:54 +08:00
bundle.destroy();
2020-01-06 19:12:27 +08:00
});
it('bundling update', () => {
const data2: GraphData = {
2020-02-14 10:10:54 +08:00
nodes: [{ id: 'n0' }, { id: 'n1' }],
edges: [{ source: 'n0', target: 'n1' }],
2020-01-06 19:12:27 +08:00
};
graph.changeData(data2);
2020-02-07 19:30:58 +08:00
const bundle = new G6.Bundling();
2020-01-06 19:12:27 +08:00
bundle.initPlugin(graph);
bundle.bundling(data2);
data2.nodes = [
2020-02-14 10:10:54 +08:00
{ id: 'n0', x: 10, y: 100 },
{ id: 'n1', x: 100, y: 100 },
{ id: 'n2', x: 10, y: 10 },
2020-01-06 19:12:27 +08:00
];
data2.edges = [
{ source: 'n0', target: 'n1' },
{ source: 'n1', target: 'n2' },
2020-02-14 10:10:54 +08:00
{ source: 'n0', target: 'n2' },
2020-01-06 19:12:27 +08:00
];
bundle.updateBundling({
bundleThreshold: 0.1,
iterations: 120,
2020-02-14 10:10:54 +08:00
data: data2,
2020-01-06 19:12:27 +08:00
});
expect(data2.edges[0].type).toEqual('polyline');
2020-01-06 19:12:27 +08:00
expect(data2.edges[0].controlPoints.length > 2).toEqual(true);
2020-02-14 10:10:54 +08:00
bundle.destroy();
2020-01-06 19:12:27 +08:00
});
it('bundling no position info, throw error', () => {
2020-02-07 19:30:58 +08:00
const bundle = new G6.Bundling();
2020-01-06 19:12:27 +08:00
bundle.initPlugin(graph);
const data2: GraphData = {
2020-02-14 10:10:54 +08:00
nodes: [{ id: 'n0' }, { id: 'n1' }],
edges: [{ source: 'n0', target: 'n1' }],
};
2020-01-06 19:12:27 +08:00
function fn() {
bundle.bundling(data2);
2020-01-06 19:12:27 +08:00
}
expect(fn).toThrowError('please layout the graph or assign x and y for nodes first');
2020-02-14 10:10:54 +08:00
bundle.destroy();
2020-01-06 19:12:27 +08:00
graph.destroy();
});
});