Skip to content

Commit 194ba1d

Browse files
authored
Merge pull request #2805 from plotly/transform-removal-from-react-fix
Transform removal from react fix
2 parents ab8b922 + acc98b2 commit 194ba1d

File tree

3 files changed

+100
-66
lines changed

3 files changed

+100
-66
lines changed

src/plots/plots.js

+2
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,8 @@ plots.doCalcdata = function(gd, traces) {
25152515

25162516
if(trace.visible === true) {
25172517

2518+
// clear existing ref in case it got relinked
2519+
delete trace._indexToPoints;
25182520
// keep ref of index-to-points map object of the *last* enabled transform,
25192521
// this index-to-points map object is required to determine the calcdata indices
25202522
// that correspond to input indices (e.g. from 'selectedpoints')

test/jasmine/tests/transform_filter_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -1274,4 +1274,27 @@ describe('filter transforms interactions', function() {
12741274
.then(done);
12751275
});
12761276

1277+
it('should clear indexToPoints on removal', function(done) {
1278+
var gd = createGraphDiv();
1279+
1280+
Plotly.react(gd, [{
1281+
y: [1, 2, 3, 1, 2, 3],
1282+
transforms: [{
1283+
type: 'filter',
1284+
target: 'y',
1285+
operation: '<',
1286+
value: 3
1287+
}]
1288+
}])
1289+
.then(function() {
1290+
expect(gd._fullData[0]._indexToPoints).toEqual({0: [0], 1: [1], 2: [3], 3: [4]});
1291+
return Plotly.react(gd, [{ y: [1, 2, 3, 1, 2, 3] }]);
1292+
})
1293+
.then(function() {
1294+
expect(gd._fullData[0]._indexToPoints).toBeUndefined();
1295+
})
1296+
.catch(failTest)
1297+
.then(done);
1298+
});
1299+
12771300
});

test/jasmine/tests/transform_multi_test.js

+75-66
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Lib = require('@src/lib');
66

77
var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
9+
var failTest = require('../assets/fail_test');
910
var customAssertions = require('../assets/custom_assertions');
1011
var supplyAllDefaults = require('../assets/supply_defaults');
1112

@@ -434,9 +435,9 @@ describe('multiple transforms:', function() {
434435
expect(gd._fullData[0].transforms[0]._indexToPoints).toEqual({0: [1], 1: [3], 2: [4]});
435436
expect(gd._fullData[0].transforms[1]._indexToPoints).toEqual({0: [1, 3], 1: [4]});
436437
expect(gd._fullData[0].transforms[2]._indexToPoints).toEqual({0: [4]});
437-
438-
done();
439-
});
438+
})
439+
.catch(failTest)
440+
.then(done);
440441
});
441442

442443

@@ -455,9 +456,9 @@ describe('multiple transforms:', function() {
455456
expect(gd._fullData[1].y).toEqual([2, 3]);
456457

457458
assertDims([2, 2]);
458-
459-
done();
460-
});
459+
})
460+
.catch(failTest)
461+
.then(done);
461462
});
462463

463464
it('Plotly.plot should plot the transform traces (reverse case)', function(done) {
@@ -477,9 +478,9 @@ describe('multiple transforms:', function() {
477478
expect(gd._fullData[1].y).toEqual([2, 3]);
478479

479480
assertDims([2, 2]);
480-
481-
done();
482-
});
481+
})
482+
.catch(failTest)
483+
.then(done);
483484
});
484485

485486
it('Plotly.restyle should work', function(done) {
@@ -529,9 +530,9 @@ describe('multiple transforms:', function() {
529530
['rgb(0, 128, 0)', 'rgb(255, 0, 0)'],
530531
[0.4, 0.4]
531532
);
532-
533-
done();
534-
});
533+
})
534+
.catch(failTest)
535+
.then(done);
535536
});
536537

