UNPKG

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