-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
612c49c
commit f607944
Showing
5 changed files
with
62 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,9 @@ | ||
const { initializeGraph, updateGraph, redo, resetGraph, undo, save, task } = require( './../lib/index.js' ); | ||
|
||
jest.setTimeout( 20000 ); | ||
|
||
async function testSequence( tasks, expected ) { | ||
let graphState = initializeGraph({}); | ||
for ( let i = 0; i < tasks.length; ++i ) { | ||
const [ state, output ] = await updateGraph( graphState, task( tasks[ i ] ) ); | ||
expect( output ).toBeNull(); | ||
if ( typeof expected[ i ] === 'function' ) { | ||
expect( state.graph ).toProjectTruthy( expected, i ); | ||
} else { | ||
expect( state.graph ).toEqual( expected[ i ] ); | ||
} | ||
graphState = state; | ||
} | ||
return graphState; | ||
} | ||
|
||
|
||
test( 'simple-directed-1', async () => { | ||
const tasks = [ | ||
'make this graph directed', | ||
'add nodes A and B and an edge from A to B', | ||
'add a node C and an edge from C to A and from C to B', | ||
'add an edge from B to itself', | ||
]; | ||
const expected = [ | ||
{ | ||
'directed': true, | ||
'nodes': {}, | ||
'edges': {}, | ||
'adjacency': {} | ||
}, | ||
( graph ) => { | ||
return ( | ||
graph.directed === true && | ||
graph.nodes.A && graph.nodes.B && | ||
Object.keys( graph.edges ).length === 1 && | ||
graph.adjacency.A[ 0 ].target === 'B' && | ||
graph.edges[ graph.adjacency.A[ 0 ].edge ] | ||
); | ||
}, | ||
( graph ) => { | ||
return ( | ||
graph.directed === true && | ||
graph.nodes.A && graph.nodes.B && graph.nodes.C && | ||
Object.keys( graph.edges ).length === 3 && | ||
graph.adjacency.A[ 0 ].target === 'B' && | ||
graph.adjacency.C[ 0 ].target === 'A' && | ||
graph.adjacency.C[ 1 ].target === 'B' && | ||
graph.edges[ graph.adjacency.A[ 0 ].edge ] && | ||
graph.edges[ graph.adjacency.C[ 0 ].edge ] && | ||
graph.edges[ graph.adjacency.C[ 1 ].edge ] && | ||
graph.adjacency.A[ 0 ].edge !== graph.adjacency.C[ 0 ].edge && | ||
graph.adjacency.A[ 0 ].edge !== graph.adjacency.C[ 1 ].edge && | ||
graph.adjacency.C[ 0 ].edge !== graph.adjacency.C[ 1 ].edge | ||
); | ||
}, | ||
( graph ) => { | ||
return ( | ||
graph.directed === true && | ||
graph.nodes.A && graph.nodes.B && graph.nodes.C && | ||
Object.keys( graph.edges ).length === 4 && | ||
graph.adjacency.B[ 0 ].target === 'B' && | ||
graph.edges[ graph.adjacency.A[ 0 ].edge ] && | ||
graph.edges[ graph.adjacency.C[ 0 ].edge ] && | ||
graph.edges[ graph.adjacency.C[ 1 ].edge ] && | ||
graph.edges[ graph.adjacency.B[ 0 ].edge ] && | ||
graph.adjacency.A[ 0 ].edge !== graph.adjacency.C[ 0 ].edge && | ||
graph.adjacency.A[ 0 ].edge !== graph.adjacency.C[ 1 ].edge && | ||
graph.adjacency.C[ 0 ].edge !== graph.adjacency.C[ 1 ].edge && | ||
graph.adjacency.B[ 0 ].edge !== graph.adjacency.A[ 0 ].edge && | ||
graph.adjacency.B[ 0 ].edge !== graph.adjacency.C[ 0 ].edge && | ||
graph.adjacency.B[ 0 ].edge !== graph.adjacency.C[ 1 ].edge | ||
); | ||
} | ||
]; | ||
await testSequence( tasks, expected ); | ||
}); | ||
|
||
test( 'convert-undirected-1', async () => { | ||
const tasks = [ | ||
'make this graph directed', | ||
'add nodes A and B and an edge from A to B', | ||
'make this graph undirected' | ||
]; | ||
const expected = [ | ||
{ | ||
'directed': true, | ||
'nodes': {}, | ||
'edges': {}, | ||
'adjacency': {} | ||
}, | ||
( graph ) => { | ||
return ( | ||
graph.directed === true && | ||
graph.nodes.A && graph.nodes.B && | ||
Object.keys( graph.edges ).length === 1 && | ||
graph.adjacency.A[ 0 ].target === 'B' && | ||
graph.edges[ graph.adjacency.A[ 0 ].edge ] | ||
); | ||
}, | ||
( graph ) => { | ||
return ( | ||
graph.directed === false && | ||
graph.nodes.A && graph.nodes.B && | ||
Object.keys( graph.edges ).length === 1 && | ||
graph.adjacency.A[ 0 ].target === 'B' && | ||
graph.adjacency.B[ 0 ].target === 'A' && | ||
graph.edges[ graph.adjacency.A[ 0 ].edge ] && | ||
graph.edges[ graph.adjacency.B[ 0 ].edge ] | ||
); | ||
} | ||
]; | ||
await testSequence( tasks, expected ); | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const graphmaker = require( './../lib/index.js' ); | ||
|
||
test('resetPositions', (t) => { | ||
const reset = graphmaker.resetPositions(); | ||
assert.strictEqual(reset.action, 'RESET_POSITIONS', 'action should be RESET_POSITIONS'); | ||
assert.strictEqual(reset.payload, null, 'payload should be null'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const { | ||
baseGraph, | ||
deepSet, | ||
typeOfNestedKey, | ||
ValidationError | ||
} = require('./../lib/utils.js'); | ||
const BASE_GRAPH = require('./../lib/spec/base-graph.json'); | ||
|
||
const mockSchema = { | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
age: { | ||
type: 'number' | ||
} | ||
} | ||
}; | ||
const mockKeys = ['name']; | ||
|
||
test('baseGraph', (t) => { | ||
const base = baseGraph(); | ||
assert.deepEqual(base, BASE_GRAPH, 'base graph should match the BASE_GRAPH'); | ||
assert.ok(base.nodes, 'base graph should have nodes'); | ||
assert.ok(base.edges, 'base graph should have edges'); | ||
assert.strictEqual(base.directed, false, 'base graph should be undirected'); | ||
assert.strictEqual(base.hasParents, false, 'base graph should have no parents'); | ||
}); | ||
|
||
test('typeOfNestedKey', (t) => { | ||
const type = typeOfNestedKey(mockSchema, mockKeys); | ||
assert.strictEqual(type, 'string', 'Type of the key "name" should be "string"'); | ||
}); | ||
|
||
test('deepSet', (t) => { | ||
const obj = {}; | ||
const props = ['name']; | ||
const val = 'John Doe'; | ||
const result = deepSet(obj, props, true, false, val); | ||
|
||
assert.ok(result, 'deepSet should return true'); | ||
assert.strictEqual(obj.name, 'John Doe', 'Name should be set to "John Doe"'); | ||
}); | ||
|
||
test('ValidationError', (t) => { | ||
const error = new ValidationError('Test error', {}, {}); | ||
|
||
assert.ok(error instanceof Error, 'ValidationError should be an instance of Error'); | ||
assert.strictEqual(error.message, 'Test error', 'Error message should be "Test error"'); | ||
}); |