@@ -6,6 +6,7 @@ var Lib = require('@src/lib');
6
6
var Dates = require ( '@src/lib/dates' ) ;
7
7
8
8
var d3 = require ( 'd3' ) ;
9
+ var customMatchers = require ( '../assets/custom_matchers' ) ;
9
10
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
10
11
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
11
12
@@ -42,9 +43,11 @@ describe('annotations relayout', function() {
42
43
'use strict' ;
43
44
44
45
var mock = require ( '@mocks/annotations.json' ) ;
45
- var len = mock . layout . annotations . length ;
46
46
var gd ;
47
47
48
+ // there is 1 visible: false item
49
+ var len = mock . layout . annotations . length - 1 ;
50
+
48
51
beforeEach ( function ( done ) {
49
52
gd = createGraphDiv ( ) ;
50
53
@@ -68,16 +71,21 @@ describe('annotations relayout', function() {
68
71
Plotly . relayout ( gd , 'annotations[' + len + ']' , ann ) . then ( function ( ) {
69
72
expect ( countAnnotations ( ) ) . toEqual ( len + 1 ) ;
70
73
71
- return Plotly . relayout ( gd , 'annotations[' + 0 + ' ]', 'remove' ) ;
74
+ return Plotly . relayout ( gd , 'annotations[0 ]' , 'remove' ) ;
72
75
} )
73
76
. then ( function ( ) {
74
77
expect ( countAnnotations ( ) ) . toEqual ( len ) ;
75
78
76
- return Plotly . relayout ( gd , 'annotations[' + 0 + ' ]', null ) ;
79
+ return Plotly . relayout ( gd , 'annotations[0 ]' , null ) ;
77
80
} )
78
81
. then ( function ( ) {
79
82
expect ( countAnnotations ( ) ) . toEqual ( len - 1 ) ;
80
83
84
+ return Plotly . relayout ( gd , 'annotations[0].visible' , false ) ;
85
+ } )
86
+ . then ( function ( ) {
87
+ expect ( countAnnotations ( ) ) . toEqual ( len - 2 ) ;
88
+
81
89
return Plotly . relayout ( gd , { annotations : [ ] } ) ;
82
90
} )
83
91
. then ( function ( ) {
@@ -86,4 +94,112 @@ describe('annotations relayout', function() {
86
94
done ( ) ;
87
95
} ) ;
88
96
} ) ;
97
+
98
+ it ( 'should be able update annotations' , function ( done ) {
99
+
100
+ function assertText ( index , expected ) {
101
+ var query = '.annotation[data-index="' + index + '"]' ,
102
+ actual = d3 . select ( query ) . select ( 'text' ) . text ( ) ;
103
+
104
+ expect ( actual ) . toEqual ( expected ) ;
105
+ }
106
+
107
+ assertText ( 0 , 'left top' ) ;
108
+
109
+ Plotly . relayout ( gd , 'annotations[0].text' , 'hello' ) . then ( function ( ) {
110
+ assertText ( 0 , 'hello' ) ;
111
+
112
+ return Plotly . relayout ( gd , 'annotations[0].text' , null ) ;
113
+ } )
114
+ . then ( function ( ) {
115
+ assertText ( 0 , 'new text' ) ;
116
+ } )
117
+ . then ( done ) ;
118
+
119
+ } ) ;
120
+ } ) ;
121
+
122
+ describe ( 'annotations autosize' , function ( ) {
123
+ 'use strict' ;
124
+
125
+ var mock = Lib . extendDeep ( { } , require ( '@mocks/annotations-autorange.json' ) ) ;
126
+ var gd ;
127
+
128
+ beforeAll ( function ( ) {
129
+ jasmine . addMatchers ( customMatchers ) ;
130
+ } ) ;
131
+
132
+ afterEach ( destroyGraphDiv ) ;
133
+
134
+ it ( 'should adapt to relayout calls' , function ( done ) {
135
+ gd = createGraphDiv ( ) ;
136
+
137
+ function assertRanges ( x , y , x2 , y2 , x3 , y3 ) {
138
+ var fullLayout = gd . _fullLayout ;
139
+ var PREC = 1 ;
140
+
141
+ // xaxis2 need a bit more tolerance to pass on CI
142
+ // this most likely due to the different text bounding box values
143
+ // on headfull vs headless browsers.
144
+ var PREC2 = 0.1 ;
145
+
146
+ expect ( fullLayout . xaxis . range ) . toBeCloseToArray ( x , PREC , '- xaxis' ) ;
147
+ expect ( fullLayout . yaxis . range ) . toBeCloseToArray ( y , PREC , '- yaxis' ) ;
148
+ expect ( fullLayout . xaxis2 . range ) . toBeCloseToArray ( x2 , PREC2 , 'xaxis2' ) ;
149
+ expect ( fullLayout . yaxis2 . range ) . toBeCloseToArray ( y2 , PREC , 'yaxis2' ) ;
150
+ expect ( fullLayout . xaxis3 . range ) . toBeCloseToArray ( x3 , PREC , 'xaxis3' ) ;
151
+ expect ( fullLayout . yaxis3 . range ) . toBeCloseToArray ( y3 , PREC , 'yaxis3' ) ;
152
+ }
153
+
154
+ Plotly . plot ( gd , mock ) . then ( function ( ) {
155
+ assertRanges (
156
+ [ 0.97 , 2.03 ] , [ 0.97 , 2.03 ] ,
157
+ [ - 0.32 , 3.38 ] , [ 0.42 , 2.58 ] ,
158
+ [ 0.9 , 2.1 ] , [ 0.86 , 2.14 ]
159
+ ) ;
160
+
161
+ return Plotly . relayout ( gd , {
162
+ 'annotations[0].visible' : false ,
163
+ 'annotations[4].visible' : false ,
164
+ 'annotations[8].visible' : false
165
+ } ) ;
166
+ } )
167
+ . then ( function ( ) {
168
+ assertRanges (
169
+ [ 1.44 , 2.02 ] , [ 0.97 , 2.03 ] ,
170
+ [ 1.31 , 2.41 ] , [ 0.42 , 2.58 ] ,
171
+ [ 1.44 , 2.1 ] , [ 0.86 , 2.14 ]
172
+ ) ;
173
+
174
+ return Plotly . relayout ( gd , {
175
+ 'annotations[2].visible' : false ,
176
+ 'annotations[5].visible' : false ,
177
+ 'annotations[9].visible' : false
178
+ } ) ;
179
+ } )
180
+ . then ( function ( ) {
181
+ assertRanges (
182
+ [ 1.44 , 2.02 ] , [ 0.99 , 1.52 ] ,
183
+ [ 0.5 , 2.5 ] , [ 0.42 , 2.58 ] ,
184
+ [ 0.5 , 2.5 ] , [ 0.86 , 2.14 ]
185
+ ) ;
186
+
187
+ return Plotly . relayout ( gd , {
188
+ 'annotations[0].visible' : true ,
189
+ 'annotations[2].visible' : true ,
190
+ 'annotations[4].visible' : true ,
191
+ 'annotations[5].visible' : true ,
192
+ 'annotations[8].visible' : true ,
193
+ 'annotations[9].visible' : true
194
+ } ) ;
195
+ } )
196
+ . then ( function ( ) {
197
+ assertRanges (
198
+ [ 0.97 , 2.03 ] , [ 0.97 , 2.03 ] ,
199
+ [ - 0.32 , 3.38 ] , [ 0.42 , 2.58 ] ,
200
+ [ 0.9 , 2.1 ] , [ 0.86 , 2.14 ]
201
+ ) ;
202
+ } )
203
+ . then ( done ) ;
204
+ } ) ;
89
205
} ) ;
0 commit comments