Skip to content

Commit c32f6c8

Browse files
committed
Adding basic testing routine
1 parent 0c64e71 commit c32f6c8

File tree

6 files changed

+164
-2
lines changed

6 files changed

+164
-2
lines changed

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"homepage": "http://sigmajs.org",
66
"bugs": "http://github.com/jacomyal/sigma.js/issues",
77
"main": "dist/sigma.js",
8+
"scripts": {
9+
"test": "mocha --compilers js:babel-core/register ./test/endpoint.js"
10+
},
811
"repository": {
912
"type": "git",
1013
"url": "http://github.com/jacomyal/sigma.js.git"
@@ -21,10 +24,15 @@
2124
],
2225
"license": "MIT",
2326
"devDependencies": {
27+
"babel-core": "^6.14.0",
28+
"babel-preset-es2015": "^6.14.0",
2429
"graphology": "0.0.4",
2530
"mocha": "^3.0.2"
2631
},
2732
"keywords": [
2833
"graph"
29-
]
34+
],
35+
"babel": {
36+
"presets": ["es2015"]
37+
}
3038
}

src/reducers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Sigma.js Reducers
3+
* ==================
4+
*
5+
* Internal reducers mostly used to map well-known attributes by default &
6+
* apply some settings before giving the resulting data to renderers.
7+
*/
8+
9+
/**
10+
* Internal node reducer.
11+
*
12+
* @param {Sigma} controller - Sigma instance.
13+
* @param {Graph} graph - The bound graph.
14+
* @param {any} node - The node to reduce.
15+
*/
16+
export function internalNodeReducer(controller, graph, node) {
17+
return {
18+
x: 1,
19+
y: 1
20+
};
21+
}

src/sigma.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,31 @@
66
* a renderer & camera to display the bound graph on screen.
77
*/
88
import Camera from './camera';
9-
import {isGraph} from './utils';
9+
import {internalNodeReducer} from './reducers';
10+
import {assign, isGraph} from './utils';
1011

1112
// TODO: possibility to pass a camera
13+
// TODO: distinguish with events, full refresh vs. draw
14+
15+
/**
16+
* Helper functions not registered as methods not to overload the prototype
17+
* and preventing sneaky users to abuse them.
18+
*/
19+
function initializeIndex(map, elements) {
20+
const index = map ? new Map() : {};
21+
22+
for (let i = 0, l = elements.length; i < l; i++) {
23+
const element = elements[i],
24+
object = {computed: {}, state: {}};
25+
26+
if (map)
27+
index.set(element, object);
28+
else
29+
index[element] = object;
30+
}
31+
32+
return index;
33+
}
1234

1335
/**
1436
* Sigma class
@@ -25,7 +47,15 @@ export default class Sigma {
2547

2648
// Properties
2749
this.graph = graph;
50+
this.map = this.graph.map;
2851
this.camera = new Camera();
52+
53+
this.state = {};
54+
this.nodesIndex = initializeIndex(this.map, graph.nodes());
55+
this.edgesIndex = initializeIndex(this.map, graph.edges());
56+
57+
this.nodeReducers = [internalNodeReducer];
58+
this.edgeReducers = [];
2959
}
3060

3161
/**---------------------------------------------------------------------------
@@ -50,4 +80,38 @@ export default class Sigma {
5080
getCamera() {
5181
return this.camera;
5282
}
83+
84+
/**---------------------------------------------------------------------------
85+
* Drawing
86+
**---------------------------------------------------------------------------
87+
*/
88+
89+
/**
90+
* Method used to refresh display of the graph.
91+
*
92+
* @return {Sigma} - Returns itself for chaining.
93+
*/
94+
refresh() {
95+
const nodes = this.graph.nodes(),
96+
egdes = this.graph.edges();
97+
98+
// 1-- We need to compute reducers
99+
for (let i = 0, l = nodes.length; i < l; i++) {
100+
const node = nodes[i];
101+
let data = {};
102+
103+
// Applying every reducers
104+
for (let j = 0, m = this.nodeReducers.length; j < m; j++) {
105+
const reducer = this.nodeReducers[j];
106+
107+
assign(data, reducer(this, this.graph, node));
108+
}
109+
110+
// Storing computed data
111+
if (this.map)
112+
this.nodesIndex.get(node).computed = data;
113+
else
114+
this.nodesIndex[node].computed = data;
115+
}
116+
}
53117
}

src/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55
* Various helper functions & classes used throughout the library.
66
*/
77

8+
/**
9+
* Very simple Object.assign-like function.
10+
*
11+
* @param {object} target - First object.
12+
* @param {object} [...objects] - Objects to merge.
13+
* @return {object}
14+
*/
15+
export function assign(target, ...objects) {
16+
target = target || {};
17+
18+
for (let i = 0, l = objects.length; i < l; i++) {
19+
if (!objects[i])
20+
continue;
21+
22+
for (const k in objects[i])
23+
target[k] = objects[i][k];
24+
}
25+
26+
return target;
27+
}
28+
829
/**
930
* Function returning whether the given value is a graphology Graph instance.
1031
*

test/endpoint.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Sigma.js Unit Tests Endpoint
3+
* =============================
4+
*
5+
* Registering every unit test.
6+
*/
7+
const util = require('util');
8+
9+
if (util.inspect.defaultOptions)
10+
util.inspect.defaultOptions.depth = null;
11+
12+
require('./sigma.js');
13+
14+

test/sigma.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Sigma.js Core Unit Tests
3+
* =========================
4+
*
5+
* Testing the core class.
6+
*/
7+
import assert from 'assert';
8+
import Graph from 'graphology';
9+
import Sigma from '../src/sigma';
10+
11+
const graph = new Graph();
12+
graph.addNodesFrom(['John', 'Mary', 'Martha']);
13+
14+
const sigma = new Sigma(graph);
15+
sigma.refresh();
16+
17+
console.log(sigma);
18+
19+
describe('Sigma', function() {
20+
21+
describe('instantiation', function() {
22+
23+
it('should throw if the given graph is invalid.', function() {
24+
25+
assert.throws(function() {
26+
const sigma = new Sigma();
27+
}, /graphology/);
28+
29+
assert.throws(function() {
30+
const sigma = new Sigma(null);
31+
}, /graphology/);
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)