BoundsMatchers.js 4.0 KB

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