chore(test): use diagram-js bounds and connection matchers

This commit is contained in:
pedesen 2016-06-08 12:03:48 +02:00 committed by Nico Rehwaldt
parent cf108a9233
commit 4d7dfe1df5
3 changed files with 2 additions and 293 deletions

View File

@ -12,5 +12,5 @@ TestHelper.insertCSS('diagram-js-testing.css',
// add suite specific matchers
global.chai.use(require('./matchers/BoundsMatchers'));
global.chai.use(require('./matchers/ConnectionMatchers'));
global.chai.use(require('diagram-js/test/matchers/BoundsMatchers'));
global.chai.use(require('diagram-js/test/matchers/ConnectionMatchers'));

View File

@ -1,167 +0,0 @@
'use strict';
var pick = require('lodash/object/pick');
var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
POSITION_ATTRS = [ 'x', 'y' ],
DIMENSION_ATTRS = [ 'width', 'height' ];
function getBounds(s) {
if ('bounds' in s) {
s = s.bounds;
}
// TLBR object
if ('top' in s) {
return {
x: s.left,
y: s.top,
width: s.right - s.left,
height: s.bottom - s.top
};
}
// { x, y, width, height } object
else {
return pick(s, BOUNDS_ATTRS);
}
}
function getDimensions(s) {
return pick(getBounds(s), DIMENSION_ATTRS);
}
function getPosition(s) {
return pick(getBounds(s), POSITION_ATTRS);
}
module.exports = function(chai, utils) {
var Assertion = chai.Assertion;
function inspect(obj) {
return utils.inspect(obj).replace(/\n /g, '');
}
/**
* A simple bounds matcher, that verifies an element
* has the correct { x, y, width, height }.
*
* @example
*
* expect(di.label).to.have.bounds({ x: 100, y: 100, width: 10, height: 20 });
* expect(shape).to.have.bounds({ top: 100, left: 0, right: 200, bottom: 50 });
*
* @param {Bounds|TLBR} exp
*/
Assertion.addMethod('bounds', function(exp) {
var obj = this._obj;
var objectBounds = getBounds(obj),
expectedBounds = getBounds(exp);
var matches = utils.eql(objectBounds, expectedBounds);
var objectBoundsStr = inspect(objectBounds),
expectedBoundsStr = inspect(expectedBounds);
var theAssert = new Assertion(objectBounds);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
'to equal \n ' + expectedBoundsStr +
'\nbut got\n ' + objectBoundsStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
'not to equal \n ' + expectedBoundsStr
);
});
/**
* A simple dimensions matcher, that verifies an element
* has the correct { width, height }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(di.label).to.have.dimensions({ width: 10, height: 20 });
*
* @param {Dimensions} exp
*/
Assertion.addMethod('dimensions', function(exp) {
var obj = this._obj;
var objectDimensions = getDimensions(obj),
expectedDimensions = getDimensions(exp);
var matches = utils.eql(objectDimensions, expectedDimensions);
var objectDimensionsStr = inspect(objectDimensionsStr),
expectedDimensionsStr = inspect(expectedDimensions);
var theAssert = new Assertion(objectDimensions);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
'to equal \n ' + expectedDimensionsStr +
'\nbut got\n ' + objectDimensionsStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
'not to equal \n ' + expectedDimensionsStr
);
});
/**
* A simple position matcher, that verifies an element
* has the correct { x, y }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(taskShape).to.have.position({ x: 100, y: 150 });
*
* @param {Point} exp
*/
Assertion.addMethod('position', function(exp) {
var obj = this._obj;
var objectPosition = getPosition(obj),
expectedPosition = getPosition(exp);
var matches = utils.eql(objectPosition, expectedPosition);
var objectPositionStr = inspect(objectPositionStr),
expectedPositionStr = inspect(expectedPosition);
var theAssert = new Assertion(objectPosition);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
'to equal \n ' + expectedPositionStr +
'\nbut got\n ' + objectPositionStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
'not to equal \n ' + expectedPositionStr
);
});
};

View File

@ -1,124 +0,0 @@
'use strict';
var pick = require('lodash/object/pick');
var POSITION_ATTRS = [ 'x', 'y' ];
function extractPoints(point) {
return pick(point, POSITION_ATTRS);
}
module.exports = function(chai, utils) {
var Assertion = chai.Assertion;
function inspect(obj) {
return utils.inspect(obj).replace(/\n /g, '');
}
/**
* A simple waypoints matcher, that verifies a connection
* consists of the correct connection points.
*
* Does not take the original docking into account.
*
* @example
*
* expect(connection).to.have.waypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
*
* @param {Connection|Array<Point>} exp
*/
Assertion.addMethod('waypoints', function(exp) {
var obj = this._obj;
var strippedWaypoints = obj.waypoints.map(extractPoints),
strippedExpectedWaypoints = exp.map(extractPoints);
var matches = utils.eql(strippedWaypoints, strippedExpectedWaypoints);
var strippedWaypointsStr = inspect(strippedWaypoints),
strippedExpectedWaypointsStr = inspect(strippedExpectedWaypoints);
var theAssert = new Assertion(strippedWaypoints);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '#waypoints> ' +
'to equal \n ' + strippedExpectedWaypointsStr +
'\nbut got\n ' + strippedWaypointsStr,
'expected <' + obj.id + '#waypoints> ' +
'not to equal \n ' + strippedExpectedWaypoints
);
});
/**
* A simple waypoints matcher, that verifies a connection
* has the given start docking.
*
* @example
*
* expect(connection).to.have.startDocking({ x: 100, y: 100 });
*
* @param {Point} exp
*/
Assertion.addMethod('startDocking', function(exp) {
var obj = this._obj;
var startPoint = obj.waypoints[0],
startDocking = startPoint && startPoint.original;
var matches = utils.eql(startDocking, exp);
var startDockingStr = inspect(startDocking),
expectedStartDockingStr = inspect(exp);
var theAssert = new Assertion(startDocking);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '> to have startDocking ' +
expectedStartDockingStr + ' but got ' + startDockingStr
);
});
/**
* A simple waypoints matcher, that verifies a connection
* has the given start docking.
*
* @example
*
* expect(connection).to.have.endDocking({ x: 100, y: 100 });
*
* @param {Point} exp
*/
Assertion.addMethod('endDocking', function(exp) {
var obj = this._obj;
var endPoint = obj.waypoints[obj.waypoints.length - 1],
endDocking = endPoint && endPoint.original;
var matches = utils.eql(endDocking, exp);
var endDockingStr = inspect(endDocking),
expectedEndDockingStr = inspect(exp);
var theAssert = new Assertion(endDocking);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '> to have endDocking ' +
expectedEndDockingStr + ' but got ' + endDockingStr
);
});
};