Skip to content

Commit 5084daf

Browse files
committed
add heatmap plot and hover tests
1 parent 221a5a1 commit 5084daf

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

test/jasmine/tests/heatmap_test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
var Plotly = require('@lib/index');
12
var Plots = require('@src/plots/plots');
23
var Lib = require('@src/lib');
34

45
var convertColumnXYZ = require('@src/traces/heatmap/convert_column_xyz');
56
var Heatmap = require('@src/traces/heatmap');
67

8+
var d3 = require('d3');
9+
var createGraphDiv = require('../assets/create_graph_div');
10+
var destroyGraphDiv = require('../assets/destroy_graph_div');
711
var customMatchers = require('../assets/custom_matchers');
812

913

@@ -299,3 +303,97 @@ describe('heatmap calc', function() {
299303
expect(out.z).toBeCloseTo2DArray([[1, 2, 3], [3, 1, 2]]);
300304
});
301305
});
306+
307+
describe('heatmap plot', function() {
308+
'use strict';
309+
310+
afterEach(destroyGraphDiv);
311+
312+
it('should not draw traces that are off-screen', function(done) {
313+
var mock = require('@mocks/heatmap_multi-trace.json'),
314+
mockCopy = Lib.extendDeep({}, mock),
315+
gd = createGraphDiv();
316+
317+
function assertImageCnt(cnt) {
318+
var images = d3.selectAll('.hm').select('image');
319+
320+
expect(images.size()).toEqual(cnt);
321+
}
322+
323+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
324+
assertImageCnt(5);
325+
326+
return Plotly.relayout(gd, 'xaxis.range', [2, 3]);
327+
}).then(function() {
328+
assertImageCnt(2);
329+
330+
return Plotly.relayout(gd, 'xaxis.autorange', true);
331+
}).then(function() {
332+
assertImageCnt(5);
333+
334+
done();
335+
});
336+
});
337+
});
338+
339+
describe('heatmap hover', function() {
340+
'use strict';
341+
342+
var gd;
343+
344+
beforeAll(function(done) {
345+
jasmine.addMatchers(customMatchers);
346+
347+
gd = createGraphDiv();
348+
349+
var mock = require('@mocks/heatmap_multi-trace.json'),
350+
mockCopy = Lib.extendDeep({}, mock);
351+
352+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
353+
});
354+
355+
afterAll(destroyGraphDiv);
356+
357+
function _hover(gd, xval, yval) {
358+
var fullLayout = gd._fullLayout,
359+
calcData = gd.calcdata,
360+
hoverData = [];
361+
362+
for(var i = 0; i < calcData.length; i++) {
363+
var pointData = {
364+
index: false,
365+
distance: 20,
366+
cd: calcData[i],
367+
trace: calcData[i][0].trace,
368+
xa: fullLayout.xaxis,
369+
ya: fullLayout.yaxis
370+
};
371+
372+
var hoverPoint = Heatmap.hoverPoints(pointData, xval, yval);
373+
if(hoverPoint) hoverData.push(hoverPoint[0]);
374+
}
375+
376+
return hoverData;
377+
}
378+
379+
function assertLabels(hoverPoint, xLabel, yLabel, zLabel) {
380+
expect(hoverPoint.xLabelVal).toEqual(xLabel, 'have correct x label');
381+
expect(hoverPoint.yLabelVal).toEqual(yLabel, 'have correct y label');
382+
expect(hoverPoint.zLabelVal).toEqual(zLabel, 'have correct z label');
383+
}
384+
385+
it('should find closest point (case 1) and should', function() {
386+
var pt = _hover(gd, 0.5, 0.5)[0];
387+
388+
expect(pt.index).toEqual([1, 0], 'have correct index');
389+
assertLabels(pt, 1, 1, 4);
390+
});
391+
392+
it('should find closest point (case 2) and should', function() {
393+
var pt = _hover(gd, 1.5, 0.5)[0];
394+
395+
expect(pt.index).toEqual([0, 0], 'have correct index');
396+
assertLabels(pt, 2, 0.2, 6);
397+
});
398+
399+
});

0 commit comments

Comments
 (0)