@@ -24,30 +24,114 @@ describe('Test shapes:', function() {
2424
2525 afterEach ( destroyGraphDiv ) ;
2626
27- function countShapeLayers ( ) {
28- return d3 . selectAll ( '.shapelayer' ) . size ( ) ;
27+ function countShapesInLowerLayer ( ) {
28+ return gd . _fullLayout . shapes . filter ( isShapeInLowerLayer ) . length ;
2929 }
3030
31- function countShapePaths ( ) {
32- return d3 . selectAll ( '.shapelayer > path' ) . size ( ) ;
31+ function countShapesInUpperLayer ( ) {
32+ return gd . _fullLayout . shapes . filter ( isShapeInUpperLayer ) . length ;
3333 }
3434
35- describe ( 'DOM' , function ( ) {
36- it ( 'has one *shapelayer* node' , function ( ) {
37- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
35+ function countShapesInSubplots ( ) {
36+ return gd . _fullLayout . shapes . filter ( isShapeInSubplot ) . length ;
37+ }
38+
39+ function isShapeInUpperLayer ( shape ) {
40+ return shape . layer !== 'below' ;
41+ }
42+
43+ function isShapeInLowerLayer ( shape ) {
44+ return ( shape . xref === 'paper' && shape . yref === 'paper' ) &&
45+ ! isShapeInUpperLayer ( shape ) ;
46+ }
47+
48+ function isShapeInSubplot ( shape ) {
49+ return ! isShapeInUpperLayer ( shape ) && ! isShapeInLowerLayer ( shape ) ;
50+ }
51+
52+ function countShapeLowerLayerNodes ( ) {
53+ return d3 . selectAll ( '.shapelayer-below' ) . size ( ) ;
54+ }
55+
56+ function countShapeUpperLayerNodes ( ) {
57+ return d3 . selectAll ( '.shapelayer-above' ) . size ( ) ;
58+ }
59+
60+ function countShapeLayerNodesInSubplots ( ) {
61+ return d3 . selectAll ( '.shapelayer-subplot' ) . size ( ) ;
62+ }
63+
64+ function countSubplots ( gd ) {
65+ return Object . getOwnPropertyNames ( gd . _fullLayout . _plots || { } ) . length ;
66+ }
67+
68+ function countShapePathsInLowerLayer ( ) {
69+ return d3 . selectAll ( '.shapelayer-below > path' ) . size ( ) ;
70+ }
71+
72+ function countShapePathsInUpperLayer ( ) {
73+ return d3 . selectAll ( '.shapelayer-above > path' ) . size ( ) ;
74+ }
75+
76+ function countShapePathsInSubplots ( ) {
77+ return d3 . selectAll ( '.shapelayer-subplot > path' ) . size ( ) ;
78+ }
79+
80+ describe ( '*shapeLowerLayer*' , function ( ) {
81+ it ( 'has one node' , function ( ) {
82+ expect ( countShapeLowerLayerNodes ( ) ) . toEqual ( 1 ) ;
83+ } ) ;
84+
85+ it ( 'has as many *path* nodes as shapes in the lower layer' , function ( ) {
86+ expect ( countShapePathsInLowerLayer ( ) )
87+ . toEqual ( countShapesInLowerLayer ( ) ) ;
88+ } ) ;
89+
90+ it ( 'should be able to get relayout' , function ( done ) {
91+ Plotly . relayout ( gd , { height : 200 , width : 400 } ) . then ( function ( ) {
92+ expect ( countShapeLowerLayerNodes ( ) ) . toEqual ( 1 ) ;
93+ expect ( countShapePathsInLowerLayer ( ) )
94+ . toEqual ( countShapesInLowerLayer ( ) ) ;
95+ } ) . then ( done ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( '*shapeUpperLayer*' , function ( ) {
100+ it ( 'has one node' , function ( ) {
101+ expect ( countShapeUpperLayerNodes ( ) ) . toEqual ( 1 ) ;
38102 } ) ;
39103
40- it ( 'has as many *path* nodes as there are shapes' , function ( ) {
41- expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
104+ it ( 'has as many *path* nodes as shapes in the upper layer' , function ( ) {
105+ expect ( countShapePathsInUpperLayer ( ) )
106+ . toEqual ( countShapesInUpperLayer ( ) ) ;
42107 } ) ;
43108
44109 it ( 'should be able to get relayout' , function ( done ) {
45- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
46- expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
110+ Plotly . relayout ( gd , { height : 200 , width : 400 } ) . then ( function ( ) {
111+ expect ( countShapeUpperLayerNodes ( ) ) . toEqual ( 1 ) ;
112+ expect ( countShapePathsInUpperLayer ( ) )
113+ . toEqual ( countShapesInUpperLayer ( ) ) ;
114+ } ) . then ( done ) ;
115+ } ) ;
116+ } ) ;
47117
118+ describe ( 'each *subplot*' , function ( ) {
119+ it ( 'has one *shapelayer*' , function ( ) {
120+ expect ( countShapeLayerNodesInSubplots ( ) )
121+ . toEqual ( countSubplots ( gd ) ) ;
122+ } ) ;
123+
124+ it ( 'has as many *path* nodes as shapes in the subplot' , function ( ) {
125+ expect ( countShapePathsInSubplots ( ) )
126+ . toEqual ( countShapesInSubplots ( ) ) ;
127+ } ) ;
128+
129+ it ( 'should be able to get relayout' , function ( done ) {
48130 Plotly . relayout ( gd , { height : 200 , width : 400 } ) . then ( function ( ) {
49- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
50- expect ( countShapePaths ( ) ) . toEqual ( mock . layout . shapes . length ) ;
131+ expect ( countShapeLayerNodesInSubplots ( ) )
132+ . toEqual ( countSubplots ( gd ) ) ;
133+ expect ( countShapePathsInSubplots ( ) )
134+ . toEqual ( countShapesInSubplots ( ) ) ;
51135 } ) . then ( done ) ;
52136 } ) ;
53137 } ) ;
@@ -75,33 +159,32 @@ describe('Test shapes:', function() {
75159
76160 describe ( 'Plotly.relayout' , function ( ) {
77161 it ( 'should be able to add a shape' , function ( done ) {
78- var pathCount = countShapePaths ( ) ;
162+ var pathCount = countShapePathsInUpperLayer ( ) ;
79163 var index = countShapes ( gd ) ;
80164 var shape = getRandomShape ( ) ;
81165
82- Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( ) {
83- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
84- expect ( countShapePaths ( ) ) . toEqual ( pathCount + 1 ) ;
166+ Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( )
167+ {
168+ expect ( countShapePathsInUpperLayer ( ) ) . toEqual ( pathCount + 1 ) ;
85169 expect ( getLastShape ( gd ) ) . toEqual ( shape ) ;
86170 expect ( countShapes ( gd ) ) . toEqual ( index + 1 ) ;
87171 } ) . then ( done ) ;
88172 } ) ;
89173
90174 it ( 'should be able to remove a shape' , function ( done ) {
91- var pathCount = countShapePaths ( ) ;
175+ var pathCount = countShapePathsInUpperLayer ( ) ;
92176 var index = countShapes ( gd ) ;
93177 var shape = getRandomShape ( ) ;
94178
95- Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( ) {
96- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
97- expect ( countShapePaths ( ) ) . toEqual ( pathCount + 1 ) ;
179+ Plotly . relayout ( gd , 'shapes[' + index + ']' , shape ) . then ( function ( )
180+ {
181+ expect ( countShapePathsInUpperLayer ( ) ) . toEqual ( pathCount + 1 ) ;
98182 expect ( getLastShape ( gd ) ) . toEqual ( shape ) ;
99183 expect ( countShapes ( gd ) ) . toEqual ( index + 1 ) ;
100184 } ) . then ( function ( ) {
101185 Plotly . relayout ( gd , 'shapes[' + index + ']' , 'remove' ) ;
102186 } ) . then ( function ( ) {
103- expect ( countShapeLayers ( ) ) . toEqual ( 1 ) ;
104- expect ( countShapePaths ( ) ) . toEqual ( pathCount ) ;
187+ expect ( countShapePathsInUpperLayer ( ) ) . toEqual ( pathCount ) ;
105188 expect ( countShapes ( gd ) ) . toEqual ( index ) ;
106189 } ) . then ( done ) ;
107190 } ) ;
0 commit comments