@@ -7,6 +7,7 @@ var Bar = require('@src/traces/bar');
7
7
var Legend = require ( '@src/components/legend' ) ;
8
8
var pkg = require ( '../../../package.json' ) ;
9
9
var subroutines = require ( '@src/plot_api/subroutines' ) ;
10
+ var helpers = require ( '@src/plot_api/helpers' ) ;
10
11
11
12
var d3 = require ( 'd3' ) ;
12
13
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
@@ -1309,3 +1310,52 @@ describe('Test plot api', function() {
1309
1310
} ) ;
1310
1311
} ) ;
1311
1312
} ) ;
1313
+
1314
+ describe ( 'plot_api helpers' , function ( ) {
1315
+ describe ( 'hasParent' , function ( ) {
1316
+ var attr = 'annotations[2].xref' ;
1317
+ var attr2 = 'marker.line.width' ;
1318
+
1319
+ it ( 'does not match the attribute itself or other related non-parent attributes' , function ( ) {
1320
+ var aobj = {
1321
+ // '' wouldn't be valid as an attribute in our framework, but tested
1322
+ // just in case this would count as a parent.
1323
+ '' : true ,
1324
+ 'annotations[1]' : { } , // parent structure, just a different array element
1325
+ 'xref' : 1 , // another substring
1326
+ 'annotations[2].x' : 0.5 , // substring of the attribute, but not a parent
1327
+ 'annotations[2].xref' : 'x2' // the attribute we're testing - not its own parent
1328
+ } ;
1329
+
1330
+ expect ( helpers . hasParent ( aobj , attr ) ) . toBe ( false ) ;
1331
+
1332
+ var aobj2 = {
1333
+ 'marker.line.color' : 'red' ,
1334
+ 'marker.line.width' : 2 ,
1335
+ 'marker.color' : 'blue' ,
1336
+ 'line' : { }
1337
+ } ;
1338
+
1339
+ expect ( helpers . hasParent ( aobj2 , attr2 ) ) . toBe ( false ) ;
1340
+ } ) ;
1341
+
1342
+ it ( 'is false when called on a top-level attribute' , function ( ) {
1343
+ var aobj = {
1344
+ '' : true ,
1345
+ 'width' : 100
1346
+ } ;
1347
+
1348
+ expect ( helpers . hasParent ( aobj , 'width' ) ) . toBe ( false ) ;
1349
+ } ) ;
1350
+
1351
+ it ( 'matches any kind of parent' , function ( ) {
1352
+ expect ( helpers . hasParent ( { 'annotations' : [ ] } , attr ) ) . toBe ( true ) ;
1353
+ expect ( helpers . hasParent ( { 'annotations[2]' : { } } , attr ) ) . toBe ( true ) ;
1354
+
1355
+ expect ( helpers . hasParent ( { 'marker' : { } } , attr2 ) ) . toBe ( true ) ;
1356
+ // this one wouldn't actually make sense: marker.line needs to be an object...
1357
+ // but hasParent doesn't look at the values in aobj, just its keys.
1358
+ expect ( helpers . hasParent ( { 'marker.line' : 1 } , attr2 ) ) . toBe ( true ) ;
1359
+ } ) ;
1360
+ } ) ;
1361
+ } ) ;
0 commit comments