BoundsMatchers.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {
  2. pick
  3. } from 'min-dash';
  4. import {
  5. getDi
  6. } from 'lib/util/ModelUtil';
  7. var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
  8. POSITION_ATTRS = [ 'x', 'y' ],
  9. DIMENSION_ATTRS = [ 'width', 'height' ];
  10. function getBounds(s) {
  11. if ('bounds' in s) {
  12. s = s.bounds;
  13. }
  14. // TLBR object
  15. if ('top' in s) {
  16. return {
  17. x: s.left,
  18. y: s.top,
  19. width: s.right - s.left,
  20. height: s.bottom - s.top
  21. };
  22. }
  23. // { x, y, width, height } object
  24. else {
  25. return pick(s, BOUNDS_ATTRS);
  26. }
  27. }
  28. function getDimensions(s) {
  29. return pick(getBounds(s), DIMENSION_ATTRS);
  30. }
  31. function getPosition(s) {
  32. return pick(getBounds(s), POSITION_ATTRS);
  33. }
  34. export default function(chai, utils) {
  35. var Assertion = chai.Assertion;
  36. function inspect(obj) {
  37. return utils.inspect(obj).replace(/\n /g, '');
  38. }
  39. /**
  40. * A simple bounds matcher, that verifies an element
  41. * has the correct { x, y, width, height }.
  42. *
  43. * @example
  44. *
  45. * expect(di.label).to.have.bounds({ x: 100, y: 100, width: 10, height: 20 });
  46. * expect(shape).to.have.bounds({ top: 100, left: 0, right: 200, bottom: 50 });
  47. *
  48. * @param {Bounds|TLBR} exp
  49. */
  50. Assertion.addMethod('bounds', function(exp) {
  51. var obj = this._obj;
  52. assertBounds(this, obj.id ? obj.id : obj, getBounds(obj), getBounds(exp));
  53. });
  54. /**
  55. * A simple bounds matcher, that verifies an element
  56. * has the correct { x, y, width, height }.
  57. *
  58. * @example
  59. *
  60. * expect(di.label).to.have.diBounds({ x: 100, y: 100, width: 10, height: 20 });
  61. * expect(shape).to.have.diBounds({ top: 100, left: 0, right: 200, bottom: 50 });
  62. *
  63. * @param {Bounds|TLBR} exp
  64. */
  65. Assertion.addMethod('diBounds', function(exp) {
  66. var obj = this._obj;
  67. var di = getDi(obj);
  68. expect(di).to.exist;
  69. assertBounds(this, di.id, getBounds(di), getBounds(exp));
  70. });
  71. /**
  72. * A simple dimensions matcher, that verifies an element
  73. * has the correct { width, height }.
  74. *
  75. * Unwraps `element.bounds` (BPMNDI) if present.
  76. *
  77. * @example
  78. *
  79. * expect(di.label).to.have.dimensions({ width: 10, height: 20 });
  80. *
  81. * @param {Dimensions} exp
  82. */
  83. Assertion.addMethod('dimensions', function(exp) {
  84. var obj = this._obj;
  85. assertDimensions(this, obj.id ? obj.id : obj, getDimensions(obj), getDimensions(exp));
  86. });
  87. /**
  88. * A simple dimensions matcher, that verifies an elements
  89. * DI has the correct { width, height }.
  90. *
  91. * Unwraps `element.bounds` (BPMNDI) if present.
  92. *
  93. * @example
  94. *
  95. * expect(di.label).to.have.diDimensions({ width: 10, height: 20 });
  96. *
  97. * @param {Dimensions} exp
  98. */
  99. Assertion.addMethod('diDimensions', function(exp) {
  100. var obj = this._obj;
  101. var di = getDi(obj);
  102. expect(di).to.exist;
  103. assertDimensions(this, di.id, getDimensions(di), getDimensions(exp));
  104. });
  105. /**
  106. * A simple position matcher, that verifies an element
  107. * has the correct { x, y }.
  108. *
  109. * Unwraps `element.bounds` (BPMNDI) if present.
  110. *
  111. * @example
  112. *
  113. * expect(taskShape).to.have.position({ x: 100, y: 150 });
  114. *
  115. * @param {Point} exp
  116. */
  117. Assertion.addMethod('position', function(exp) {
  118. var obj = this._obj;
  119. assertPosition(this, obj.id ? obj.id : obj, getPosition(obj), getPosition(exp));
  120. });
  121. /**
  122. * A simple position matcher, that verifies an element
  123. * has the correct DI position { x, y }.
  124. *
  125. * Unwraps `element.bounds` (BPMNDI) if present.
  126. *
  127. * @example
  128. *
  129. * expect(taskShape).to.have.diPosition({ x: 100, y: 150 });
  130. *
  131. * @param {Point} exp
  132. */
  133. Assertion.addMethod('diPosition', function(exp) {
  134. var obj = this._obj;
  135. var di = getDi(obj);
  136. expect(di).to.exist;
  137. assertPosition(this, di.id, getPosition(di), getPosition(exp));
  138. });
  139. // helpers ////////////////
  140. function assertBounds(self, desc, bounds, expectedBounds) {
  141. var matches = utils.eql(bounds, expectedBounds);
  142. var boundsStr = inspect(bounds),
  143. expectedBoundsStr = inspect(expectedBounds);
  144. var theAssert = new Assertion(bounds);
  145. // transfer flags
  146. utils.transferFlags(self, theAssert, false);
  147. theAssert.assert(
  148. matches,
  149. 'expected <' + desc + '> bounds ' +
  150. 'to equal \n ' + expectedBoundsStr +
  151. '\nbut got\n ' + boundsStr,
  152. 'expected <' + desc + '> bounds ' +
  153. 'not to equal \n ' + expectedBoundsStr,
  154. expectedBounds
  155. );
  156. }
  157. function assertDimensions(self, desc, dimensions, expectedDimensions) {
  158. var matches = utils.eql(dimensions, expectedDimensions);
  159. var dimensionsStr = inspect(dimensions),
  160. expectedDimensionsStr = inspect(expectedDimensions);
  161. var theAssert = new Assertion(dimensions);
  162. // transfer flags
  163. utils.transferFlags(self, theAssert, false);
  164. theAssert.assert(
  165. matches,
  166. 'expected <' + desc + '> dimensions ' +
  167. 'to equal \n ' + expectedDimensionsStr +
  168. '\nbut got\n ' + dimensionsStr,
  169. 'expected <' + desc + '> dimensions ' +
  170. 'not to equal \n ' + expectedDimensionsStr,
  171. expectedDimensions
  172. );
  173. }
  174. function assertPosition(self, desc, position, expectedPosition) {
  175. var matches = utils.eql(position, expectedPosition);
  176. var positionStr = inspect(position),
  177. expectedPositionStr = inspect(expectedPosition);
  178. var theAssert = new Assertion(position);
  179. // transfer flags
  180. utils.transferFlags(self, theAssert, false);
  181. theAssert.assert(
  182. matches,
  183. 'expected <' + desc + '> position ' +
  184. 'to equal \n ' + expectedPositionStr +
  185. '\nbut got\n ' + positionStr,
  186. 'expected <' + desc + '> position ' +
  187. 'not to equal \n ' + expectedPositionStr,
  188. expectedPosition
  189. );
  190. }
  191. }