@@ -545,17 +545,22 @@ describe('mapbox plots', function() {
545545 return layerLen ;
546546 }
547547
548+ function getLayerLength ( gd ) {
549+ return ( gd . layout . mapbox . layers || [ ] ) . length ;
550+ }
551+
548552 function assertLayerStyle ( gd , expectations , index ) {
549553 var mapInfo = getMapInfo ( gd ) ,
550554 layers = mapInfo . layers ,
551555 layerNames = mapInfo . layoutLayers ;
552556
553557 var layer = layers [ layerNames [ index ] ] ;
558+ expect ( layer ) . toBeDefined ( layerNames [ index ] ) ;
554559
555560 return new Promise ( function ( resolve ) {
556561 setTimeout ( function ( ) {
557562 Object . keys ( expectations ) . forEach ( function ( k ) {
558- expect ( layer . paint [ k ] ) . toEqual ( expectations [ k ] ) ;
563+ expect ( ( ( layer || { } ) . paint || { } ) [ k ] ) . toEqual ( expectations [ k ] ) ;
559564 } ) ;
560565 resolve ( ) ;
561566 } , TRANSITION_DELAY ) ;
@@ -565,26 +570,26 @@ describe('mapbox plots', function() {
565570 expect ( countVisibleLayers ( gd ) ) . toEqual ( 0 ) ;
566571
567572 Plotly . relayout ( gd , 'mapbox.layers[0]' , layer0 ) . then ( function ( ) {
568- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 1 ) ;
573+ expect ( getLayerLength ( gd ) ) . toEqual ( 1 ) ;
569574 expect ( countVisibleLayers ( gd ) ) . toEqual ( 1 ) ;
570575
571576 // add a new layer at the beginning
572577 return Plotly . relayout ( gd , 'mapbox.layers[1]' , layer1 ) ;
573578 } )
574579 . then ( function ( ) {
575- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
580+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
576581 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
577582
578583 return Plotly . relayout ( gd , mapUpdate ) ;
579584 } )
580585 . then ( function ( ) {
581- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
586+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
582587 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
583588
584589 return Plotly . relayout ( gd , styleUpdate0 ) ;
585590 } )
586591 . then ( function ( ) {
587- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
592+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
588593 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
589594
590595 return assertLayerStyle ( gd , {
@@ -594,13 +599,13 @@ describe('mapbox plots', function() {
594599 } , 0 ) ;
595600 } )
596601 . then ( function ( ) {
597- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
602+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
598603 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
599604
600605 return Plotly . relayout ( gd , styleUpdate1 ) ;
601606 } )
602607 . then ( function ( ) {
603- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
608+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
604609 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
605610
606611 return assertLayerStyle ( gd , {
@@ -610,20 +615,20 @@ describe('mapbox plots', function() {
610615 } , 1 ) ;
611616 } )
612617 . then ( function ( ) {
613- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 2 ) ;
618+ expect ( getLayerLength ( gd ) ) . toEqual ( 2 ) ;
614619 expect ( countVisibleLayers ( gd ) ) . toEqual ( 2 ) ;
615620
616621 // delete the first layer
617622 return Plotly . relayout ( gd , 'mapbox.layers[0]' , null ) ;
618623 } )
619624 . then ( function ( ) {
620- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 1 ) ;
625+ expect ( getLayerLength ( gd ) ) . toEqual ( 1 ) ;
621626 expect ( countVisibleLayers ( gd ) ) . toEqual ( 1 ) ;
622627
623628 return Plotly . relayout ( gd , 'mapbox.layers[0]' , null ) ;
624629 } )
625630 . then ( function ( ) {
626- expect ( gd . layout . mapbox . layers ) . toBeUndefined ( ) ;
631+ expect ( getLayerLength ( gd ) ) . toEqual ( 0 ) ;
627632 expect ( countVisibleLayers ( gd ) ) . toEqual ( 0 ) ;
628633
629634 return Plotly . relayout ( gd , 'mapbox.layers[0]' , { } ) ;
@@ -637,7 +642,7 @@ describe('mapbox plots', function() {
637642 return Plotly . relayout ( gd , 'mapbox.layers[0].source' , layer0 . source ) ;
638643 } )
639644 . then ( function ( ) {
640- expect ( gd . layout . mapbox . layers . length ) . toEqual ( 1 ) ;
645+ expect ( getLayerLength ( gd ) ) . toEqual ( 1 ) ;
641646 expect ( countVisibleLayers ( gd ) ) . toEqual ( 1 ) ;
642647 } )
643648 . catch ( failTest )
0 commit comments