BoundsMatchers.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { pick } from 'min-dash';
  2. var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
  3. POSITION_ATTRS = [ 'x', 'y' ],
  4. DIMENSION_ATTRS = [ 'width', 'height' ];
  5. function getBounds(s) {
  6. if ('bounds' in s) {
  7. s = s.bounds;
  8. }
  9. // TLBR object
  10. if ('top' in s) {
  11. return {
  12. x: s.left,
  13. y: s.top,
  14. width: s.right - s.left,
  15. height: s.bottom - s.top
  16. };
  17. }
  18. // { x, y, width, height } object
  19. else {
  20. return pick(s, BOUNDS_ATTRS);
  21. }
  22. }
  23. function getDimensions(s) {
  24. return pick(getBounds(s), DIMENSION_ATTRS);
  25. }
  26. function getPosition(s) {
  27. return pick(getBounds(s), POSITION_ATTRS);
  28. }
  29. export default function(chai, utils) {
  30. var Assertion = chai.Assertion;
  31. function inspect(obj) {
  32. return utils.inspect(obj).replace(/\n /g, '');
  33. }
  34. /**
  35. * A simple bounds matcher, that verifies an element
  36. * has the correct { x, y, width, height }.
  37. *
  38. * @example
  39. *
  40. * expect(di.label).to.have.bounds({ x: 100, y: 100, width: 10, height: 20 });
  41. * expect(shape).to.have.bounds({ top: 100, left: 0, right: 200, bottom: 50 });
  42. *
  43. * @param {Bounds|TLBR} exp
  44. */
  45. Assertion.addMethod('bounds', function(exp) {
  46. var obj = this._obj;
  47. var objectBounds = getBounds(obj),
  48. expectedBounds = getBounds(exp);
  49. var matches = utils.eql(objectBounds, expectedBounds);
  50. var objectBoundsStr = inspect(objectBounds),
  51. expectedBoundsStr = inspect(expectedBounds);
  52. var theAssert = new Assertion(objectBounds);
  53. // transfer flags
  54. utils.transferFlags(this, theAssert, false);
  55. theAssert.assert(
  56. matches,
  57. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
  58. 'to equal \n ' + expectedBoundsStr +
  59. '\nbut got\n ' + objectBoundsStr,
  60. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
  61. 'not to equal \n ' + expectedBoundsStr
  62. );
  63. });
  64. /**
  65. * A simple dimensions matcher, that verifies an element
  66. * has the correct { width, height }.
  67. *
  68. * Unwraps `element.bounds` (BPMNDI) if present.
  69. *
  70. * @example
  71. *
  72. * expect(di.label).to.have.dimensions({ width: 10, height: 20 });
  73. *
  74. * @param {Dimensions} exp
  75. */
  76. Assertion.addMethod('dimensions', function(exp) {
  77. var obj = this._obj;
  78. var objectDimensions = getDimensions(obj),
  79. expectedDimensions = getDimensions(exp);
  80. var matches = utils.eql(objectDimensions, expectedDimensions);
  81. var objectDimensionsStr = inspect(objectDimensions),
  82. expectedDimensionsStr = inspect(expectedDimensions);
  83. var theAssert = new Assertion(objectDimensions);
  84. // transfer flags
  85. utils.transferFlags(this, theAssert, false);
  86. theAssert.assert(
  87. matches,
  88. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
  89. 'to equal \n ' + expectedDimensionsStr +
  90. '\nbut got\n ' + objectDimensionsStr,
  91. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
  92. 'not to equal \n ' + expectedDimensionsStr
  93. );
  94. });
  95. /**
  96. * A simple position matcher, that verifies an element
  97. * has the correct { x, y }.
  98. *
  99. * Unwraps `element.bounds` (BPMNDI) if present.
  100. *
  101. * @example
  102. *
  103. * expect(taskShape).to.have.position({ x: 100, y: 150 });
  104. *
  105. * @param {Point} exp
  106. */
  107. Assertion.addMethod('position', function(exp) {
  108. var obj = this._obj;
  109. var objectPosition = getPosition(obj),
  110. expectedPosition = getPosition(exp);
  111. var matches = utils.eql(objectPosition, expectedPosition);
  112. var objectPositionStr = inspect(objectPosition),
  113. expectedPositionStr = inspect(expectedPosition);
  114. var theAssert = new Assertion(objectPosition);
  115. // transfer flags
  116. utils.transferFlags(this, theAssert, false);
  117. theAssert.assert(
  118. matches,
  119. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
  120. 'to equal \n ' + expectedPositionStr +
  121. '\nbut got\n ' + objectPositionStr,
  122. 'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
  123. 'not to equal \n ' + expectedPositionStr
  124. );
  125. });
  126. }