Skip to content

Commit 8d7dc46

Browse files
committed
improve transform tests
1 parent da2c24c commit 8d7dc46

File tree

1 file changed

+142
-24
lines changed

1 file changed

+142
-24
lines changed

test/jasmine/tests/transforms_test.js

Lines changed: 142 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,22 @@ describe('one-to-one transforms:', function() {
5252
}]);
5353
});
5454

55-
it('supplyDataDefaults should apply the transform', function() {
55+
it('supplyTraceDefaults should not bail if transform module is not found', function() {
56+
var traceIn = {
57+
y: [2, 1, 2],
58+
transforms: [{ type: 'invalid' }]
59+
};
60+
61+
var traceOut = Plots.supplyTraceDefaults(traceIn, 0, {});
62+
63+
expect(traceOut.y).toBe(traceIn.y);
64+
});
65+
66+
it('supplyDataDefaults should apply the transform while', function() {
5667
var dataIn = [{
68+
x: [-2, -2, 1, 2, 3],
69+
y: [1, 2, 2, 3, 1],
70+
}, {
5771
x: [-2, -1, -2, 0, 1, 2, 3],
5872
y: [1, 2, 3, 1, 2, 3, 1],
5973
transforms: [{
@@ -67,47 +81,55 @@ describe('one-to-one transforms:', function() {
6781
var dataOut = [];
6882
Plots.supplyDataDefaults(dataIn, dataOut, {}, []);
6983

70-
// does not mutate user data
71-
expect(dataIn[0].x).toEqual([-2, -1, -2, 0, 1, 2, 3]);
72-
expect(dataIn[0].y).toEqual([1, 2, 3, 1, 2, 3, 1]);
73-
expect(dataIn[0].transforms).toEqual([{
84+
var msg;
85+
86+
msg = 'does not mutate user data';
87+
expect(dataIn[1].x).toEqual([-2, -1, -2, 0, 1, 2, 3], msg);
88+
expect(dataIn[1].y).toEqual([1, 2, 3, 1, 2, 3, 1], msg);
89+
expect(dataIn[1].transforms).toEqual([{
7490
type: 'filter',
7591
operation: '>',
7692
value: '0',
7793
filtersrc: 'x'
78-
}]);
94+
}], msg);
7995

80-
// applies transform
81-
expect(dataOut[0].x).toEqual([1, 2, 3]);
82-
expect(dataOut[0].y).toEqual([2, 3, 1]);
96+
msg = 'applies transform';
97+
expect(dataOut[1].x).toEqual([1, 2, 3], msg);
98+
expect(dataOut[1].y).toEqual([2, 3, 1], msg);
8399

84-
// TODO what is the expected behavior ???
85-
// expect(dataOut[0].transforms).toEqual([]);
100+
msg = 'supplying the transform defaults';
101+
expect(dataOut[1].transforms[0]).toEqual({
102+
type: 'filter',
103+
operation: '>',
104+
value: 0,
105+
filtersrc: 'x'
106+
}, msg);
86107

87-
// keep ref to user data
88-
expect(dataOut[0]._input.x).toEqual([-2, -1, -2, 0, 1, 2, 3]);
89-
expect(dataOut[0]._input.y).toEqual([1, 2, 3, 1, 2, 3, 1]);
90-
expect(dataOut[0]._input.transforms).toEqual([{
108+
msg = 'keeping refs to user data';
109+
expect(dataOut[1]._input.x).toEqual([-2, -1, -2, 0, 1, 2, 3], msg);
110+
expect(dataOut[1]._input.y).toEqual([1, 2, 3, 1, 2, 3, 1], msg);
111+
expect(dataOut[1]._input.transforms).toEqual([{
91112
type: 'filter',
92113
operation: '>',
93114
value: '0',
94115
filtersrc: 'x'
95-
}]);
116+
}], msg);
96117

97-
// keep ref to full transforms array
98-
expect(dataOut[0]._fullInput.transforms).toEqual([{
118+
msg = 'keeping refs to full transforms array';
119+
expect(dataOut[1]._fullInput.transforms).toEqual([{
99120
type: 'filter',
100121
operation: '>',
101122
value: 0,
102123
filtersrc: 'x'
103-
}]);
124+
}], msg);
104125

105-
// set index w.r.t. fullData
106-
expect(dataOut[0].index).toEqual(0);
126+
msg = 'setting index w.r.t user data';
127+
expect(dataOut[0].index).toEqual(0, msg);
128+
expect(dataOut[1].index).toEqual(1, msg);
107129

108-
// TODO do we really need this ???
109-
// set _index w.r.t. user data
110-
expect(dataOut[0].index).toEqual(0);
130+
msg = 'setting _expandedIndex w.r.t full data';
131+
expect(dataOut[0]._expandedIndex).toEqual(0, msg);
132+
expect(dataOut[1]._expandedIndex).toEqual(1, msg);
111133
});
112134

113135
it('Plotly.plot should plot the transform trace', function(done) {
@@ -130,22 +152,34 @@ describe('one-to-one transforms:', function() {
130152
var gd = createGraphDiv();
131153
var dims = [3];
132154

155+
var uid;
156+
function assertUid(gd) {
157+
expect(gd._fullData[0].uid)
158+
.toEqual(uid + '0', 'should preserve uid on restyle');
159+
}
160+
133161
Plotly.plot(gd, data).then(function() {
162+
uid = gd.data[0].uid;
163+
134164
expect(gd._fullData[0].marker.color).toEqual('red');
165+
assertUid(gd);
135166
assertStyle(dims, ['rgb(255, 0, 0)'], [1]);
136167

137168
return Plotly.restyle(gd, 'marker.color', 'blue');
138169
}).then(function() {
139170
expect(gd._fullData[0].marker.color).toEqual('blue');
171+
assertUid(gd);
140172
assertStyle(dims, ['rgb(0, 0, 255)'], [1]);
141173

142174
return Plotly.restyle(gd, 'marker.color', 'red');
143175
}).then(function() {
144176
expect(gd._fullData[0].marker.color).toEqual('red');
177+
assertUid(gd);
145178
assertStyle(dims, ['rgb(255, 0, 0)'], [1]);
146179

147180
return Plotly.restyle(gd, 'transforms[0].value', 2.5);
148181
}).then(function() {
182+
assertUid(gd);
149183
assertStyle([1], ['rgb(255, 0, 0)'], [1]);
150184

151185
done();
@@ -251,6 +285,34 @@ describe('one-to-many transforms:', function() {
251285

252286
afterEach(destroyGraphDiv);
253287

288+
it('supplyDataDefaults should apply the transform while', function() {
289+
var dummyTrace0 = {
290+
x: [-2, -2, 1, 2, 3],
291+
y: [1, 2, 2, 3, 1],
292+
};
293+
294+
var dummyTrace1 = {
295+
x: [-1, 2, 3],
296+
y: [2, 3, 1],
297+
};
298+
299+
var dataIn = [
300+
dummyTrace0,
301+
Lib.extendDeep({}, mockData0[0]),
302+
dummyTrace1,
303+
Lib.extendDeep({}, mockData1[0])
304+
];
305+
306+
var dataOut = [];
307+
Plots.supplyDataDefaults(dataIn, dataOut, {}, []);
308+
309+
expect(dataOut.map(function(trace) { return trace.index; }))
310+
.toEqual([0, 1, 1, 2, 3, 3], 'setting index w.r.t user data');
311+
312+
expect(dataOut.map(function(trace) { return trace._expandedIndex; }))
313+
.toEqual([0, 1, 2, 3, 4, 5], 'setting index w.r.t full data');
314+
});
315+
254316
it('Plotly.plot should plot the transform traces', function(done) {
255317
var data = Lib.extendDeep([], mockData0);
256318

@@ -428,6 +490,34 @@ describe('multiple transforms:', function() {
428490

429491
afterEach(destroyGraphDiv);
430492

493+
it('supplyDataDefaults should apply the transform while', function() {
494+
var dummyTrace0 = {
495+
x: [-2, -2, 1, 2, 3],
496+
y: [1, 2, 2, 3, 1],
497+
};
498+
499+
var dummyTrace1 = {
500+
x: [-1, 2, 3],
501+
y: [2, 3, 1],
502+
};
503+
504+
var dataIn = [
505+
dummyTrace0,
506+
Lib.extendDeep({}, mockData0[0]),
507+
Lib.extendDeep({}, mockData1[0]),
508+
dummyTrace1
509+
];
510+
511+
var dataOut = [];
512+
Plots.supplyDataDefaults(dataIn, dataOut, {}, []);
513+
514+
expect(dataOut.map(function(trace) { return trace.index; }))
515+
.toEqual([0, 1, 1, 2, 2, 3], 'setting index w.r.t user data');
516+
517+
expect(dataOut.map(function(trace) { return trace._expandedIndex; }))
518+
.toEqual([0, 1, 2, 3, 4, 5], 'setting index w.r.t full data');
519+
});
520+
431521
it('Plotly.plot should plot the transform traces', function(done) {
432522
var data = Lib.extendDeep([], mockData0);
433523

@@ -625,6 +715,34 @@ describe('multiple traces with transforms:', function() {
625715

626716
afterEach(destroyGraphDiv);
627717

718+
it('supplyDataDefaults should apply the transform while', function() {
719+
var dummyTrace0 = {
720+
x: [-2, -2, 1, 2, 3],
721+
y: [1, 2, 2, 3, 1],
722+
};
723+
724+
var dummyTrace1 = {
725+
x: [-1, 2, 3],
726+
y: [2, 3, 1],
727+
};
728+
729+
var dataIn = [
730+
dummyTrace0,
731+
Lib.extendDeep({}, mockData0[0]),
732+
Lib.extendDeep({}, mockData0[1]),
733+
dummyTrace1
734+
];
735+
736+
var dataOut = [];
737+
Plots.supplyDataDefaults(dataIn, dataOut, {}, []);
738+
739+
expect(dataOut.map(function(trace) { return trace.index; }))
740+
.toEqual([0, 1, 2, 2, 3], 'setting index w.r.t user data');
741+
742+
expect(dataOut.map(function(trace) { return trace._expandedIndex; }))
743+
.toEqual([0, 1, 2, 3, 4], 'setting index w.r.t full data');
744+
});
745+
628746
it('Plotly.plot should plot the transform traces', function(done) {
629747
var data = Lib.extendDeep([], mockData0);
630748

0 commit comments

Comments
 (0)