chore(matchers): add to.have.di* matchers

This commit is contained in:
Nico Rehwaldt 2020-01-20 11:50:03 +01:00
parent 50630c7aac
commit e345664f6e
2 changed files with 195 additions and 71 deletions

View File

@ -2,6 +2,10 @@ import {
pick pick
} from 'min-dash'; } from 'min-dash';
import {
getBusinessObject
} from 'lib/util/ModelUtil';
var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ], var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
POSITION_ATTRS = [ 'x', 'y' ], POSITION_ATTRS = [ 'x', 'y' ],
DIMENSION_ATTRS = [ 'width', 'height' ]; DIMENSION_ATTRS = [ 'width', 'height' ];
@ -60,28 +64,29 @@ export default function(chai, utils) {
Assertion.addMethod('bounds', function(exp) { Assertion.addMethod('bounds', function(exp) {
var obj = this._obj; var obj = this._obj;
var objectBounds = getBounds(obj), assertBounds(this, obj.id ? obj.id : obj, getBounds(obj), getBounds(exp));
expectedBounds = getBounds(exp); });
var matches = utils.eql(objectBounds, expectedBounds);
var objectBoundsStr = inspect(objectBounds),
expectedBoundsStr = inspect(expectedBounds);
var theAssert = new Assertion(objectBounds); /**
* A simple bounds matcher, that verifies an element
* has the correct { x, y, width, height }.
*
* @example
*
* expect(di.label).to.have.diBounds({ x: 100, y: 100, width: 10, height: 20 });
* expect(shape).to.have.diBounds({ top: 100, left: 0, right: 200, bottom: 50 });
*
* @param {Bounds|TLBR} exp
*/
Assertion.addMethod('diBounds', function(exp) {
var obj = this._obj;
// transfer flags var bo = getBusinessObject(obj);
utils.transferFlags(this, theAssert, false);
theAssert.assert( expect(bo).to.have.property('di');
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' + assertBounds(this, bo.id + '#di', getBounds(bo.di), getBounds(exp));
'to equal \n ' + expectedBoundsStr +
'\nbut got\n ' + objectBoundsStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
'not to equal \n ' + expectedBoundsStr
);
}); });
/** /**
@ -97,30 +102,34 @@ export default function(chai, utils) {
* @param {Dimensions} exp * @param {Dimensions} exp
*/ */
Assertion.addMethod('dimensions', function(exp) { Assertion.addMethod('dimensions', function(exp) {
var obj = this._obj; var obj = this._obj;
var objectDimensions = getDimensions(obj), assertDimensions(this, obj.id ? obj.id : obj, getDimensions(obj), getDimensions(exp));
expectedDimensions = getDimensions(exp); });
var matches = utils.eql(objectDimensions, expectedDimensions);
var objectDimensionsStr = inspect(objectDimensions),
expectedDimensionsStr = inspect(expectedDimensions);
var theAssert = new Assertion(objectDimensions); /**
* A simple dimensions matcher, that verifies an elements
* DI has the correct { width, height }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(di.label).to.have.diDimensions({ width: 10, height: 20 });
*
* @param {Dimensions} exp
*/
Assertion.addMethod('diDimensions', function(exp) {
// transfer flags var obj = this._obj;
utils.transferFlags(this, theAssert, false);
theAssert.assert( var bo = getBusinessObject(obj);
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' + expect(bo).to.have.property('di');
'to equal \n ' + expectedDimensionsStr +
'\nbut got\n ' + objectDimensionsStr, assertDimensions(this, bo.id + '#di', getDimensions(bo.di), getDimensions(exp));
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
'not to equal \n ' + expectedDimensionsStr
);
}); });
@ -137,30 +146,104 @@ export default function(chai, utils) {
* @param {Point} exp * @param {Point} exp
*/ */
Assertion.addMethod('position', function(exp) { Assertion.addMethod('position', function(exp) {
var obj = this._obj; var obj = this._obj;
var objectPosition = getPosition(obj), assertPosition(this, obj.id ? obj.id : obj, getPosition(obj), getPosition(exp));
expectedPosition = getPosition(exp); });
var matches = utils.eql(objectPosition, expectedPosition);
var objectPositionStr = inspect(objectPosition),
expectedPositionStr = inspect(expectedPosition);
var theAssert = new Assertion(objectPosition); /**
* A simple position matcher, that verifies an element
* has the correct DI position { x, y }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(taskShape).to.have.diPosition({ x: 100, y: 150 });
*
* @param {Point} exp
*/
Assertion.addMethod('diPosition', function(exp) {
var obj = this._obj;
var bo = getBusinessObject(obj);
expect(bo).to.have.property('di');
assertPosition(this, bo.id + '#di', getPosition(bo.di), getPosition(exp));
});
// helpers ////////////////
function assertBounds(self, desc, bounds, expectedBounds) {
var matches = utils.eql(bounds, expectedBounds);
var boundsStr = inspect(bounds),
expectedBoundsStr = inspect(expectedBounds);
var theAssert = new Assertion(bounds);
// transfer flags // transfer flags
utils.transferFlags(this, theAssert, false); utils.transferFlags(self, theAssert, false);
theAssert.assert( theAssert.assert(
matches, matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' + 'expected <' + desc + '> bounds ' +
'to equal \n ' + expectedBoundsStr +
'\nbut got\n ' + boundsStr,
'expected <' + desc + '> bounds ' +
'not to equal \n ' + expectedBoundsStr
);
}
function assertDimensions(self, desc, dimensions, expectedDimensions) {
var matches = utils.eql(dimensions, expectedDimensions);
var dimensionsStr = inspect(dimensions),
expectedDimensionsStr = inspect(expectedDimensions);
var theAssert = new Assertion(dimensions);
// transfer flags
utils.transferFlags(self, theAssert, false);
theAssert.assert(
matches,
'expected <' + desc + '> dimensions ' +
'to equal \n ' + expectedDimensionsStr +
'\nbut got\n ' + dimensionsStr,
'expected <' + desc + '> dimensions ' +
'not to equal \n ' + expectedDimensionsStr
);
}
function assertPosition(self, desc, position, expectedPosition) {
var matches = utils.eql(position, expectedPosition);
var positionStr = inspect(position),
expectedPositionStr = inspect(expectedPosition);
var theAssert = new Assertion(position);
// transfer flags
utils.transferFlags(self, theAssert, false);
theAssert.assert(
matches,
'expected <' + desc + '> position ' +
'to equal \n ' + expectedPositionStr + 'to equal \n ' + expectedPositionStr +
'\nbut got\n ' + objectPositionStr, '\nbut got\n ' + positionStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' + 'expected <' + desc + '> position ' +
'not to equal \n ' + expectedPositionStr 'not to equal \n ' + expectedPositionStr
); );
}); }
} }

