1 | import {
|
2 | pick
|
3 | } from 'min-dash';
|
4 |
|
5 | var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
|
6 | POSITION_ATTRS = [ 'x', 'y' ],
|
7 | DIMENSION_ATTRS = [ 'width', 'height' ];
|
8 |
|
9 | function getBounds(s) {
|
10 |
|
11 | if ('bounds' in s) {
|
12 | s = s.bounds;
|
13 | }
|
14 |
|
15 |
|
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 |
|
26 | else {
|
27 | return pick(s, BOUNDS_ATTRS);
|
28 | }
|
29 | }
|
30 |
|
31 | function getDimensions(s) {
|
32 | return pick(getBounds(s), DIMENSION_ATTRS);
|
33 | }
|
34 |
|
35 | function getPosition(s) {
|
36 | return pick(getBounds(s), POSITION_ATTRS);
|
37 | }
|
38 |
|
39 |
|
40 | export 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 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
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 |
|
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 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
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 |
|
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 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
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 |
|
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 | }
|