| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import {
- pick
- } from 'min-dash';
- import {
- getDi
- } from 'lib/util/ModelUtil';
- 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);
- }
- export default 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;
- assertBounds(this, obj.id ? obj.id : obj, getBounds(obj), getBounds(exp));
- });
- /**
- * 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;
- var di = getDi(obj);
- expect(di).to.exist;
- assertBounds(this, di.id, getBounds(di), getBounds(exp));
- });
- /**
- * 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;
- assertDimensions(this, obj.id ? obj.id : obj, getDimensions(obj), getDimensions(exp));
- });
- /**
- * 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) {
- var obj = this._obj;
- var di = getDi(obj);
- expect(di).to.exist;
- assertDimensions(this, di.id, getDimensions(di), getDimensions(exp));
- });
- /**
- * 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;
- assertPosition(this, obj.id ? obj.id : obj, getPosition(obj), getPosition(exp));
- });
- /**
- * 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 di = getDi(obj);
- expect(di).to.exist;
- assertPosition(this, di.id, getPosition(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
- utils.transferFlags(self, theAssert, false);
- theAssert.assert(
- matches,
- 'expected <' + desc + '> bounds ' +
- 'to equal \n ' + expectedBoundsStr +
- '\nbut got\n ' + boundsStr,
- 'expected <' + desc + '> bounds ' +
- 'not to equal \n ' + expectedBoundsStr,
- expectedBounds
- );
- }
- 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,
- expectedDimensions
- );
- }
- 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 +
- '\nbut got\n ' + positionStr,
- 'expected <' + desc + '> position ' +
- 'not to equal \n ' + expectedPositionStr,
- expectedPosition
- );
- }
- }
|