View File

@ -2,12 +2,20 @@ import {
pick pick
} from 'min-dash'; } from 'min-dash';
import {
getBusinessObject
} from 'lib/util/ModelUtil';
var POSITION_ATTRS = [ 'x', 'y' ]; var POSITION_ATTRS = [ 'x', 'y' ];
function extractPoints(point) { function getPoint(point) {
return pick(point, POSITION_ATTRS); return pick(point, POSITION_ATTRS);
} }
function getPoints(waypoints) {
return waypoints.map(getPoint);
}
export default function(chai, utils) { export default function(chai, utils) {
@ -32,29 +40,37 @@ export default function(chai, utils) {
Assertion.addMethod('waypoints', function(exp) { Assertion.addMethod('waypoints', function(exp) {
var obj = this._obj; var obj = this._obj;
var strippedWaypoints = obj.waypoints.map(extractPoints), expect(obj).to.have.property('waypoints');
strippedExpectedWaypoints = exp.map(extractPoints);
var matches = utils.eql(strippedWaypoints, strippedExpectedWaypoints); assertWaypoints(this, obj.id + '#waypoints', getPoints(obj.waypoints), getPoints(exp));
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
* consists of the correct DI waypoints.
*
* Does not take the original docking into account.
*
* @example
*
* expect(connection).to.have.diWaypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
*
* @param {Connection|Array<Point>} exp
*/
Assertion.addMethod('diWaypoints', function(exp) {
var obj = this._obj;
var bo = getBusinessObject(obj);
expect(bo).to.have.property('di');
expect(bo.di).to.have.property('waypoint');
assertWaypoints(this, obj.id + '#di#waypoint', getPoints(bo.di.waypoint), getPoints(exp));
});
/** /**
* A simple waypoints matcher, that verifies a connection * A simple waypoints matcher, that verifies a connection
* has the given start docking. * has the given start docking.
@ -121,4 +137,29 @@ export default function(chai, utils) {
); );
}); });
// helpers ////////////////
function assertWaypoints(self, desc, waypoints, expectedWaypoints) {
var matches = utils.eql(waypoints, expectedWaypoints);
var waypointsStr = inspect(waypoints),
expectedWaypointsStr = inspect(expectedWaypoints);
var theAssert = new Assertion(waypoints);
// transfer negate status
utils.transferFlags(self, theAssert, false);
theAssert.assert(
matches,
'expected <' + desc + '> ' +
'to equal \n ' + expectedWaypointsStr +
'\nbut got\n ' + waypointsStr,
'expected <' + desc + '> ' +
'not to equal \n ' + expectedWaypoints
);
}
} }