537538
it('Plotly.extendTraces should work', function(done) {
@@ -573,9 +574,9 @@ describe('multiple transforms:', function() {
573574
return Plotly.deleteTraces(gd, [0]);
574575
}).then(function() {
575576
assertDims([]);
576-
577-
done();
578-
});
577+
})
578+
.catch(failTest)
579+
.then(done);
579580
});
580581

581582
it('toggling trace visibility should work', function(done) {
@@ -595,12 +596,12 @@ describe('multiple transforms:', function() {
595596
return Plotly.restyle(gd, 'visible', [true, true]);
596597
}).then(function() {
597598
assertDims([2, 2, 2, 2]);
598-
599-
done();
600-
});
599+
})
600+
.catch(failTest)
601+
.then(done);
601602
});
602603

603-
it('executes filter and aggregate in the order given', function() {
604+
it('executes filter and aggregate in the order given', function(done) {
604605
// filter and aggregate do not commute!
605606

606607
var trace1 = {
@@ -624,18 +625,21 @@ describe('multiple transforms:', function() {
624625
var trace2 = Lib.extendDeep({}, trace1);
625626
trace2.transforms.reverse();
626627

627-
Plotly.newPlot(gd, [trace1, trace2]);
628+
Plotly.newPlot(gd, [trace1, trace2]).then(function() {
629+
var trace1Out = gd._fullData[0];
630+
expect(trace1Out.x).toEqual([2]);
631+
expect(trace1Out.y).toEqual([5]);
628632

629-
var trace1Out = gd._fullData[0];
630-
expect(trace1Out.x).toEqual([2]);
631-
expect(trace1Out.y).toEqual([5]);
633+
var trace2Out = gd._fullData[1];
634+
expect(trace2Out.x).toEqual([4, -5]);
635+
expect(trace2Out.y).toEqual([5, 4]);
632636

633-
var trace2Out = gd._fullData[1];
634-
expect(trace2Out.x).toEqual([4, -5]);
635-
expect(trace2Out.y).toEqual([5, 4]);
637+
})
638+
.catch(failTest)
639+
.then(done);
636640
});
637641

638-
it('always executes groupby before aggregate', function() {
642+
it('always executes groupby before aggregate', function(done) {
639643
// aggregate and groupby wouldn't commute, but groupby always happens first
640644
// because it has a `transform`, and aggregate has a `calcTransform`
641645

@@ -658,29 +662,31 @@ describe('multiple transforms:', function() {
658662
var trace2 = Lib.extendDeep({}, trace1);
659663
trace2.transforms.reverse();
660664

661-
Plotly.newPlot(gd, [trace1, trace2]);
662-
663-
var t1g1 = gd._fullData[0];
664-
var t1g2 = gd._fullData[1];
665-
var t2g1 = gd._fullData[2];
666-
var t2g2 = gd._fullData[3];
667-
668-
expect(t1g1.x).toEqual([1, 2]);
669-
expect(t1g1.y).toEqual([2, 4]);
670-
// group 2 has its aggregations switched, since group 2 comes first
671-
expect(t1g2.x).toEqual([3, 9]);
672-
expect(t1g2.y).toEqual([6, 9]);
673-
674-
// if we had done aggregation first, we'd implicitly get the first val
675-
// for each of the groupby groups, which is [1, 1]
676-
// so we'd only make 1 output trace, and it would look like:
677-
// {x: [10, 5], y: [20/3, 5]}
678-
// (and if we got some other groupby groups values, the most it could do
679-
// is break ^^ into two separate traces)
680-
expect(t2g1.x).toEqual(t1g1.x);
681-
expect(t2g1.y).toEqual(t1g1.y);
682-
expect(t2g2.x).toEqual(t1g2.x);
683-
expect(t2g2.y).toEqual(t1g2.y);
665+
Plotly.newPlot(gd, [trace1, trace2]).then(function() {
666+
var t1g1 = gd._fullData[0];
667+
var t1g2 = gd._fullData[1];
668+
var t2g1 = gd._fullData[2];
669+
var t2g2 = gd._fullData[3];
670+
671+
expect(t1g1.x).toEqual([1, 2]);
672+
expect(t1g1.y).toEqual([2, 4]);
673+
// group 2 has its aggregations switched, since group 2 comes first
674+
expect(t1g2.x).toEqual([3, 9]);
675+
expect(t1g2.y).toEqual([6, 9]);
676+
677+
// if we had done aggregation first, we'd implicitly get the first val
678+
// for each of the groupby groups, which is [1, 1]
679+
// so we'd only make 1 output trace, and it would look like:
680+
// {x: [10, 5], y: [20/3, 5]}
681+
// (and if we got some other groupby groups values, the most it could do
682+
// is break ^^ into two separate traces)
683+
expect(t2g1.x).toEqual(t1g1.x);
684+
expect(t2g1.y).toEqual(t1g1.y);
685+
expect(t2g2.x).toEqual(t1g2.x);
686+
expect(t2g2.y).toEqual(t1g2.y);
687+
})
688+
.catch(failTest)
689+
.then(done);
684690
});
685691
});
686692

@@ -699,8 +705,9 @@ describe('invalid transforms', function() {
699705
transforms: [{}]
700706
}]).then(function() {
701707
expect(gd._fullData[0].transforms.length).toEqual(1);
702-
done();
703-
});
708+
})
709+
.catch(failTest)
710+
.then(done);
704711
});
705712
});
706713

