@@ -2,12 +2,12 @@ var d3 = require('d3');
2
2
3
3
var Plotly = require ( '@lib/index' ) ;
4
4
var interactConstants = require ( '@src/constants/interactions' ) ;
5
+ var Lib = require ( '@src/lib' ) ;
5
6
6
7
var createGraphDiv = require ( '../assets/create_graph_div' ) ;
7
8
var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
8
9
var mouseEvent = require ( '../assets/mouse_event' ) ;
9
10
10
-
11
11
describe ( 'Plot title' , function ( ) {
12
12
'use strict' ;
13
13
@@ -28,39 +28,164 @@ describe('Plot title', function() {
28
28
it ( 'is centered horizontally and vertically above the plot by default' , function ( ) {
29
29
Plotly . plot ( gd , data , layout ) ;
30
30
31
- expectDefaultCenteredPosition ( ) ;
31
+ expectDefaultCenteredPosition ( gd ) ;
32
32
} ) ;
33
33
34
34
it ( 'can still be defined as `layout.title` to ensure backwards-compatibility' , function ( ) {
35
35
Plotly . plot ( gd , data , { title : 'Plotly line chart' } ) ;
36
36
37
- expect ( titleSel ( ) . text ( ) ) . toBe ( 'Plotly line chart' ) ;
38
- expectDefaultCenteredPosition ( ) ;
37
+ expectTitle ( 'Plotly line chart' ) ;
38
+ expectDefaultCenteredPosition ( gd ) ;
39
39
} ) ;
40
40
41
- function expectDefaultCenteredPosition ( ) {
42
- var containerBB = gd . getBoundingClientRect ( ) ;
41
+ it ( 'can be updated via `relayout`' , function ( done ) {
42
+ Plotly . plot ( gd , data , { title : 'Plotly line chart' } )
43
+ . then ( expectTitleFn ( 'Plotly line chart' ) )
44
+ . then ( function ( ) {
45
+ return Plotly . relayout ( gd , { title : { text : 'Some other title' } } ) ;
46
+ } )
47
+ . then ( expectTitleFn ( 'Some other title' ) )
48
+ . catch ( fail )
49
+ . then ( done ) ;
50
+ } ) ;
43
51
44
- expect ( titleX ( ) ) . toBe ( containerBB . width / 2 ) ;
45
- expect ( titleY ( ) ) . toBe ( gd . _fullLayout . margin . t / 2 ) ;
46
- }
52
+ it ( 'preserves alignment when text is updated via `Plotly.relayout` using an attribute string' , function ( ) {
53
+ // TODO implement once alignment is implemented
54
+ } ) ;
47
55
48
- function titleX ( ) {
49
- return Number . parseFloat ( titleSel ( ) . attr ( 'x' ) ) ;
50
- }
56
+ it ( 'preserves alignment when text is updated via `Plotly.update` using an attribute string' , function ( ) {
57
+ // TODO implement once alignment is implemented
58
+ } ) ;
59
+
60
+ it ( 'discards alignment when text is updated via `Plotly.relayout` by passing a new title object' , function ( ) {
61
+ // TODO implement once alignment is implemented
62
+ } ) ;
63
+
64
+ it ( 'discards alignment when text is updated via `Plotly.update` by passing a new title object' , function ( ) {
65
+ // TODO implement once alignment is implemented
66
+ } ) ;
67
+ } ) ;
68
+
69
+ describe ( 'Titles can be updated' , function ( ) {
70
+ 'use strict' ;
71
+
72
+ var data = [ { x : [ 1 , 2 , 3 ] , y : [ 1 , 2 , 3 ] } ] ;
73
+ var NEW_TITLE = 'Weight over years' ;
74
+ var NEW_XTITLE = 'Age in years' ;
75
+ var NEW_YTITLE = 'Average weight' ;
76
+ var gd ;
77
+
78
+ beforeEach ( function ( ) {
79
+ var layout = {
80
+ title : { text : 'Plotly line chart' } ,
81
+ xaxis : { title : { text : 'Age' } } ,
82
+ yaxis : { title : { text : 'Weight' } }
83
+ } ;
84
+ gd = createGraphDiv ( ) ;
85
+ Plotly . plot ( gd , data , Lib . extendDeep ( { } , layout ) ) ;
86
+
87
+ expectTitles ( 'Plotly line chart' , 'Age' , 'Weight' ) ;
88
+ } ) ;
51
89
52
- function titleY ( ) {
53
- return Number . parseFloat ( titleSel ( ) . attr ( 'y' ) ) ;
90
+ afterEach ( destroyGraphDiv ) ;
91
+
92
+ [
93
+ {
94
+ desc : 'by replacing the entire title objects' ,
95
+ update : {
96
+ title : { text : NEW_TITLE } ,
97
+ xaxis : { title : { text : NEW_XTITLE } } ,
98
+ yaxis : { title : { text : NEW_YTITLE } }
99
+ }
100
+ } ,
101
+ {
102
+ desc : 'by using attribute strings' ,
103
+ update : {
104
+ 'title.text' : NEW_TITLE ,
105
+ 'xaxis.title.text' : NEW_XTITLE ,
106
+ 'yaxis.title.text' : NEW_YTITLE
107
+ }
108
+ } ,
109
+ {
110
+ desc : 'despite passing title only as a string (backwards-compatibility)' ,
111
+ update : {
112
+ title : NEW_TITLE ,
113
+ xaxis : { title : NEW_XTITLE } ,
114
+ yaxis : { title : NEW_YTITLE }
115
+ }
116
+ } ,
117
+ {
118
+ desc : 'despite passing title only as a string using string attributes ' +
119
+ '(backwards-compatibility)' ,
120
+ update : {
121
+ 'title' : NEW_TITLE ,
122
+ 'xaxis.title' : NEW_XTITLE ,
123
+ 'yaxis.title' : NEW_YTITLE
124
+ }
125
+ }
126
+ ] . forEach ( function ( testCase ) {
127
+ it ( 'via `Plotly.relayout` ' + testCase . desc , function ( ) {
128
+ Plotly . relayout ( gd , testCase . update ) ;
129
+
130
+ expectChangedTitles ( ) ;
131
+ } ) ;
132
+
133
+ it ( 'via `Plotly.update` ' + testCase . desc , function ( ) {
134
+ Plotly . update ( gd , { } , testCase . update ) ;
135
+
136
+ expectChangedTitles ( ) ;
137
+ } ) ;
138
+ } ) ;
139
+
140
+ function expectChangedTitles ( ) {
141
+ expectTitles ( NEW_TITLE , NEW_XTITLE , NEW_YTITLE ) ;
54
142
}
55
143
56
- function titleSel ( ) {
57
- var titleSel = d3 . select ( '.infolayer .g-gtitle .gtitle' ) ;
58
- expect ( titleSel . empty ( ) ) . toBe ( false , 'Title element missing' ) ;
59
- return titleSel ;
144
+ function expectTitles ( expTitle , expXTitle , expYTitle ) {
145
+ expectTitle ( expTitle ) ;
146
+
147
+ var xTitleSel = d3 . select ( '.xtitle' ) ;
148
+ expect ( xTitleSel . empty ( ) ) . toBe ( false , 'X-axis title element missing' ) ;
149
+ expect ( xTitleSel . text ( ) ) . toBe ( expXTitle ) ;
150
+
151
+ var yTitleSel = d3 . select ( '.ytitle' ) ;
152
+ expect ( yTitleSel . empty ( ) ) . toBe ( false , 'Y-axis title element missing' ) ;
153
+ expect ( yTitleSel . text ( ) ) . toBe ( expYTitle ) ;
60
154
}
61
155
} ) ;
62
156
63
- describe ( 'editable titles' , function ( ) {
157
+ function expectTitle ( expTitle ) {
158
+ expectTitleFn ( expTitle ) ( ) ;
159
+ }
160
+
161
+ function expectTitleFn ( expTitle ) {
162
+ return function ( ) {
163
+ expect ( titleSel ( ) . text ( ) ) . toBe ( expTitle ) ;
164
+ } ;
165
+ }
166
+
167
+ function expectDefaultCenteredPosition ( gd ) {
168
+ var containerBB = gd . getBoundingClientRect ( ) ;
169
+
170
+ expect ( titleX ( ) ) . toBe ( containerBB . width / 2 ) ;
171
+ expect ( titleY ( ) ) . toBe ( gd . _fullLayout . margin . t / 2 ) ;
172
+ }
173
+
174
+ function titleX ( ) {
175
+ return Number . parseFloat ( titleSel ( ) . attr ( 'x' ) ) ;
176
+ }
177
+
178
+ function titleY ( ) {
179
+ return Number . parseFloat ( titleSel ( ) . attr ( 'y' ) ) ;
180
+ }
181
+
182
+ function titleSel ( ) {
183
+ var titleSel = d3 . select ( '.infolayer .g-gtitle .gtitle' ) ;
184
+ expect ( titleSel . empty ( ) ) . toBe ( false , 'Title element missing' ) ;
185
+ return titleSel ;
186
+ }
187
+
188
+ describe ( 'Editable titles' , function ( ) {
64
189
'use strict' ;
65
190
66
191
var data = [ { x : [ 1 , 2 , 3 ] , y : [ 1 , 2 , 3 ] } ] ;
0 commit comments