Files
OpenNoodl/packages/noodl-runtime/test/models/componentmodel.test.js
Michael Cartner b9c60b07dc Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com>
Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com>
Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com>
Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com>
Co-Authored-By: Johan  <4934465+joolsus@users.noreply.github.com>
Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com>
Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
2024-01-26 11:52:55 +01:00

67 lines
2.2 KiB
JavaScript

const { ComponentModel } = require('./componentmodel');
const NodeModel = require('./nodemodel');
test('Returns all nodes in the component', ()=>{
const component = new ComponentModel('testComponent');
component.addNode(new NodeModel('id1', 'testNode'));
let nodes = component.getAllNodes();
expect(nodes.length).toBe(1);
expect(nodes[0].id).toBe('id1');
component.addNode(new NodeModel('id2', 'testNode'));
nodes = component.getAllNodes();
expect(nodes.length).toBe(2);
//the order is not guaranteed, so let's just use find
expect(nodes.find(n=>n.id==='id1')).not.toBe(undefined);
expect(nodes.find(n=>n.id==='id2')).not.toBe(undefined);
});
test('Connections can be added', ()=>{
const component = new ComponentModel('testComponent');
component.addNode(new NodeModel('id1', 'testNode'));
component.addNode(new NodeModel('id2', 'testNode'));
component.addConnection({
sourceId: 'id1',
sourcePort: 'test',
targetId: 'id2',
targetPort: 'test'
});
const connections = component.getConnectionsFrom('id1');
expect(connections.length).toBe(1);
});
test('Connections can be removed', ()=>{
const connection = {
sourceId: 'id1',
sourcePort: 'test',
targetId: 'id2',
targetPort: 'test'
};
const component = new ComponentModel('testComponent');
component.addNode(new NodeModel('id1', 'testNode'));
component.addNode(new NodeModel('id2', 'testNode'));
component.addConnection(connection);
//test with a copy of the object, and not the same object, to verify it works then as well
component.removeConnection(JSON.parse(JSON.stringify(connection)));
expect(component.getAllConnections().length).toBe(0);
});
test('Removing non-existing connection should not throw', ()=>{
const connection = {
sourceId: 'id1',
sourcePort: 'test',
targetId: 'id2',
targetPort: 'test'
};
const component = new ComponentModel('testComponent');
component.addNode(new NodeModel('id1', 'testNode'));
component.addNode(new NodeModel('id2', 'testNode'));
component.addConnection(connection);
expect(()=>component.removeConnection({})).not.toThrow();
expect(component.getAllConnections().length).toBe(1);
});