From a0a8b38c463fccc80d87b3cee462517f2811adab Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sat, 10 Oct 2015 01:24:20 +0200 Subject: [PATCH] chore(tests): add connection matchers --- test/TestHelper.js | 3 +- test/matchers/Bounds.js | 132 ---------------------- test/matchers/BoundsMatchers.js | 167 ++++++++++++++++++++++++++++ test/matchers/ConnectionMatchers.js | 124 +++++++++++++++++++++ 4 files changed, 293 insertions(+), 133 deletions(-) delete mode 100644 test/matchers/Bounds.js create mode 100644 test/matchers/BoundsMatchers.js create mode 100644 test/matchers/ConnectionMatchers.js diff --git a/test/TestHelper.js b/test/TestHelper.js index 3c141ab5..009a5d71 100644 --- a/test/TestHelper.js +++ b/test/TestHelper.js @@ -12,4 +12,5 @@ TestHelper.insertCSS('diagram-js-testing.css', // add suite specific matchers -global.chai.use(require('./matchers/Bounds')); \ No newline at end of file +global.chai.use(require('./matchers/BoundsMatchers')); +global.chai.use(require('./matchers/ConnectionMatchers')); \ No newline at end of file diff --git a/test/matchers/Bounds.js b/test/matchers/Bounds.js deleted file mode 100644 index 6a0f97dc..00000000 --- a/test/matchers/Bounds.js +++ /dev/null @@ -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] - ); - }); - }); - -}; \ No newline at end of file diff --git a/test/matchers/BoundsMatchers.js b/test/matchers/BoundsMatchers.js new file mode 100644 index 00000000..3b9f2232 --- /dev/null +++ b/test/matchers/BoundsMatchers.js @@ -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 + ); + }); + +}; \ No newline at end of file diff --git a/test/matchers/ConnectionMatchers.js b/test/matchers/ConnectionMatchers.js new file mode 100644 index 00000000..89c8f20c --- /dev/null +++ b/test/matchers/ConnectionMatchers.js @@ -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} 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 + ); + }); + +};