Skip to content

Commit e25b9ff

Browse files
committed
clearer contour heatmap ordering test
and add assertNodeOrder as a custom assertion
1 parent 7843d83 commit e25b9ff

File tree

2 files changed

+75
-32
lines changed

2 files changed

+75
-32
lines changed

test/jasmine/assets/custom_assertions.js

+63
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,66 @@ exports.assertPlotSize = function(opts, msg) {
247247
if(widthLessThan) expect(actualWidth).toBeLessThan(widthLessThan - 1, 'widthLessThan' + msgPlus);
248248
if(heightLessThan) expect(actualHeight).toBeLessThan(heightLessThan - 1, 'heightLessThan' + msgPlus);
249249
};
250+
251+
/**
252+
* Ordering test - since SVG layering is purely dependent on ordering in the
253+
* node tree, this tells you if the items are layered correctly.
254+
* Note that we only take the first matching node for each selector, and it's
255+
* not necessary that the nodes be siblings or at the same level of nesting.
256+
*
257+
* @param {string} selectorBehind: css selector for the node that should be behind
258+
* @param {string} selectorInFront: css selector for the node that should be in front
259+
* @param {string} msg: context for debugging
260+
*/
261+
exports.assertNodeOrder = function(selectorBehind, selectorInFront, msg) {
262+
var nodeBehind = document.querySelector(selectorBehind);
263+
var nodeInFront = document.querySelector(selectorInFront);
264+
if(!nodeBehind) {
265+
fail(selectorBehind + ' not found (' + msg + ')');
266+
}
267+
else if(!nodeInFront) {
268+
fail(selectorInFront + ' not found (' + msg + ')');
269+
}
270+
else {
271+
var parentsBehind = getParents(nodeBehind);
272+
var parentsInFront = getParents(nodeInFront);
273+
274+
var commonParent = null;
275+
var siblingBehind = null;
276+
var siblingInFront = null;
277+
for(var i = 0; i < parentsBehind.length; i++) {
278+
if(parentsBehind[i] === parentsInFront[i]) {
279+
commonParent = parentsBehind[i];
280+
}
281+
else {
282+
siblingBehind = parentsBehind[i];
283+
siblingInFront = parentsInFront[i];
284+
break;
285+
}
286+
}
287+
var allSiblings = collectionToArray(commonParent.children);
288+
var behindIndex = allSiblings.indexOf(siblingBehind);
289+
var frontIndex = allSiblings.indexOf(siblingInFront);
290+
291+
// sanity check - if these fail there's just something wrong in this routine
292+
expect(behindIndex).toBeGreaterThan(-1, 'error in assertNodeOrder: ' + msg);
293+
expect(frontIndex).toBeGreaterThan(-1, 'error in assertNodeOrder: ' + msg);
294+
295+
// the real test
296+
expect(frontIndex).toBeGreaterThan(behindIndex,
297+
'"' + selectorBehind + '" is not behind "' + selectorInFront + '": ' + msg);
298+
}
299+
};
300+
301+
function getParents(node) {
302+
var parent = node.parentNode;
303+
if(parent) return getParents(parent).concat(node);
304+
return [node];
305+
}
306+
307+
function collectionToArray(collection) {
308+
var len = collection.length;
309+
var a = new Array(len);
310+
for(var i = 0; i < len; i++) a[i] = collection[i];
311+
return a;
312+
}

test/jasmine/tests/contour_test.js

+12-32
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var colorScales = require('@src/components/colorscale/scales');
1111
var failTest = require('../assets/fail_test');
1212
var createGraphDiv = require('../assets/create_graph_div');
1313
var destroyGraphDiv = require('../assets/destroy_graph_div');
14-
var checkTicks = require('../assets/custom_assertions').checkTicks;
14+
var customAssertions = require('../assets/custom_assertions');
15+
var checkTicks = customAssertions.checkTicks;
16+
var assertNodeOrder = customAssertions.assertNodeOrder;
1517
var supplyAllDefaults = require('../assets/supply_defaults');
1618

1719

@@ -412,25 +414,12 @@ describe('contour plotting and editing', function() {
412414
});
413415

414416
it('should always draw heatmap coloring layer below contour lines', function(done) {
415-
var cnt = 0;
416-
417-
function _assert(exp) {
418-
var msg = ' index in <g.contourlayer> (call #' + cnt + ')';
419-
var contourPlot = gd.querySelector('.xy > .plot > .contourlayer > .contour');
420-
var hmIndex = -1;
421-
var contoursIndex = -1;
422-
423-
for(var i in contourPlot.children) {
424-
var child = contourPlot.children[i];
425-
if(child.querySelector) {
426-
if(child.querySelector('.hm')) hmIndex = +i;
427-
else if(child.querySelector('.contourlevel')) contoursIndex = +i;
428-
}
429-
}
430-
431-
expect(hmIndex).toBe(exp.hmIndex, 'heatmap' + msg);
432-
expect(contoursIndex).toBe(exp.contoursIndex, 'contours' + msg);
433-
cnt++;
417+
function _assertNoHeatmap(msg) {
418+
msg = ' (' + msg + ')';
419+
// all we care about here *really* is that there are contour levels
420+
// *somewhere* on the plot, and there is no heatmap anywhere.
421+
expect(gd.querySelector('.hm')).toBe(null, 'heatmap exists' + msg);
422+
expect(gd.querySelector('.contourlevel')).not.toBe(null, 'missing contours' + msg);
434423
}
435424

436425
Plotly.newPlot(gd, [{
@@ -439,24 +428,15 @@ describe('contour plotting and editing', function() {
439428
contours: {coloring: 'heatmap'}
440429
}])
441430
.then(function() {
442-
_assert({
443-
hmIndex: 0,
444-
contoursIndex: 3
445-
});
431+
assertNodeOrder('.hm', '.contourlevel', 'initial heatmap coloring');
446432
return Plotly.restyle(gd, 'contours.coloring', 'lines');
447433
})
448434
.then(function() {
449-
_assert({
450-
hmIndex: -1,
451-
contoursIndex: 3
452-
});
435+
_assertNoHeatmap('line coloring');
453436
return Plotly.restyle(gd, 'contours.coloring', 'heatmap');
454437
})
455438
.then(function() {
456-
_assert({
457-
hmIndex: 0,
458-
contoursIndex: 3
459-
});
439+
assertNodeOrder('.hm', '.contourlevel', 'back to heatmap coloring');
460440
})
461441
.catch(failTest)
462442
.then(done);

0 commit comments

Comments
 (0)