Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad6f702

Browse files
committed
add test + comments
1 parent 762e3d5 commit ad6f702

File tree

2 files changed

+167
-11
lines changed

2 files changed

+167
-11
lines changed

src/traces/scattermapbox/convert.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,27 @@ function makeLineGeoJSON(_, coords) {
156156
}
157157

158158
// N.B. `hash` is mutated here
159+
//
160+
// The `hash` object contains mapping between values
161+
// (e.g. calculated marker.size and marker.color items)
162+
// and their index in the input arrayOk attributes.
163+
//
164+
// GeoJSON features have their 'data-driven' properties set to
165+
// the index of the first value found in the data.
166+
//
167+
// The `hash` object is then converted to mapbox `stops` arrays
168+
// mapping index to value.
169+
//
170+
// The solution prove to be more robust than trying to generate
171+
// `stops` arrays from scale functions.
159172
function makeCircleGeoJSON(calcTrace, hash) {
160173
var trace = calcTrace[0].trace;
161174

162175
var marker = trace.marker,
163176
hasColorArray = Array.isArray(marker.color),
164177
hasSizeArray = Array.isArray(marker.size);
165178

166-
// translate vals in trace arrayOk containers
179+
// Translate vals in trace arrayOk containers
167180
// into a val-to-index hash object
168181
function translate(props, key, val, index) {
169182
if(!hash[key][val]) hash[key][val] = index;

test/jasmine/tests/scattermapbox_test.js

Lines changed: 153 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,84 @@ var customMatchers = require('../assets/custom_matchers');
1313
describe('scattermapbox defaults', function() {
1414
'use strict';
1515

16+
function _supply(traceIn) {
17+
var traceOut = { visible: true },
18+
defaultColor = '#444',
19+
layout = { _dataLength: 1 };
20+
21+
ScatterMapbox.supplyDefaults(traceIn, traceOut, defaultColor, layout);
22+
23+
return traceOut;
24+
}
25+
26+
it('should truncate \'lon\' if longer than \'lat\'', function() {
27+
var fullTrace = _supply({
28+
lon: [1, 2, 3],
29+
lat: [2, 3]
30+
});
31+
32+
expect(fullTrace.lon).toEqual([1, 2]);
33+
expect(fullTrace.lat).toEqual([2, 3]);
34+
});
35+
36+
it('should truncate \'lat\' if longer than \'lon\'', function() {
37+
var fullTrace = _supply({
38+
lon: [1, 2, 3],
39+
lat: [2, 3, 3, 5]
40+
});
41+
42+
expect(fullTrace.lon).toEqual([1, 2, 3]);
43+
expect(fullTrace.lat).toEqual([2, 3, 3]);
44+
});
45+
46+
it('should set \'visible\' to false if \'lat\' and/or \'lon\' has zero length', function() {
47+
var fullTrace = _supply({
48+
lon: [1, 2, 3],
49+
lat: []
50+
});
51+
52+
expect(fullTrace.visible).toEqual(false);
53+
54+
fullTrace = _supply({
55+
lon: null,
56+
lat: [1, 2, 3]
57+
});
58+
59+
expect(fullTrace.visible).toEqual(false);
60+
});
61+
62+
it('should set \'marker.color\' and \'marker.size\' to first item if symbol is set to \'circle\'', function() {
63+
var base = {
64+
mode: 'markers',
65+
lon: [1, 2, 3],
66+
lat: [2, 3, 3],
67+
marker: {
68+
color: ['red', 'green', 'blue'],
69+
size: [10, 20, 30]
70+
}
71+
};
72+
73+
var fullTrace = _supply(Lib.extendDeep({}, base, {
74+
marker: { symbol: 'monument' }
75+
}));
76+
77+
expect(fullTrace.marker.color).toEqual('red');
78+
expect(fullTrace.marker.size).toEqual(10);
79+
80+
fullTrace = _supply(Lib.extendDeep({}, base, {
81+
marker: { symbol: ['monument', 'music', 'harbor'] }
82+
}));
83+
84+
expect(fullTrace.marker.color).toEqual('red');
85+
expect(fullTrace.marker.size).toEqual(10);
86+
87+
fullTrace = _supply(Lib.extendDeep({}, base, {
88+
marker: { symbol: 'circle' }
89+
}));
90+
91+
expect(fullTrace.marker.color).toEqual(['red', 'green', 'blue']);
92+
expect(fullTrace.marker.size).toEqual([10, 20, 30]);
93+
});
1694
});
1795

1896
describe('scattermapbox calc', function() {
@@ -187,10 +265,28 @@ describe('scattermapbox convert', function() {
187265

188266
expect(opts.circle.paint['circle-color']).toEqual({
189267
property: 'circle-color',
190-
stops: [[
191-
192-
]]
268+
stops: [
269+
[0, 'rgb(220, 220, 220)'], [1, '#444'], [2, 'rgb(178, 10, 28)']
270+
]
271+
}, 'have correct circle-color stops');
272+
273+
expect(opts.circle.paint['circle-radius']).toEqual({
274+
property: 'circle-radius',
275+
stops: [ [0, 5], [1, 10], [2, 0] ]
276+
}, 'have correct circle-radius stops');
277+
278+
var circleProps = opts.circle.geojson.features.map(function(f) {
279+
return f.properties;
193280
});
281+
282+
// N.B repeated values have same geojson props
283+
expect(circleProps).toEqual([
284+
{ 'circle-color': 0, 'circle-radius': 0 },
285+
{ 'circle-color': 1, 'circle-radius': 1 },
286+
{ 'circle-color': 2, 'circle-radius': 2 },
287+
{ 'circle-color': 1, 'circle-radius': 2 },
288+
{ 'circle-color': 1, 'circle-radius': 2 }
289+
], 'have correct geojson feature properties');
194290
});
195291

196292
it('fill + markers + lines traces, should', function() {
@@ -211,7 +307,13 @@ describe('scattermapbox convert', function() {
211307
expect(opts.fill.geojson.coordinates).toEqual(lineCoords, 'have correct fill coords');
212308
expect(opts.line.geojson.coordinates).toEqual(lineCoords, 'have correct line coords');
213309

310+
var circleCoords = opts.circle.geojson.features.map(function(f) {
311+
return f.geometry.coordinates;
312+
});
214313

314+
expect(circleCoords).toEqual([
315+
[10, 20], [20, 20], [30, 10], [20, 10], [10, 20]
316+
], 'have correct circle coords');
215317
});
216318

217319
it('for markers + non-circle traces, should', function() {
@@ -221,12 +323,23 @@ describe('scattermapbox convert', function() {
221323
}));
222324

223325
assertVisibility(opts, ['none', 'none', 'none', 'visible']);
326+
327+
var symbolProps = opts.symbol.geojson.features.map(function(f) {
328+
return [f.properties.symbol, f.properties.text];
329+
});
330+
331+
var expected = opts.symbol.geojson.features.map(function() {
332+
return ['monument', ''];
333+
});
334+
335+
expect(symbolProps).toEqual(expected, 'have correct geojson properties');
224336
});
225337

226338
it('for text + lines traces, should', function() {
227339
var opts = _convert(Lib.extendFlat({}, base, {
228340
mode: 'lines+text',
229-
connectgaps: true
341+
connectgaps: true,
342+
text: ['A', 'B', 'C', 'D', 'E', 'F']
230343
}));
231344

232345
assertVisibility(opts, ['none', 'visible', 'none', 'visible']);
@@ -236,6 +349,42 @@ describe('scattermapbox convert', function() {
236349
]];
237350

238351
expect(opts.line.geojson.coordinates).toEqual(lineCoords, 'have correct line coords');
352+
353+
var actualText = opts.symbol.geojson.features.map(function(f) {
354+
return f.properties.text;
355+
});
356+
357+
expect(actualText).toEqual(['A', 'B', 'C', 'F', '']);
358+
});
359+
360+
it('should correctly convert \'textposition\' to \'text-anchor\' and \'text-offset\'', function() {
361+
var specs = {
362+
'top left': ['top-right', [-1.5, -2.5]],
363+
'top center': ['top', [0, -2.5]],
364+
'top right': ['top-left', [1.5, -2.5]],
365+
'middle left': ['right', [-1.5, 0]],
366+
'middle center': ['center', [0, 0]],
367+
'middle right': ['left', [1.5, 0]],
368+
'bottom left': ['bottom-right', [-1.5, 2.5]],
369+
'bottom center': ['bottom', [0, 2.5]],
370+
'bottom right': ['bottom-left', [1.5, 2.5]]
371+
};
372+
373+
Object.keys(specs).forEach(function(k) {
374+
var spec = specs[k];
375+
376+
var opts = _convert(Lib.extendFlat({}, base, {
377+
textposition: k,
378+
mode: 'text+markers',
379+
marker: { size: 15 },
380+
text: ['A', 'B', 'C']
381+
}));
382+
383+
expect([
384+
opts.symbol.layout['text-anchor'],
385+
opts.symbol.layout['text-offset']
386+
]).toEqual(spec, '(case ' + k + ')');
387+
});
239388
});
240389

241390
function assertVisibility(opts, expectations) {
@@ -249,12 +398,6 @@ describe('scattermapbox convert', function() {
249398
}
250399
});
251400

252-
describe('scattermapbox plot', function() {
253-
'use strict';
254-
255-
256-
});
257-
258401
describe('scattermapbox hover', function() {
259402
'use strict';
260403

0 commit comments

Comments
 (0)