-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplotschema_test.js
219 lines (179 loc) · 7.37 KB
/
plotschema_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
describe('plot schema', function() {
'use strict';
var plotSchema = Plotly.PlotSchema.get(),
valObjects = plotSchema.defs.valObjects;
var isValObject = Plotly.PlotSchema.isValObject,
isPlainObject = Lib.isPlainObject;
var VALTYPES = Object.keys(valObjects),
ROLES = ['info', 'style', 'data'];
function assertPlotSchema(callback) {
var traces = plotSchema.traces;
Object.keys(traces).forEach(function(traceName) {
Plotly.PlotSchema.crawl(traces[traceName].attributes, callback);
});
Plotly.PlotSchema.crawl(plotSchema.layout.layoutAttributes, callback);
}
it('all attributes should have a valid `valType`', function() {
assertPlotSchema(
function(attr) {
if(isValObject(attr)) {
expect(VALTYPES.indexOf(attr.valType) !== -1).toBe(true);
}
}
);
});
it('all attributes should only have valid `role`', function() {
assertPlotSchema(
function(attr) {
if(isValObject(attr)) {
expect(ROLES.indexOf(attr.role) !== -1).toBe(true, attr);
}
}
);
});
it('all nested objects should have the *object* `role`', function() {
assertPlotSchema(
function(attr, attrName) {
if(!isValObject(attr) && isPlainObject(attr) && attrName !== 'items') {
expect(attr.role === 'object').toBe(true);
}
}
);
});
it('all attributes should have the required options', function() {
assertPlotSchema(
function(attr) {
if(isValObject(attr)) {
var keys = Object.keys(attr);
valObjects[attr.valType].requiredOpts.forEach(function(opt) {
expect(keys.indexOf(opt) !== -1).toBe(true);
});
}
}
);
});
it('all attributes should only have compatible options', function() {
assertPlotSchema(
function(attr) {
if(isValObject(attr)) {
var valObject = valObjects[attr.valType],
opts = valObject.requiredOpts
.concat(valObject.otherOpts)
.concat(['valType', 'description', 'role']);
Object.keys(attr).forEach(function(key) {
expect(opts.indexOf(key) !== -1).toBe(true, key, attr);
});
}
}
);
});
it('all subplot objects should contain _isSubplotObj', function() {
var IS_SUBPLOT_OBJ = '_isSubplotObj',
astrs = ['xaxis', 'yaxis', 'scene', 'geo', 'ternary', 'mapbox'],
cnt = 0;
// check if the subplot objects have '_isSubplotObj'
astrs.forEach(function(astr) {
expect(
Lib.nestedProperty(
plotSchema.layout.layoutAttributes,
astr + '.' + IS_SUBPLOT_OBJ
).get()
).toBe(true);
});
// check that no other object has '_isSubplotObj'
assertPlotSchema(
function(attr, attrName) {
if(attr[IS_SUBPLOT_OBJ] === true) {
expect(astrs.indexOf(attrName)).not.toEqual(-1);
cnt++;
}
}
);
expect(cnt).toEqual(astrs.length);
});
it('should convert _isLinkedToArray attributes to items object', function() {
var astrs = [
'annotations', 'shapes', 'images',
'xaxis.rangeselector.buttons',
'updatemenus',
'sliders',
'mapbox.layers'
];
astrs.forEach(function(astr) {
var np = Lib.nestedProperty(
plotSchema.layout.layoutAttributes, astr
);
var name = np.parts[np.parts.length - 1],
itemName = name.substr(0, name.length - 1);
var itemsObj = np.get().items,
itemObj = itemsObj[itemName];
// N.B. the specs below must be satisfied for plotly.py
expect(isPlainObject(itemsObj)).toBe(true);
expect(itemsObj.role).toBeUndefined();
expect(Object.keys(itemsObj).length).toEqual(1);
expect(isPlainObject(itemObj)).toBe(true);
expect(itemObj.role).toBe('object');
var role = np.get().role;
expect(role).toEqual('object');
});
});
it('valObjects descriptions should be strings', function() {
assertPlotSchema(
function(attr) {
var isValid;
if(isValObject(attr)) {
// attribute don't have to have a description (for now)
isValid = (typeof attr.description === 'string') ||
(attr.description === undefined);
expect(isValid).toBe(true);
}
}
);
});
it('deprecated attributes should have a `valType` and `role`', function() {
var DEPRECATED = '_deprecated';
assertPlotSchema(
function(attr) {
if(isPlainObject(attr[DEPRECATED])) {
Object.keys(attr[DEPRECATED]).forEach(function(dAttrName) {
var dAttr = attr[DEPRECATED][dAttrName];
expect(VALTYPES.indexOf(dAttr.valType) !== -1).toBe(true);
expect(ROLES.indexOf(dAttr.role) !== -1).toBe(true);
});
}
}
);
});
it('should work with registered transforms', function() {
var valObjects = plotSchema.transforms.filter.attributes,
attrNames = Object.keys(valObjects);
['operation', 'value', 'target'].forEach(function(k) {
expect(attrNames).toContain(k);
});
});
it('should work with registered components', function() {
expect(plotSchema.traces.scatter.attributes.xcalendar.valType).toEqual('enumerated');
expect(plotSchema.traces.scatter3d.attributes.zcalendar.valType).toEqual('enumerated');
expect(plotSchema.layout.layoutAttributes.calendar.valType).toEqual('enumerated');
expect(plotSchema.layout.layoutAttributes.xaxis.calendar.valType).toEqual('enumerated');
expect(plotSchema.layout.layoutAttributes.scene.xaxis.calendar.valType).toEqual('enumerated');
expect(plotSchema.transforms.filter.attributes.valuecalendar.valType).toEqual('enumerated');
expect(plotSchema.transforms.filter.attributes.targetcalendar.valType).toEqual('enumerated');
});
it('should list correct defs', function() {
expect(plotSchema.defs.valObjects).toBeDefined();
expect(plotSchema.defs.metaKeys)
.toEqual([
'_isSubplotObj', '_isLinkedToArray', '_arrayAttrRegexps',
'_deprecated', 'description', 'role'
]);
});
it('should list the correct frame attributes', function() {
expect(plotSchema.frames).toBeDefined();
expect(plotSchema.frames.role).toEqual('object');
expect(plotSchema.frames.items.frames_entry).toBeDefined();
expect(plotSchema.frames.items.frames_entry.role).toEqual('object');
});
});