@@ -59,9 +59,7 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
59
59
expect ( textStyle . fill ) . toBe ( expectation . fontColor , msg + ': font.color' ) ;
60
60
} ;
61
61
62
- function assertLabelContent ( label , expectation , msg ) {
63
- if ( ! expectation ) expectation = '' ;
64
-
62
+ function getLabelContent ( label ) {
65
63
var lines = label . selectAll ( 'tspan.line' ) ;
66
64
var content = [ ] ;
67
65
@@ -77,8 +75,15 @@ function assertLabelContent(label, expectation, msg) {
77
75
} else {
78
76
fill ( label ) ;
79
77
}
78
+ return content . join ( '\n' ) ;
79
+ }
80
+
81
+ function assertLabelContent ( label , expectation , msg ) {
82
+ if ( ! expectation ) expectation = '' ;
83
+
84
+ var content = getLabelContent ( label ) ;
80
85
81
- expect ( content . join ( '\n' ) ) . toBe ( expectation , msg + ': text content' ) ;
86
+ expect ( content ) . toBe ( expectation , msg + ': text content' ) ;
82
87
}
83
88
84
89
function count ( selector ) {
@@ -130,35 +135,63 @@ exports.assertHoverLabelContent = function(expectation, msg) {
130
135
expect ( ptCnt )
131
136
. toBe ( expectation . name . length , ptMsg + ' # of visible labels' ) ;
132
137
133
- var bboxes = [ ] ;
138
+ var observed = [ ] ;
139
+ var expected = expectation . nums . map ( function ( num , i ) {
140
+ return {
141
+ num : num ,
142
+ name : expectation . name [ i ] ,
143
+ order : ( expectation . hOrder || expectation . vOrder || [ ] ) . indexOf ( i )
144
+ } ;
145
+ } ) ;
134
146
d3 . selectAll ( ptSelector ) . each ( function ( _ , i ) {
135
147
var g = d3 . select ( this ) ;
136
148
var numsSel = g . select ( 'text.nums' ) ;
137
149
var nameSel = g . select ( 'text.name' ) ;
138
150
139
- assertLabelContent ( numsSel , expectation . nums [ i ] , ptMsg + ' (nums ' + i + ')' ) ;
140
- assertLabelContent ( nameSel , expectation . name [ i ] , ptMsg + ' (name ' + i + ')' ) ;
151
+ // Label selection can be out of order... dunno why, but on AJ's Mac,
152
+ // just for certain box and violin cases, the order looks correct but
153
+ // it's different from what we see in CI (and presumably on
154
+ // other systems) which looks wrong.
155
+ // Anyway we don't *really* care about the order within the selection,
156
+ // we just care that each label is correct. So collect all the info
157
+ // about each label, and sort both observed and expected identically.
158
+ observed . push ( {
159
+ num : getLabelContent ( numsSel ) ,
160
+ name : getLabelContent ( nameSel ) ,
161
+ bbox : this . getBoundingClientRect ( ) ,
162
+ order : - 1
163
+ } ) ;
141
164
142
165
if ( 'isRotated' in expectation ) {
143
166
expect ( g . attr ( 'transform' ) . match ( reRotate ) )
144
167
. negateIf ( expectation . isRotated )
145
168
. toBe ( null , ptMsg + ' ' + i + ' should be rotated' ) ;
146
169
}
147
-
148
- bboxes . push ( { bbox : this . getBoundingClientRect ( ) , index : i } ) ;
149
170
} ) ;
150
- if ( expectation . vOrder ) {
151
- bboxes . sort ( function ( a , b ) {
152
- return ( a . bbox . top + a . bbox . bottom - b . bbox . top - b . bbox . bottom ) / 2 ;
171
+ if ( expectation . vOrder || expectation . hOrder ) {
172
+ var o2 = observed . slice ( ) ;
173
+ o2 . sort ( function ( a , b ) {
174
+ return expectation . vOrder ?
175
+ ( a . bbox . top + a . bbox . bottom - b . bbox . top - b . bbox . bottom ) / 2 :
176
+ ( b . bbox . left + b . bbox . right - a . bbox . left - a . bbox . right ) / 2 ;
153
177
} ) ;
154
- expect ( bboxes . map ( function ( d ) { return d . index ; } ) ) . toEqual ( expectation . vOrder ) ;
155
- }
156
- if ( expectation . hOrder ) {
157
- bboxes . sort ( function ( a , b ) {
158
- return ( b . bbox . left + b . bbox . right - a . bbox . left - a . bbox . right ) / 2 ;
178
+ o2 . forEach ( function ( item , i ) {
179
+ item . order = i ;
180
+ delete item . bbox ;
159
181
} ) ;
160
- expect ( bboxes . map ( function ( d ) { return d . index ; } ) ) . toEqual ( expectation . hOrder ) ;
161
182
}
183
+ observed . sort ( labelSorter ) ;
184
+ expected . sort ( labelSorter ) ;
185
+ // don't use .toEqual here because we want the message
186
+ expect ( observed . length ) . toBe ( expected . length , ptMsg ) ;
187
+ observed . forEach ( function ( obsi , i ) {
188
+ var expi = expected [ i ] ;
189
+ expect ( obsi . num ) . toBe ( expi . num , ptMsg + ' (nums ' + i + ')' ) ;
190
+ expect ( obsi . name ) . toBe ( expi . name , ptMsg + ' (name ' + i + ')' ) ;
191
+ if ( expectation . vOrder || expectation . hOrder ) {
192
+ expect ( obsi . order ) . toBe ( expi . order , ptMsg + ' (order ' + i + ')' ) ;
193
+ }
194
+ } ) ;
162
195
} else {
163
196
if ( expectation . nums ) {
164
197
fail ( ptMsg + ': expecting *nums* labels, did not find any.' ) ;
@@ -181,6 +214,12 @@ exports.assertHoverLabelContent = function(expectation, msg) {
181
214
}
182
215
} ;
183
216
217
+ function labelSorter ( a , b ) {
218
+ if ( a . name !== b . name ) return a . name > b . name ? 1 : - 1 ;
219
+ if ( a . num !== b . num ) return a . num > b . num ? 1 : - 1 ;
220
+ return a . order - b . order ;
221
+ }
222
+
184
223
exports . assertClip = function ( sel , isClipped , size , msg ) {
185
224
expect ( sel . size ( ) ) . toBe ( size , msg + ' clip path (selection size)' ) ;
186
225
0 commit comments