Testing
The Toolkit has several thousand unit tests, and in the course of writing these we've developed a set of useful methods that enable us to write tests at a reasonably high level - things like "drag an edge from node '1' to node '3'", for instance, are achieved directly through the test API.
From release 2.1.0 these helper methods are shipped in jsplumbtoolkit-testing.tgz. As these have not yet been used in the wild we're interested in any feedback or suggestions licensees may have.
Setup
How you set things up for testing depends on the internals of your project, both the app itself and the testing framework you're using. The Toolkit's testing package is framework agnostic, though, and can work in any setup.
package.json
"devDependencies":{
...
"jsplumbtoolkit-testing":"file:/path/to/jsplumbtoolkit-testing.tgz"
...
}
Script import
<script type="text/javascript" src="node_modules/jsplumbtoolkit-testing/dist/js/plumbtoolkit-testing.js"></script>The test support exposes jsPlumbToolkitTestSupport object on the window, with two methods:
create(toolkitParams, surfaceParams?, containerParams?): jsPlumbToolkitTestHarness
We use this method internally - it creates a Toolkit and then, if surfaceParams are provided, renders it to an element that it creates.
clear()
Resets the test manager. You should run this after each of your tests.
attachToSurface(surface:Surface): jsPlumbToolkitTestHarness
Creates a jsPlumbToolkitTestHarness with the given Surface and returns it. This is the method you'll want to use in order to use the testing API in your own applications.
toolkit:jsPlumbToolkit
The underlying jsPlumbToolkit instance.
surface:Surface
The underlying surface widget
jsplumb:jsPlumbInstance
The underlying community instance which renders the edges
dragConnection(obj1:string|Node|Group|Port, obj2:string|Node|Group|Port, callbacks?:any):void
Connect, using the mouse, obj1 to obj2. This will throw an Error if either or both of the arguments cannot be resolved. Any interceptors you have setup on the underlying Toolkit instance will be invoked - this functions just as if the user had used the mouse to drag a connection from one object to the other.
obj1An node/group/port ID, or node/group/port, or a DOM element.obj2An node/group/port ID, or node/group/port, or a DOM element.callbacksOptional map of callbacks for the connection.
dragANodeAround(obj:string|Node|Group|Element):void
Randomly drag a Node or Group around. You can use this, for instance, if you just want to test that a move listener is working.
objA Node/Group ID, or Node/Group, or a DOM element.
dragAnElementAround(el:Element):void
Randomly drag a DOM element around.
elThe DOM element to drag.
load (options:LoadOptions):void
Shortcut to the underlying load method of the toolkit.
optionsOptions to the Toolkit's load method.
clear():void
Shortcut to the underlying clear method of the toolkit.
getRenderedElement(obj:string|Node|Group|Port\Element):Element
For the given argument, find and return the corresponding DOM element.
objA node/group/port id, or node/group/port, or a DOM element.
getRenderedConnection(obj:string|Edge):Connection
For the given argument, find and return the underlying Connection used to render it.
objAn edge ID, or an Edge.
dragNodeIntoGroup(node:string|Node|Element, group:string|Group|Element):void
Drag the given node into the given group.
nodeNode id, Node, or DOM element.groupGroup id, Group, or DOM element.
getGroup (obj:string|Element|Group):Group
Gets a Group from the underlying Toolkit.
objGroup Id, DOM element, or Group.
getNode (obj:string|Element|Node):Node
Gets a Node from the underlying Toolkit.
objNode Id, DOM element, or Node.
getToolkitObject(obj:string|Element):Node|Group|Port|Edge
Find the corresponding Toolkit object for the given input.
objA string representing an ID, a DOM element, or an existing Toolkit object.
dragNodeTo(obj:string|Node|Group|Element, x:number, y:number):void
Drag the given Node/Group to the given [x,y], which are canvas coordinates.
objNode id, node, or DOM element.xLocation on canvas in X axis to position top left corner of the node.yLocation on canvas in Y axis to position top left corner of the node.
dragNodeBy(obj:string|Node|Group|Element, x:number, y:number):void
Drag the given Node/Group by the given x/y amounts.
objNode id, node, or DOM element.xAmount to move in X axisyAmount to move in Y axis
dragElementBy(el:Element, x:number, y:number):void
Drag the given DOM element by the given x/y amounts.
elDOM element.xAmount to move in X axisyAmount to move in Y axis
connect(source:string|Node|Port|Group, target:string|Node|Port|Group, data?:any):Edge
Connect the given source and target via a call on the Toolkit, ie. without using the mouse. Returns an Edge.
sourceNode/Port/Group id, node/port/group, or DOM element.targetNode/Port/Group id, node/port/group, or DOM element.dataOptional data for the edge.
dragGroupTo(obj:string|Group|Element, x:number, y:number):void
Drag the given Group to the given [x,y], which are canvas coordinates.
objGroup id, group, or DOM element.xLocation on canvas in X axis to position top left corner of the group.yLocation on canvas in Y axis to position top left corner of the group.
trigger(obj:string|Node|Port|Group|Element, eventName:string, evt:Event):void
Trigger the event with the given name on the given object. By default the event will occur in the middle of the DOM element representing the object.
objNode/Port/Group id, node/port/group, or DOM element.eventNameeg 'click', 'mouseover'evtOptional, an event you previously created viamakeEvent. Sometimes you want to control the specific location of an event.
makeEvent(obj:string|Node|Port|Group|Element, dx?:number, dy?:number):Event
Synthesize an event for the given object. The return value of this is declared as type Event but note that this Event type is the Toolkit's internal testing event type, not an Event as you might know it from Typescript.
- `obj Node/Port/Group id, node/port/group, or DOM element.
dxOptional offset from the center of the x axis of the related DOM element to position the event.dyOptional offset from the center of the y axis of the related DOM element to position the event.
getEdge(edgeId:string)
Gets an Edge.
edgeIdID of the Edge to retrieve
updateEdge(edge:string|Edge, data:any):void
Updates an Edge.
edgeEdge, or edge ID.dataData to update the edge with.
getAllEdges():Array
Gets all edges in the underlying Toolkit.
getRenderedPort(obj:string|Port):Endpoint
Gets the Endpoint that was rendered for some Port.
objPort, or port ID.
addNode(data:any):Node
Add a Node to the Toolkit.
dataData for the Node.
updateNode(obj:string|Node, data:any):void
Update a Node in to the Toolkit.
objNode, or Node Id.dataData for the Node.
clickOnNode(nodeId:string):void
Clicks on the node with the given ID.
nodeIdID of the node to click on.
clickOnElementInsideNode(nodeId:string, selector:string):void
Clicks on an element inside the node with the given ID.
nodeIdID of the node to click on.selectorCSS selector identifying the child element to click on.
clickOnEdge(spec:string|Edge):void
Clicks on the given edge
specID of the edge, or the Edge
clickOnOverlay(edgeSpec:string|Edge, overlayId:string):void
Clicks on the overlay with the given ID, on the given Edge.
edgeSpecID of the Edge, or the EdgeoverlayIdID of the overlay to click on.
clickOnPort(nodeId:string, portId:string):void
Clicks on the port with the given ID on the node with given node id.
nodeIdID of the node containing the portportIdID of the port to click on.
getEdgeCount():number
Returns the count of Edges in the underlying Toolkit.
configureUndoRedo(options:DropManagerOptions):jsPlumbToolkitDropManager
Create a new Undo manager and attach to our Surface. This method is probably not something you'll need to use much - it exists for us to test the drop manager itself. In your applications if you have a drop manager then you'll want to test how it integrates - for which, see the dragElementToCanvas method below.
optionsOptions for the underlyingjsPlumbToolkitDropManager.
dragElementToCanvas(el:Element, x?:number, y?:number)
Drag the given element onto the canvas, optionally at a specific x,y. Use this when you want to test drag/drop from some palette.
elElement to drop onto the canvas.xOptional, defaults to 250.yOptional, defaults to 250.