chore(tests): add connection matchers

This commit is contained in:
Nico Rehwaldt 2015-10-10 01:24:20 +02:00 committed by pedesen
parent 03b4a59f84
commit a0a8b38c46
4 changed files with 293 additions and 133 deletions

View File

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

View File

@ -1,132 +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);
}
}
module.exports = function(chai, utils) {
var Assertion = chai.Assertion;
/**
* A simple bounds matcher, that verifies an element
* has the correct { x, y, width, height }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @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);
BOUNDS_ATTRS.forEach(function(attr) {
var bounds = new Assertion(objectBounds[attr]);
bounds.assert(
objectBounds[attr] == expectedBounds[attr],
'expected <' + obj.id + '#' + attr + '> to equal #{exp} but got #{act}',
'expected <' + obj.id + '#' + attr + '> to not equal #{exp}',
expectedBounds[attr],
objectBounds[attr]
);
});
});
/**
* 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 objectBounds = getBounds(obj),
expectedBounds = getBounds(exp);
DIMENSION_ATTRS.forEach(function(attr) {
var bounds = new Assertion(objectBounds[attr]);
bounds.assert(
objectBounds[attr] == expectedBounds[attr],
'expected <' + obj.id + '#' + attr + '> to equal #{exp} but got #{act}',
'expected <' + obj.id + '#' + attr + '> to not equal #{exp}',
expectedBounds[attr],
objectBounds[attr]
);
});
});
/**
* 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 objectBounds = getBounds(obj),
expectedBounds = getBounds(exp);
POSITION_ATTRS.forEach(function(attr) {
var bounds = new Assertion(objectBounds[attr]);
bounds.assert(
objectBounds[attr] == expectedBounds[attr],
'expected <' + obj.id + '#' + attr + '> to equal #{exp} but got #{act}',
'expected <' + obj.id + '#' + attr + '> to not equal #{exp}',
expectedBounds[attr],
objectBounds[attr]
);
});
});
};

View File

@ -0,0 +1,167 @@
'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

@ -0,0 +1,124 @@
'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
);
});
};