Skip to content

Commit 83834f8

Browse files
Static shape, annotation and image tests are passing
What seemed like the problem was that the gd.layout remained referring to a single object obtained from the test/image/mocks/domain_ref_base.json file, so if we changed the layout in one test, it remained changed in the other test. What this seems to mean is that Plotly.newPlot doesn't make a copy of the layout it's handed (?). Any how, this was fixed by calling `Lib.extendDeep({},mock)` for each test, which obtains a new object pointing to new memory and therefore will not be affected by previous tests. Tests for editable mode do not exist yet.
1 parent 0f1eae6 commit 83834f8

File tree

3 files changed

+94
-92
lines changed

3 files changed

+94
-92
lines changed

test/jasmine/assets/domain_ref/components.js

Lines changed: 63 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,46 @@ var destroyGraphDiv = require('../../assets/destroy_graph_div');
1717
var pixelCalc = require('../../assets/pixel_calc');
1818
var getSVGElemScreenBBox = require(
1919
'../../assets/get_svg_elem_screen_bbox');
20+
var SVGTools = require(
21+
'../../assets/svg_tools');
2022
var Lib = require('../../../../src/lib');
2123
var Axes = require('../../../../src/plots/cartesian/axes');
2224
var axisIds = require('../../../../src/plots/cartesian/axis_ids');
2325
var testImage = 'https://images.plot.ly/language-icons/api-home/js-logo.png';
2426
var iterable = require('extra-iterable');
2527
var delay = require('../../assets/delay');
2628

27-
var testMock = Lib.extendDeep({},
28-
require('../../../image/mocks/domain_ref_base.json'));
29+
var testMock = require('../../../image/mocks/domain_ref_base.json');
30+
31+
var defaultLayout = {
32+
"xaxis": {
33+
"domain": [0, 0.75],
34+
"range": [1, 3]
35+
},
36+
"yaxis": {
37+
"domain": [0, 0.4],
38+
"range": [1, 4]
39+
},
40+
"xaxis2": {
41+
"domain": [0.75, 1],
42+
"range": [1, 4],
43+
"anchor": "y2"
44+
},
45+
"yaxis2": {
46+
"domain": [0.4, 1],
47+
"range": [1, 3],
48+
"anchor": "x2"
49+
},
50+
"margin": {
51+
"l": 100,
52+
"r": 100,
53+
"t": 100,
54+
"b": 100,
55+
"autoexpand": false
56+
},
57+
"width": 400,
58+
"height": 400
59+
};
2960

