@@ -60,88 +60,99 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
60
60
} ;
61
61
62
62
function assertLabelContent ( label , expectation , msg ) {
63
- var lines = label . selectAll ( 'tspan' ) ;
64
- var lineCnt = lines . size ( ) ;
65
- var expectMultiLine = Array . isArray ( expectation ) ;
63
+ if ( ! expectation ) expectation = '' ;
66
64
67
- function extract ( sel ) {
68
- return sel . node ( ) ? sel . html ( ) : null ;
69
- }
65
+ var lines = label . selectAll ( 'tspan.line' ) ;
66
+ var content = [ ] ;
70
67
71
- if ( lineCnt > 0 ) {
72
- if ( expectMultiLine ) {
73
- expect ( lines . size ( ) ) . toBe ( expectation . length , msg + ': # of lines' ) ;
74
- lines . each ( function ( _ , i ) {
75
- var l = d3 . select ( this ) ;
76
- expect ( extract ( l ) ) . toBe ( expectation [ i ] , msg + ': tspan line ' + i ) ;
77
- } ) ;
78
- } else {
79
- fail ( 'Expected a single-line label, found multiple lines' ) ;
68
+ function fill ( sel ) {
69
+ if ( sel . node ( ) ) {
70
+ var html = sel . html ( ) ;
71
+ if ( html ) content . push ( html ) ;
80
72
}
73
+ }
74
+
75
+ if ( lines . size ( ) ) {
76
+ lines . each ( function ( ) { fill ( d3 . select ( this ) ) ; } ) ;
81
77
} else {
82
- if ( ! expectMultiLine ) {
83
- expect ( extract ( label ) ) . toBe ( expectation , msg + ': text content' ) ;
84
- } else {
85
- fail ( 'Expected a multi-line label, found single' ) ;
86
- }
78
+ fill ( label ) ;
87
79
}
80
+
81
+ expect ( content . join ( '\n' ) ) . toBe ( expectation , msg + ': text content' ) ;
88
82
}
89
83
90
84
function count ( selector ) {
91
85
return d3 . selectAll ( selector ) . size ( ) ;
92
86
}
93
87
88
+ /**
89
+ * @param {object } expectation
90
+ * - nums {string || array of strings}
91
+ * - name {string || array of strings}
92
+ * - axis {string}
93
+ * @param {string } msg
94
+ */
94
95
exports . assertHoverLabelContent = function ( expectation , msg ) {
95
96
if ( ! msg ) msg = '' ;
96
97
97
98
var ptSelector = 'g.hovertext' ;
98
- var ptExpectation = expectation [ 0 ] ;
99
99
var ptMsg = msg + ' point hover label' ;
100
100
var ptCnt = count ( ptSelector ) ;
101
101
102
102
var axSelector = 'g.axistext' ;
103
- var axExpectation = expectation [ 1 ] ;
104
103
var axMsg = 'common axis hover label' ;
105
104
var axCnt = count ( axSelector ) ;
106
105
107
106
if ( ptCnt === 1 ) {
108
107
assertLabelContent (
109
108
d3 . select ( ptSelector + '> text.nums' ) ,
110
- ptExpectation [ 0 ] ,
109
+ expectation . nums ,
111
110
ptMsg + ' (nums)'
112
111
) ;
113
112
assertLabelContent (
114
113
d3 . select ( ptSelector + '> text.name' ) ,
115
- ptExpectation [ 1 ] ,
114
+ expectation . name ,
116
115
ptMsg + ' (name)'
117
116
) ;
118
117
} else if ( ptCnt > 1 ) {
119
- expect ( ptCnt ) . toBe ( ptExpectation . length , ptMsg + ' # of visible nodes' ) ;
118
+ if ( ! Array . isArray ( expectation . nums ) || ! Array . isArray ( expectation . name ) ) {
119
+ fail ( ptMsg + ': expecting more than 1 labels.' ) ;
120
+ }
121
+
122
+ expect ( ptCnt )
123
+ . toBe ( expectation . name . length , ptMsg + ' # of visible labels' ) ;
120
124
121
125
d3 . selectAll ( ptSelector ) . each ( function ( _ , i ) {
122
126
assertLabelContent (
123
127
d3 . select ( this ) . select ( 'text.nums' ) ,
124
- ptExpectation [ i ] [ 0 ] ,
128
+ expectation . nums [ i ] ,
125
129
ptMsg + ' (nums ' + i + ')'
126
130
) ;
127
131
assertLabelContent (
128
132
d3 . select ( this ) . select ( 'text.name' ) ,
129
- ptExpectation [ i ] [ 1 ] ,
133
+ expectation . name [ i ] ,
130
134
ptMsg + ' (name ' + i + ')'
131
135
) ;
132
136
} ) ;
133
137
} else {
134
- expect ( ptExpectation ) . toBe ( null , ptMsg + ' should not be displayed' ) ;
138
+ if ( expectation . nums ) {
139
+ fail ( ptMsg + ': expecting *nums* labels, did not find any.' ) ;
140
+ }
141
+ if ( expectation . name ) {
142
+ fail ( ptMsg + ': expecting *nums* labels, did not find any.' ) ;
143
+ }
135
144
}
136
145
137
- if ( axCnt > 0 ) {
146
+ if ( axCnt ) {
138
147
assertLabelContent (
139
148
d3 . select ( axSelector + '> text' ) ,
140
- axExpectation ,
149
+ expectation . axis ,
141
150
axMsg
142
151
) ;
143
152
} else {
144
- expect ( axExpectation ) . toBe ( null , axMsg + ' should not be displayed' ) ;
153
+ if ( expectation . axis ) {
154
+ fail ( axMsg + ': expecting label, did not find any.' ) ;
155
+ }
145
156
}
146
157
} ;
147
158
0 commit comments