Skip to content

Commit d0953a0

Browse files
committed
test: add cases for array container defaults
1 parent 138ac35 commit d0953a0

File tree

4 files changed

+146
-14
lines changed

4 files changed

+146
-14
lines changed

test/jasmine/tests/annotations_test.js

+59-11
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,79 @@ var customMatchers = require('../assets/custom_matchers');
1010
var createGraphDiv = require('../assets/create_graph_div');
1111
var destroyGraphDiv = require('../assets/destroy_graph_div');
1212

13+
1314
describe('Test annotations', function() {
1415
'use strict';
1516

1617
describe('supplyLayoutDefaults', function() {
18+
19+
function _supply(layoutIn, layoutOut) {
20+
layoutOut = layoutOut || {};
21+
layoutOut._has = Plots._hasPlotType.bind(layoutOut);
22+
23+
Annotations.supplyLayoutDefaults(layoutIn, layoutOut);
24+
25+
return layoutOut.annotations;
26+
}
27+
28+
it('should skip non-array containers', function() {
29+
[null, undefined, {}, 'str', 0, false, true].forEach(function(cont) {
30+
var msg = '- ' + JSON.stringify(cont);
31+
var layoutIn = { annotations: cont };
32+
var out = _supply(layoutIn);
33+
34+
expect(layoutIn.annotations).toBe(cont, msg);
35+
expect(out).toEqual([], msg);
36+
});
37+
});
38+
39+
it('should make non-object item visible: false', function() {
40+
var annotations = [null, undefined, [], 'str', 0, false, true];
41+
var layoutIn = { annotations: annotations };
42+
var out = _supply(layoutIn);
43+
44+
expect(layoutIn.annotations).toEqual(annotations);
45+
46+
out.forEach(function(item, i) {
47+
expect(item).toEqual({
48+
visible: false,
49+
_input: {},
50+
_index: i
51+
});
52+
});
53+
});
54+
1755
it('should default to pixel for axref/ayref', function() {
18-
var annotationDefaults = {};
19-
annotationDefaults._has = Plots._hasPlotType.bind(annotationDefaults);
56+
var layoutIn = {
57+
annotations: [{ showarrow: true, arrowhead: 2}]
58+
};
2059

21-
Annotations.supplyLayoutDefaults({ annotations: [{ showarrow: true, arrowhead: 2}] }, annotationDefaults);
60+
var out = _supply(layoutIn);
2261

23-
expect(annotationDefaults.annotations[0].axref).toEqual('pixel');
24-
expect(annotationDefaults.annotations[0].ayref).toEqual('pixel');
62+
expect(out[0].axref).toEqual('pixel');
63+
expect(out[0].ayref).toEqual('pixel');
2564
});
2665

2766
it('should convert ax/ay date coordinates to milliseconds if tail is in axis terms and axis is a date', function() {
28-
var annotationOut = { xaxis: { type: 'date', range: ['2000-01-01', '2016-01-01'] }};
29-
annotationOut._has = Plots._hasPlotType.bind(annotationOut);
67+
var layoutIn = {
68+
annotations: [{
69+
showarrow: true,
70+
axref: 'x',
71+
ayref: 'y',
72+
x: '2008-07-01',
73+
ax: '2004-07-01',
74+
y: 0,
75+
ay: 50
76+
}]
77+
};
3078

31-
var annotationIn = {
32-
annotations: [{ showarrow: true, axref: 'x', ayref: 'y', x: '2008-07-01', ax: '2004-07-01', y: 0, ay: 50}]
79+
var layoutOut = {
80+
xaxis: { type: 'date', range: ['2000-01-01', '2016-01-01'] }
3381
};
3482

35-
Annotations.supplyLayoutDefaults(annotationIn, annotationOut);
83+
_supply(layoutIn, layoutOut);
3684

37-
expect(annotationIn.annotations[0].ax).toEqual(Dates.dateTime2ms('2004-07-01'));
85+
expect(layoutIn.annotations[0].ax).toEqual(Dates.dateTime2ms('2004-07-01'));
3886
});
3987
});
4088
});

test/jasmine/tests/layout_images_test.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ describe('Layout images', function() {
2727

2828
Images.supplyLayoutDefaults(layoutIn, layoutOut);
2929

30-
expect(layoutOut.images).toEqual([{ visible: false }]);
30+
expect(layoutOut.images).toEqual([{
31+
visible: false,
32+
_index: 0,
33+
_input: layoutIn.images[0]
34+
}]);
3135
});
3236

3337
it('should reject when not an array', function() {
@@ -44,7 +48,9 @@ describe('Layout images', function() {
4448
});
4549

4650
it('should coerce the correct defaults', function() {
47-
layoutIn.images[0] = { source: jsLogo };
51+
var image = { source: jsLogo };
52+
53+
layoutIn.images[0] = image;
4854

4955
var expected = {
5056
source: jsLogo,
@@ -59,7 +65,9 @@ describe('Layout images', function() {
5965
sizing: 'contain',
6066
opacity: 1,
6167
xref: 'paper',
62-
yref: 'paper'
68+
yref: 'paper',
69+
_input: image,
70+
_index: 0
6371
};
6472

6573
Images.supplyLayoutDefaults(layoutIn, layoutOut);

test/jasmine/tests/shapes_test.js

+44
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
var Shapes = require('@src/components/shapes');
12
var helpers = require('@src/components/shapes/helpers');
23
var constants = require('@src/components/shapes/constants');
34

45
var Plotly = require('@lib/index');
56
var PlotlyInternal = require('@src/plotly');
67
var Lib = require('@src/lib');
8+
9+
var Plots = PlotlyInternal.Plots;
710
var Axes = PlotlyInternal.Axes;
811

912
var d3 = require('d3');
@@ -12,6 +15,47 @@ var createGraphDiv = require('../assets/create_graph_div');
1215
var destroyGraphDiv = require('../assets/destroy_graph_div');
1316

1417

18+
describe('Test shapes defaults:', function() {
19+
'use strict';
20+
21+
function _supply(layoutIn, layoutOut) {
22+
layoutOut = layoutOut || {};
23+
layoutOut._has = Plots._hasPlotType.bind(layoutOut);
24+
25+
Shapes.supplyLayoutDefaults(layoutIn, layoutOut);
26+
27+
return layoutOut.shapes;
28+
}
29+
30+
it('should skip non-array containers', function() {
31+
[null, undefined, {}, 'str', 0, false, true].forEach(function(cont) {
32+
var msg = '- ' + JSON.stringify(cont);
33+
var layoutIn = { shapes: cont };
34+
var out = _supply(layoutIn);
35+
36+
expect(layoutIn.shapes).toBe(cont, msg);
37+
expect(out).toEqual([], msg);
38+
});
39+
});
40+
41+
it('should make non-object item visible: false', function() {
42+
var shapes = [null, undefined, [], 'str', 0, false, true];
43+
var layoutIn = { shapes: shapes };
44+
var out = _supply(layoutIn);
45+
46+
expect(layoutIn.shapes).toEqual(shapes);
47+
48+
out.forEach(function(item, i) {
49+
expect(item).toEqual({
50+
visible: false,
51+
_input: {},
52+
_index: i
53+
});
54+
});
55+
});
56+
57+
});
58+
1559
describe('Test shapes:', function() {
1660
'use strict';
1761

test/jasmine/tests/updatemenus_test.js

+32
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@ describe('update menus defaults', function() {
2121
layoutOut = {};
2222
});
2323

24+
it('should skip non-array containers', function() {
25+
[null, undefined, {}, 'str', 0, false, true].forEach(function(cont) {
26+
var msg = '- ' + JSON.stringify(cont);
27+
28+
layoutIn = { updatemenus: cont };
29+
layoutOut = {};
30+
supply(layoutIn, layoutOut);
31+
32+
expect(layoutIn.updatemenus).toBe(cont, msg);
33+
expect(layoutOut.updatemenus).toEqual([], msg);
34+
});
35+
});
36+
37+
it('should make non-object item visible: false', function() {
38+
var updatemenus = [null, undefined, [], 'str', 0, false, true];
39+
40+
layoutIn = { updatemenus: updatemenus };
41+
layoutOut = {};
42+
supply(layoutIn, layoutOut);
43+
44+
expect(layoutIn.updatemenus).toEqual(updatemenus);
45+
46+
layoutOut.updatemenus.forEach(function(item, i) {
47+
expect(item).toEqual({
48+
visible: false,
49+
buttons: [],
50+
_input: {},
51+
_index: i
52+
});
53+
});
54+
});
55+
2456
it('should set \'visible\' to false when no buttons are present', function() {
2557
layoutIn.updatemenus = [{
2658
buttons: [{

0 commit comments

Comments
 (0)