ConnectionMatchers.js 3.1 KB

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