@@ -28,7 +28,8 @@ describe('ModeBar', function() {
2828 _context : {
2929 displaylogo : true ,
3030 displayModeBar : true ,
31- modeBarButtonsToRemove : [ ]
31+ modeBarButtonsToRemove : [ ] ,
32+ modeBarButtonsToAdd : [ ]
3233 }
3334 } ;
3435 }
@@ -144,10 +145,9 @@ describe('ModeBar', function() {
144145 function getButtons ( list ) {
145146 for ( var i = 0 ; i < list . length ; i ++ ) {
146147 for ( var j = 0 ; j < list [ i ] . length ; j ++ ) {
147- list [ i ] [ j ] = {
148- name : list [ i ] [ j ] ,
149- click : noop
150- } ;
148+
149+ // minimal button config object
150+ list [ i ] [ j ] = { name : list [ i ] [ j ] , click : noop } ;
151151 }
152152 }
153153 return list ;
@@ -277,6 +277,13 @@ describe('ModeBar', function() {
277277 expect ( function ( ) { manageModeBar ( gd ) ; } ) . toThrowError ( ) ;
278278 } ) ;
279279
280+ it ( 'throws an error if modeBarButtonsToAdd isn\'t an array' , function ( ) {
281+ var gd = getMockGraphInfo ( ) ;
282+ gd . _context . modeBarButtonsToAdd = 'not gonna work' ;
283+
284+ expect ( function ( ) { manageModeBar ( gd ) ; } ) . toThrowError ( ) ;
285+ } ) ;
286+
280287 it ( 'displays or not mode bar according to displayModeBar config arg' , function ( ) {
281288 var gd = getMockGraphInfo ( ) ;
282289 manageModeBar ( gd ) ;
@@ -297,31 +304,108 @@ describe('ModeBar', function() {
297304 expect ( countLogo ( gd . _fullLayout . _modeBar ) ) . toEqual ( 0 ) ;
298305 } ) ;
299306
300- it ( 'updates mode bar buttons if plot type changes' , function ( ) {
307+ // gives 11 buttons in 5 groups by default
308+ function setupGraphInfo ( ) {
301309 var gd = getMockGraphInfo ( ) ;
302310 gd . _fullLayout . _hasCartesian = true ;
303311 gd . _fullLayout . xaxis = { fixedrange : false } ;
312+ return gd ;
313+ }
314+
315+ it ( 'updates mode bar buttons if plot type changes' , function ( ) {
316+ var gd = setupGraphInfo ( ) ;
317+ manageModeBar ( gd ) ;
304318
305- manageModeBar ( gd ) ; // gives 11 buttons
306319 gd . _fullLayout . _hasCartesian = false ;
307320 gd . _fullLayout . _hasGL3D = true ;
308321 manageModeBar ( gd ) ;
309322
310323 expect ( countButtons ( gd . _fullLayout . _modeBar ) ) . toEqual ( 10 ) ;
311324 } ) ;
312325
313- it ( 'updates mode bar buttons if plot type changes' , function ( ) {
314- var gd = getMockGraphInfo ( ) ;
315- gd . _fullLayout . _hasCartesian = true ;
316- gd . _fullLayout . xaxis = { fixedrange : false } ;
326+ it ( 'updates mode bar buttons if modeBarButtonsToRemove changes' , function ( ) {
327+ var gd = setupGraphInfo ( ) ;
328+ manageModeBar ( gd ) ;
317329
318- manageModeBar ( gd ) ; // gives 11 buttons
319330 gd . _context . modeBarButtonsToRemove = [ 'toImage' , 'sendDataToCloud' ] ;
320331 manageModeBar ( gd ) ;
321332
322333 expect ( countButtons ( gd . _fullLayout . _modeBar ) ) . toEqual ( 9 ) ;
323334 } ) ;
324335
336+ it ( 'updates mode bar buttons if modeBarButtonsToAdd changes' , function ( ) {
337+ var gd = setupGraphInfo ( ) ;
338+ manageModeBar ( gd ) ;
339+
340+ gd . _context . modeBarButtonsToAdd = [ {
341+ name : 'some button' ,
342+ click : noop
343+ } ] ;
344+ manageModeBar ( gd ) ;
345+
346+ expect ( countGroups ( gd . _fullLayout . _modeBar ) ) . toEqual ( 6 ) ;
347+ expect ( countButtons ( gd . _fullLayout . _modeBar ) ) . toEqual ( 12 ) ;
348+ } ) ;
349+
350+ it ( 'sets up buttons with modeBarButtonsToAdd and modeBarButtonToRemove' , function ( ) {
351+ var gd = setupGraphInfo ( ) ;
352+ gd . _context . modeBarButtonsToRemove = [
353+ 'toImage' , 'pan2d' , 'hoverCompareCartesian'
354+ ] ;
355+ gd . _context . modeBarButtonsToAdd = [
356+ { name : 'some button' , click : noop } ,
357+ { name : 'some other button' , click : noop }
358+ ] ;
359+
360+ manageModeBar ( gd ) ;
361+
362+ var modeBar = gd . _fullLayout . _modeBar ;
363+ expect ( countGroups ( modeBar ) ) . toEqual ( 6 ) ;
364+ expect ( countButtons ( modeBar ) ) . toEqual ( 10 ) ;
365+ } ) ;
366+
367+ it ( 'sets up buttons with fully custom modeBarButtons' , function ( ) {
368+ var gd = setupGraphInfo ( ) ;
369+ gd . _context . modeBarButtons = [ [
370+ { name : 'some button' , click : noop } ,
371+ { name : 'some other button' , click : noop }
372+ ] , [
373+ { name : 'some button in another group' , click : noop } ,
374+ { name : 'some other button in another group' , click : noop }
375+ ] ] ;
376+
377+ manageModeBar ( gd ) ;
378+
379+ var modeBar = gd . _fullLayout . _modeBar ;
380+ expect ( countGroups ( modeBar ) ) . toEqual ( 3 ) ;
381+ expect ( countButtons ( modeBar ) ) . toEqual ( 5 ) ;
382+ } ) ;
383+
384+ it ( 'sets up buttons with custom modeBarButtons + default name' , function ( ) {
385+ var gd = setupGraphInfo ( ) ;
386+ gd . _context . modeBarButtons = [ [
387+ { name : 'some button' , click : noop } ,
388+ { name : 'some other button' , click : noop }
389+ ] , [
390+ 'toImage' , 'pan2d' , 'hoverCompareCartesian'
391+ ] ] ;
392+
393+ manageModeBar ( gd ) ;
394+
395+ var modeBar = gd . _fullLayout . _modeBar ;
396+ expect ( countGroups ( modeBar ) ) . toEqual ( 3 ) ;
397+ expect ( countButtons ( modeBar ) ) . toEqual ( 6 ) ;
398+ } ) ;
399+
400+ it ( 'throw error when modeBarButtons contains invalid name' , function ( ) {
401+ var gd = setupGraphInfo ( ) ;
402+ gd . _context . modeBarButtons = [ [
403+ 'toImage' , 'pan2d' , 'no gonna work'
404+ ] ] ;
405+
406+ expect ( function ( ) { manageModeBar ( gd ) ; } ) . toThrowError ( ) ;
407+ } ) ;
408+
325409 } ) ;
326410
327411} ) ;
0 commit comments