3061
// NOTE: this tolerance is in pixels
3162
var EQUALITY_TOLERANCE = 1e-2;
@@ -373,7 +404,10 @@ function imageToBBox(layout, img) {
373404

374405

375406
function coordsEq(a, b) {
376-
return Math.abs(a - b) < EQUALITY_TOLERANCE;
407+
if (a && b) {
408+
return Math.abs(a - b) < EQUALITY_TOLERANCE;
409+
}
410+
return false;
377411
}
378412

379413
function compareBBoxes(a, b) {
@@ -438,6 +472,8 @@ function checkAROPosition(gd, aro) {
438472
var aroPathBBox = getSVGElemScreenBBox(aroPath);
439473
var aroBBox = shapeToBBox(gd.layout, aro);
440474
var ret = compareBBoxes(aroBBox, aroPathBBox);
475+
console.log("aroBBox: " + JSON.stringify(aroBBox))
476+
console.log("aroPathBBox: " + JSON.stringify(SVGTools.SVGRectToObj(aroPathBBox)))
441477
return ret;
442478
}
443479

@@ -535,6 +571,7 @@ function testShape(
535571
yaroPos,
536572
aroType
537573
) {
574+
console.log('gd.layout: ', JSON.stringify(gd.layout));
538575
var aro = {
539576
type: aroType,
540577
line: {
@@ -549,6 +586,7 @@ function testShape(
549586
// change to log axes if need be
550587
logAxisIfAxType(gd.layout, layout, 'x' + xAxNum, xaxisType);
551588
logAxisIfAxType(gd.layout, layout, 'y' + yAxNum, yaxisType);
589+
console.log('layout: ', JSON.stringify(layout));
552590
return Plotly.relayout(gd, layout)
553591
.then(function(gd) {
554592
return checkAROPosition(gd, aro);
@@ -562,15 +600,16 @@ function describeShapeComboTest(combo) {
562600
var xaroPos = combo[3];
563601
var yaroPos = combo[4];
564602
var gd_id = combo[5];
603+
var xid = axispair[0];
604+
var yid = axispair[1];
565605
return [
566-
"should create a plot with graph ID",
567-
gd_id,
568-
" with parameters:", "\n",
606+
"#", gd_id,
607+
"should create a plot with parameters:", "\n",
569608
"x-axis type:", xaxisType, "\n",
570609
"y-axis type:", yaxisType, "\n",
571-
"axis pair:", axispair, "\n",
572-
"ARO position x:", xaroPos, "\n",
573-
"ARO position y:", yaroPos, "\n",
610+
"axis pair:", xid, yid, "\n",
611+
"ARO position x:", JSON.stringify(xaroPos), "\n",
612+
"ARO position y:", JSON.stringify(yaroPos), "\n",
574613
].join(' ');
575614
}
576615

@@ -582,70 +621,14 @@ function testShapeCombo(combo, assert, gd) {
582621
var yaroPos = combo[4];
583622
var xAxNum = axispair[0].substr(1);
584623
var yAxNum = axispair[1].substr(1);
585-
return Plotly.newPlot(gd, testMock)
624+
return Plotly.newPlot(gd, Lib.extendDeep({}, testMock))
586625
.then(function(gd) {
587-
return testShape(gd,xAxNum,xaxisType,xaroPos,yAxNum,yaxisType,yaroPos,'shape');
626+
return testShape(gd, xAxNum, xaxisType, xaroPos, yAxNum, yaxisType, yaroPos, 'shape');
588627
}).then(function(test_ret) {
589628
assert(test_ret);
590629
});
591630
}
592631

593-
var testDomRefAROCombo = function(combo) {
594-
var xAxNum = combo[0];
595-
var xaxisType = combo[1];
596-
var xaroPos = combo[2];
597-
var yAxNum = combo[3];
598-
var yaxisType = combo[4];
599-
var yaroPos = combo[5];
600-
var aroType = combo[6];
601-
it('should draw a ' + aroType +
602-
' for x' + xAxNum + ' of type ' +
603-
xaxisType +
604-
' with a value referencing ' +
605-
xaroPos.ref +
606-
' and for y' + yAxNum + ' of type ' +
607-
yaxisType +
608-
' with a value referencing ' +
609-
yaroPos.ref,
610-
function(done) {
611-
var gd = createGraphDiv();
612-
var mock = testMock;
613-
if (DEBUG) {
614-
console.log(combo);
615-
}
616-
Plotly.newPlot(gd, mock)
617-
var aro = {
618-
type: aroType,
619-
line: {
620-
color: aroColor
621-
}
622-
};
623-
aroFromAROPos(aro, 'x', xAxNum, xaroPos);
624-
aroFromAROPos(aro, 'y', yAxNum, yaroPos);
625-
var layout = {
626-
shapes: [aro]
627-
};
628-
// change to log axes if need be
629-
logAxisIfAxType(gd.layout, layout, 'x' + xAxNum, xaxisType);
630-
logAxisIfAxType(gd.layout, layout, 'y' + yAxNum, yaxisType);
631-
Plotly.relayout(gd, layout);
632-
console.log(checkAROPosition(gd, aro));
633-
destroyGraphDiv();
634-
});
635-
}
636-
637-
// Test correct aro positions
638-
function test_correct_aro_positions() {
639-
// for both x and y axes
640-
var testCombos = [...iterable.cartesianProduct([
641-
axNum, axisTypes, aroPositionsX, axNum, axisTypes,
642-
aroPositionsY, aroType
643-
])];
644-
// map all the combinations to a aro definition and check this aro is
645-
// placed properly
646-
testCombos.forEach(testDomRefAROCombo);
647-
}
648-
649632
function describeImageComboTest(combo) {
650633
var axistypex = combo[0];
651634
var axistypey = combo[1];
@@ -661,14 +644,13 @@ function describeImageComboTest(combo) {
661644
var yref = makeAxRef(yid, aroposy.ref);
662645
// TODO Add image combo test description
663646
return [
664-
"layout image with graph ID",
665-
gd_id,
666-
"with parameters:",
647+
"#", gd_id,
648+
"should create a plot with parameters:", "\n",
667649
"x-axis type:", axistypex, "\n",
668650
"y-axis type:", axistypey, "\n",
669-
"axis pair:", axispair, "\n",
670-
"ARO position x:", aroposx, "\n",
671-
"ARO position y:", aroposy, "\n",
651+
"axis pair:", xid, yid, "\n",
652+
"ARO position x:", JSON.stringify(aroposx), "\n",
653+
"ARO position y:", JSON.stringify(aroposy), "\n",
672654
"xanchor:", xanchor, "\n",
673655
"yanchor:", yanchor, "\n",
674656
"xref:", xref, "\n",
@@ -689,7 +671,7 @@ function testImageCombo(combo, assert, gd) {
689671
var yid = axispair[1];
690672
var xref = makeAxRef(xid, aroposx.ref);
691673
var yref = makeAxRef(yid, aroposy.ref);
692-
return Plotly.newPlot(gd, testMock)
674+
return Plotly.newPlot(gd, Lib.extendDeep({}, testMock))
693675
.then(function(gd) {
694676
return imageTest(gd, {}, axistypex, axistypey,
695677
aroposx.value[0], aroposy.value[0], aroposx.size,
@@ -713,14 +695,13 @@ function describeAnnotationComboTest(combo) {
713695
var xref = makeAxRef(xid, aroposx.ref);
714696
var yref = makeAxRef(yid, aroposy.ref);
715697
return [
716-
"should create a plot with graph ID",
717-
gd_id,
718-
" with parameters:", "\n",
698+
"#", gd_id,
699+
"should create a plot with parameters:", "\n",
719700
"x-axis type:", axistypex, "\n",
720701
"y-axis type:", axistypey, "\n",
721-
"axis pair:", axispair, "\n",
722-
"ARO position x:", aroposx, "\n",
723-
"ARO position y:", aroposy, "\n",
702+
"axis pair:", xid, yid, "\n",
703+
"ARO position x:", JSON.stringify(aroposx), "\n",
704+
"ARO position y:", JSON.stringify(aroposy), "\n",
724705
"arrow axis pair:", arrowaxispair, "\n",
725706
"xref:", xref, "\n",
726707
"yref:", yref, "\n",
@@ -745,7 +726,7 @@ function testAnnotationCombo(combo, assert, gd) {
745726
var y0 = aroposy.value[0];
746727
var ax = axref === 'pixel' ? aroposx.pixel : aroposx.value[1];
747728
var ay = ayref === 'pixel' ? aroposy.pixel : aroposy.value[1];
748-
return Plotly.newPlot(gd, testMock)
729+
return Plotly.newPlot(gd, Lib.extendDeep({}, testMock))
749730
.then(function(gd) {
750731
return annotationTest(gd, {}, x0, y0, ax, ay, xref, yref, axref,
751732
ayref, axistypex, axistypey, xid, yid);
@@ -859,4 +840,4 @@ module.exports = {
859840
},
860841
// utilities
861842
findAROByColor: findAROByColor
862-
};
843+
};

test/jasmine/assets/svg_tools.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
'use strict';
22

33
module.exports = {
4-
findParentSVG: findParentSVG
4+
findParentSVG: findParentSVG,
5+
SVGRectToObj: SVGRectToObj
56
};
67

78
function findParentSVG(node) {
89
var parentNode = node.parentNode;
910

10-
if(parentNode.tagName === 'svg') {
11+
if (parentNode.tagName === 'svg') {
1112
return parentNode;
1213
} else {
1314
return findParentSVG(parentNode);
1415
}
1516
}
17+
18+
function SVGRectToObj(svgrect) {
19+
var obj = {};
20+
obj.x = svgrect.x;
21+
obj.y = svgrect.y;
22+
obj.width = svgrect.width;
23+
obj.height = svgrect.height;
24+
return obj;
25+
}

test/jasmine/tests/domain_ref_test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@ function makeTests(component, filter) {
1616
var descriptions = component.descriptions().filter(filter);
1717
var tests = component.tests().filter(filter);
1818
var gd;
19-
beforeEach(function () { gd = createGraphDiv(); });
20-
afterEach(function () {
19+
beforeEach(function() {
20+
gd = createGraphDiv();
21+
});
22+
afterEach(function() {
2123
Plotly.purge(gd);
2224
destroyGraphDiv(gd);
25+
gd = null;
2326
});
2427
descriptions.forEach(function(d, i) {
2528
it(d, function(done) {
29+
console.log("testing " + d);
30+
gd.id = "graph-" + i;
2631
tests[i](function(v) {
2732
expect(v).toBe(true);
28-
},gd)
33+
}, gd)
2934
.catch(failTest)
3035
.then(done);
3136
});
@@ -35,18 +40,24 @@ function makeTests(component, filter) {
3540

3641
describe('Test annotations', makeTests(domainRefComponents.annotations,
3742
function(f, i) {
38-
if (testNumber === undefined) { return true; }
43+
if (testNumber === undefined) {
44+
return true;
45+
}
3946
return i == testNumber;
4047
}));
4148

4249
describe('Test images', makeTests(domainRefComponents.images,
4350
function(f, i) {
44-
if (testNumber === undefined) { return true; }
51+
if (testNumber === undefined) {
52+
return true;
53+
}
4554
return i == testNumber;
4655
}));
4756

48-
fdescribe('Test shapes', makeTests(domainRefComponents.shapes,
57+
describe('Test shapes', makeTests(domainRefComponents.shapes,
4958
function(f, i) {
50-
if (testNumber === undefined) { return true; }
59+
if (testNumber === undefined) {
60+
return true;
61+
}
5162
return i == testNumber;
52-
}));
63+
}));

0 commit comments

Comments
 (0)