Skip to content

Commit

Permalink
build: switch to native test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Jul 11, 2023
1 parent 612c49c commit f607944
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 253 deletions.
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"graphmaker": "bin/cli"
},
"scripts": {
"test": "jest",
"test": "node --test test/",
"compile-constraints": "nearleyc lib/constraint-grammar.ne -o lib/constraint-grammar.js",
"build-css": "npx tailwindcss -i ./docs/css/styles.css -o ./docs/css/tailwind.css --minify",
"build-ts-docs": "npx typedoc --tsconfig .config/tsconfig.json --readme none",
Expand Down Expand Up @@ -58,7 +58,7 @@
"nearley": "^2.20.1",
"openai": "^3.3.0",
"semver-compare": "^1.0.0",
"sharp": "^0.32.1",
"sharp": "^0.32.2",
"sharp-pdf": "^0.1.3",
"svd-js": "^1.1.1",
"svgdom": "^0.1.14",
Expand All @@ -68,20 +68,12 @@
"@babel/eslint-parser": "^7.22.7",
"dedent": "^1.0.1",
"eslint": "^8.44.0",
"eslint-plugin-jest": "^27.2.2",
"jest": "^29.6.1",
"json-schema-to-typescript": "^13.0.2",
"markdown-it": "^13.0.1",
"markdown-it-anchor": "^8.6.7",
"tailwindcss": "^3.3.2",
"typedoc": "^0.24.8"
},
"jest": {
"testEnvironment": "node",
"setupFilesAfterEnv": [
"./test/setup.js"
]
},
"keywords": [
"graphmaker",
"text-to-graph",
Expand Down
117 changes: 0 additions & 117 deletions test/position.test.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/setup.js

This file was deleted.

124 changes: 8 additions & 116 deletions test/test.js
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');
});
52 changes: 52 additions & 0 deletions test/test.utils.js
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"');
});

0 comments on commit f607944

Please sign in to comment.