UNPKG

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