Skip to content

Commit 3a70fee

Browse files
committed
make hover label test independent of selection order
1 parent 427fd14 commit 3a70fee

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

test/jasmine/assets/custom_assertions.js

+57-18
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
5959
expect(textStyle.fill).toBe(expectation.fontColor, msg + ': font.color');
6060
};
6161

62-
function assertLabelContent(label, expectation, msg) {
63-
if(!expectation) expectation = '';
64-
62+
function getLabelContent(label) {
6563
var lines = label.selectAll('tspan.line');
6664
var content = [];
6765

@@ -77,8 +75,15 @@ function assertLabelContent(label, expectation, msg) {
7775
} else {
7876
fill(label);
7977
}
78+
return content.join('\n');
79+
}
80+
81+
function assertLabelContent(label, expectation, msg) {
82+
if(!expectation) expectation = '';
83+
84+
var content = getLabelContent(label);
8085

81-
expect(content.join('\n')).toBe(expectation, msg + ': text content');
86+
expect(content).toBe(expectation, msg + ': text content');
8287
}
8388

8489
function count(selector) {
@@ -130,35 +135,63 @@ exports.assertHoverLabelContent = function(expectation, msg) {
130135
expect(ptCnt)
131136
.toBe(expectation.name.length, ptMsg + ' # of visible labels');
132137

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+
});
134146
d3.selectAll(ptSelector).each(function(_, i) {
135147
var g = d3.select(this);
136148
var numsSel = g.select('text.nums');
137149
var nameSel = g.select('text.name');
138150

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+
});
141164

142165
if('isRotated' in expectation) {
143166
expect(g.attr('transform').match(reRotate))
144167
.negateIf(expectation.isRotated)
145168
.toBe(null, ptMsg + ' ' + i + ' should be rotated');
146169
}
147-
148-
bboxes.push({bbox: this.getBoundingClientRect(), index: i});
149170
});
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;
153177
});
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;
159181
});
160-
expect(bboxes.map(function(d) { return d.index; })).toEqual(expectation.hOrder);
161182
}
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+
});
162195
} else {
163196
if(expectation.nums) {
164197
fail(ptMsg + ': expecting *nums* labels, did not find any.');
@@ -181,6 +214,12 @@ exports.assertHoverLabelContent = function(expectation, msg) {
181214
}
182215
};
183216

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+
184223
exports.assertClip = function(sel, isClipped, size, msg) {
185224
expect(sel.size()).toBe(size, msg + ' clip path (selection size)');
186225

0 commit comments

Comments
 (0)