2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
pick
|
|
|
|
} from 'min-dash';
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
import {
|
2021-08-06 08:49:45 +00:00
|
|
|
getDi
|
2020-01-20 10:50:03 +00:00
|
|
|
} from 'lib/util/ModelUtil';
|
|
|
|
|
2017-10-11 13:19:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function(chai, utils) {
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
assertBounds(this, obj.id ? obj.id : obj, getBounds(obj), getBounds(exp));
|
|
|
|
});
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
var di = getDi(obj);
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
expect(di).to.exist;
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
assertBounds(this, di.id, getBounds(di), getBounds(exp));
|
2017-10-11 13:19:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-01-20 10:50:03 +00:00
|
|
|
|
2017-10-11 13:19:36 +00:00
|
|
|
var obj = this._obj;
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
assertDimensions(this, obj.id ? obj.id : obj, getDimensions(obj), getDimensions(exp));
|
|
|
|
});
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
var obj = this._obj;
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
var di = getDi(obj);
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
expect(di).to.exist;
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
assertDimensions(this, di.id, getDimensions(di), getDimensions(exp));
|
2017-10-11 13:19:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-01-20 10:50:03 +00:00
|
|
|
|
2017-10-11 13:19:36 +00:00
|
|
|
var obj = this._obj;
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
assertPosition(this, obj.id ? obj.id : obj, getPosition(obj), getPosition(exp));
|
|
|
|
});
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
var di = getDi(obj);
|
2020-01-20 10:50:03 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
expect(di).to.exist;
|
2020-01-20 10:50:03 +00:00
|
|
|
|
2021-08-06 08:49:45 +00:00
|
|
|
assertPosition(this, di.id, getPosition(di), getPosition(exp));
|
2020-01-20 10:50:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
utils.transferFlags(self, theAssert, false);
|
|
|
|
|
|
|
|
theAssert.assert(
|
|
|
|
matches,
|
|
|
|
'expected <' + desc + '> bounds ' +
|
|
|
|
'to equal \n ' + expectedBoundsStr +
|
|
|
|
'\nbut got\n ' + boundsStr,
|
|
|
|
'expected <' + desc + '> bounds ' +
|
2020-01-20 13:40:04 +00:00
|
|
|
'not to equal \n ' + expectedBoundsStr,
|
|
|
|
expectedBounds
|
2020-01-20 10:50:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ' +
|
2020-01-20 13:40:04 +00:00
|
|
|
'not to equal \n ' + expectedDimensionsStr,
|
|
|
|
expectedDimensions
|
2020-01-20 10:50:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertPosition(self, desc, position, expectedPosition) {
|
|
|
|
|
|
|
|
var matches = utils.eql(position, expectedPosition);
|
|
|
|
|
|
|
|
var positionStr = inspect(position),
|
2017-10-11 13:19:36 +00:00
|
|
|
expectedPositionStr = inspect(expectedPosition);
|
|
|
|
|
|
|
|
|
2020-01-20 10:50:03 +00:00
|
|
|
var theAssert = new Assertion(position);
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
// transfer flags
|
2020-01-20 10:50:03 +00:00
|
|
|
utils.transferFlags(self, theAssert, false);
|
2017-10-11 13:19:36 +00:00
|
|
|
|
|
|
|
theAssert.assert(
|
|
|
|
matches,
|
2020-01-20 10:50:03 +00:00
|
|
|
'expected <' + desc + '> position ' +
|
2017-10-11 13:19:36 +00:00
|
|
|
'to equal \n ' + expectedPositionStr +
|
2020-01-20 10:50:03 +00:00
|
|
|
'\nbut got\n ' + positionStr,
|
|
|
|
'expected <' + desc + '> position ' +
|
2020-01-20 13:40:04 +00:00
|
|
|
'not to equal \n ' + expectedPositionStr,
|
|
|
|
expectedPosition
|
2017-10-11 13:19:36 +00:00
|
|
|
);
|
2020-01-20 10:50:03 +00:00
|
|
|
}
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|