@@ -761,9 +768,9 @@ describe('multiple traces with transforms:', function() {
761768
expect(gd._fullData[2].y).toEqual([3, 5, 2]);
762769

763770
assertDims([2, 3, 3]);
764-
765-
done();
766-
});
771+
})
772+
.catch(failTest)
773+
.then(done);
767774
});
768775

769776
it('Plotly.restyle should work', function(done) {
@@ -816,9 +823,9 @@ describe('multiple traces with transforms:', function() {
816823
['rgb(0, 128, 0)', 'rgb(0, 128, 0)', 'rgb(255, 0, 0)'],
817824
[0.4, 0.6, 0.6]
818825
);
819-
820-
done();
821-
});
826+
})
827+
.catch(failTest)
828+
.then(done);
822829
});
823830

824831
it('Plotly.extendTraces should work', function(done) {
@@ -843,9 +850,9 @@ describe('multiple traces with transforms:', function() {
843850
}, [0]);
844851
}).then(function() {
845852
assertDims([5, 4, 4]);
846-
847-
done();
848-
});
853+
})
854+
.catch(failTest)
855+
.then(done);
849856
});
850857

851858
it('Plotly.deleteTraces should work', function(done) {
@@ -863,9 +870,9 @@ describe('multiple traces with transforms:', function() {
863870
return Plotly.deleteTraces(gd, [0]);
864871
}).then(function() {
865872
assertDims([]);
866-
867-
done();
868-
});
873+
})
874+
.catch(failTest)
875+
.then(done);
869876
});
870877

871878
it('toggling trace visibility should work', function(done) {
@@ -891,9 +898,10 @@ describe('multiple traces with transforms:', function() {
891898
return Plotly.restyle(gd, 'visible', 'legendonly', [0]);
892899
}).then(function() {
893900
assertDims([3, 3]);
901+
})
902+
.catch(failTest)
903+
.then(done);
894904

895-
done();
896-
});
897905
});
898906
});
899907

@@ -959,6 +967,7 @@ describe('restyle applied on transforms:', function() {
959967
expect(gd.data[0].transforms).toBeUndefined(msg);
960968
expect(gd._fullData[0].y).toEqual([2, 1, 2], msg);
961969
})
970+
.catch(failTest)
962971
.then(done);
963972
});
964973

0 commit comments

Comments
 (0)