@@ -20,6 +20,11 @@ describe('The legend', function() {
2020 return gd . _fullLayout . _topdefs . selectAll ( '#legend' + uid ) . size ( ) ;
2121 }
2222
23+ function getPlotHeight ( gd ) {
24+ return gd . _fullLayout . height - gd . _fullLayout . margin . t - gd . _fullLayout . margin . b ;
25+ }
26+
27+
2328 describe ( 'when plotted with many traces' , function ( ) {
2429 beforeEach ( function ( ) {
2530 gd = createGraph ( ) ;
@@ -30,10 +35,9 @@ describe('The legend', function() {
3035 afterEach ( destroyGraph ) ;
3136
3237 it ( 'should not exceed plot height' , function ( ) {
33- var legendHeight = getBBox ( legend ) . height ,
34- plotHeight = gd . _fullLayout . height - gd . _fullLayout . margin . t - gd . _fullLayout . margin . b ;
38+ var legendHeight = getBBox ( legend ) . height ;
3539
36- expect ( + legendHeight ) . toBe ( plotHeight ) ;
40+ expect ( + legendHeight ) . toBe ( getPlotHeight ( gd ) ) ;
3741 } ) ;
3842
3943 it ( 'should insert a scrollbar' , function ( ) {
@@ -88,10 +92,29 @@ describe('The legend', function() {
8892 done ( ) ;
8993 } ) ;
9094 } ) ;
95+
96+ it ( 'should resize when relayout\'ed with new height' , function ( done ) {
97+ var origLegendHeight = getBBox ( legend ) . height ;
98+
99+ Plotly . relayout ( gd , { 'height' : gd . _fullLayout . height / 2 } ) . then ( function ( ) {
100+ var legendHeight = getBBox ( legend ) . height ;
101+
102+ //legend still exists and not duplicated
103+ expect ( countLegendGroups ( gd ) ) . toBe ( 1 ) ;
104+ expect ( countLegendClipPaths ( gd ) ) . toBe ( 1 ) ;
105+
106+ // clippath resized to new height less than new plot height
107+ expect ( + legendHeight ) . toBe ( getPlotHeight ( gd ) ) ;
108+ expect ( + legendHeight ) . toBeLessThan ( + origLegendHeight ) ;
109+
110+ done ( ) ;
111+ } ) ;
112+ } ) ;
91113 } ) ;
92114
115+
93116 describe ( 'when plotted with few traces' , function ( ) {
94- var gd ;
117+ var gd , legend ;
95118
96119 beforeEach ( function ( ) {
97120 gd = createGraph ( ) ;
@@ -122,6 +145,21 @@ describe('The legend', function() {
122145 done ( ) ;
123146 } ) ;
124147 } ) ;
148+
149+ it ( 'should resize when traces added' , function ( done ) {
150+ legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
151+ var origLegendHeight = getBBox ( legend ) . height ;
152+
153+ Plotly . addTrace ( gd , { x : [ 1 , 2 , 3 ] , y : [ 4 , 3 , 2 ] , name : 'Test2' } ) . then ( function ( ) {
154+ var legend = document . getElementsByClassName ( 'legend' ) [ 0 ] ;
155+ var legendHeight = getBBox ( legend ) . height ;
156+ // clippath resized to show new trace
157+ expect ( + legendHeight ) . toBeCloseTo ( + origLegendHeight + 18 , 0 ) ;
158+
159+ done ( ) ;
160+ } ) ;
161+
162+ } ) ;
125163 } ) ;
126164} ) ;
127165
0 commit comments