ConnectionMatchers.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. pick
  3. } from 'min-dash';
  4. var POSITION_ATTRS = [ 'x', 'y' ];
  5. function extractPoints(point) {
  6. return pick(point, POSITION_ATTRS);
  7. }
  8. export default function(chai, utils) {
  9. var Assertion = chai.Assertion;
  10. function inspect(obj) {
  11. return utils.inspect(obj).replace(/\n /g, '');
  12. }
  13. /**
  14. * A simple waypoints matcher, that verifies a connection
  15. * consists of the correct connection points.
  16. *
  17. * Does not take the original docking into account.
  18. *
  19. * @example
  20. *
  21. * expect(connection).to.have.waypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
  22. *
  23. * @param {Connection|Array<Point>} exp
  24. */
  25. Assertion.addMethod('waypoints', function(exp) {
  26. var obj = this._obj;
  27. var strippedWaypoints = obj.waypoints.map(extractPoints),
  28. strippedExpectedWaypoints = exp.map(extractPoints);
  29. var matches = utils.eql(strippedWaypoints, strippedExpectedWaypoints);
  30. var strippedWaypointsStr = inspect(strippedWaypoints),
  31. strippedExpectedWaypointsStr = inspect(strippedExpectedWaypoints);
  32. var theAssert = new Assertion(strippedWaypoints);
  33. // transfer negate status
  34. utils.transferFlags(this, theAssert, false);
  35. theAssert.assert(
  36. matches,
  37. 'expected <' + obj.id + '#waypoints> ' +
  38. 'to equal \n ' + strippedExpectedWaypointsStr +
  39. '\nbut got\n ' + strippedWaypointsStr,
  40. 'expected <' + obj.id + '#waypoints> ' +
  41. 'not to equal \n ' + strippedExpectedWaypoints
  42. );
  43. });
  44. /**
  45. * A simple waypoints matcher, that verifies a connection
  46. * has the given start docking.
  47. *
  48. * @example
  49. *
  50. * expect(connection).to.have.startDocking({ x: 100, y: 100 });
  51. *
  52. * @param {Point} exp
  53. */
  54. Assertion.addMethod('startDocking', function(exp) {
  55. var obj = this._obj;
  56. var startPoint = obj.waypoints[0],
  57. startDocking = startPoint && startPoint.original;
  58. var matches = utils.eql(startDocking, exp);
  59. var startDockingStr = inspect(startDocking),
  60. expectedStartDockingStr = inspect(exp);
  61. var theAssert = new Assertion(startDocking);
  62. // transfer negate status
  63. utils.transferFlags(this, theAssert, false);
  64. theAssert.assert(
  65. matches,
  66. 'expected <' + obj.id + '> to have startDocking ' +
  67. expectedStartDockingStr + ' but got ' + startDockingStr
  68. );
  69. });
  70. /**
  71. * A simple waypoints matcher, that verifies a connection
  72. * has the given start docking.
  73. *
  74. * @example
  75. *
  76. * expect(connection).to.have.endDocking({ x: 100, y: 100 });
  77. *
  78. * @param {Point} exp
  79. */
  80. Assertion.addMethod('endDocking', function(exp) {
  81. var obj = this._obj;
  82. var endPoint = obj.waypoints[obj.waypoints.length - 1],
  83. endDocking = endPoint && endPoint.original;
  84. var matches = utils.eql(endDocking, exp);
  85. var endDockingStr = inspect(endDocking),
  86. expectedEndDockingStr = inspect(exp);
  87. var theAssert = new Assertion(endDocking);
  88. // transfer negate status
  89. utils.transferFlags(this, theAssert, false);
  90. theAssert.assert(
  91. matches,
  92. 'expected <' + obj.id + '> to have endDocking ' +
  93. expectedEndDockingStr + ' but got ' + endDockingStr
  94. );
  95. });
  96. }