@@ -60,88 +60,99 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
6060} ;
6161
6262function 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 = '' ;
6664
67- function extract ( sel ) {
68- return sel . node ( ) ? sel . html ( ) : null ;
69- }
65+ var lines = label . selectAll ( 'tspan.line' ) ;
66+ var content = [ ] ;
7067
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 ) ;
8072 }
73+ }
74+
75+ if ( lines . size ( ) ) {
76+ lines . each ( function ( ) { fill ( d3 . select ( this ) ) ; } ) ;
8177 } 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 ) ;
8779 }
80+
81+ expect ( content . join ( '\n' ) ) . toBe ( expectation , msg + ': text content' ) ;
8882}
8983
9084function count ( selector ) {
9185 return d3 . selectAll ( selector ) . size ( ) ;
9286}
9387
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+ */
9495exports . assertHoverLabelContent = function ( expectation , msg ) {
9596 if ( ! msg ) msg = '' ;
9697
9798 var ptSelector = 'g.hovertext' ;
98- var ptExpectation = expectation [ 0 ] ;
9999 var ptMsg = msg + ' point hover label' ;
100100 var ptCnt = count ( ptSelector ) ;
101101
102102 var axSelector = 'g.axistext' ;
103- var axExpectation = expectation [ 1 ] ;
104103 var axMsg = 'common axis hover label' ;
105104 var axCnt = count ( axSelector ) ;
106105
107106 if ( ptCnt === 1 ) {
108107 assertLabelContent (
109108 d3 . select ( ptSelector + '> text.nums' ) ,
110- ptExpectation [ 0 ] ,
109+ expectation . nums ,
111110 ptMsg + ' (nums)'
112111 ) ;
113112 assertLabelContent (
114113 d3 . select ( ptSelector + '> text.name' ) ,
115- ptExpectation [ 1 ] ,
114+ expectation . name ,
116115 ptMsg + ' (name)'
117116 ) ;
118117 } 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' ) ;
120124
121125 d3 . selectAll ( ptSelector ) . each ( function ( _ , i ) {
122126 assertLabelContent (
123127 d3 . select ( this ) . select ( 'text.nums' ) ,
124- ptExpectation [ i ] [ 0 ] ,
128+ expectation . nums [ i ] ,
125129 ptMsg + ' (nums ' + i + ')'
126130 ) ;
127131 assertLabelContent (
128132 d3 . select ( this ) . select ( 'text.name' ) ,
129- ptExpectation [ i ] [ 1 ] ,
133+ expectation . name [ i ] ,
130134 ptMsg + ' (name ' + i + ')'
131135 ) ;
132136 } ) ;
133137 } 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+ }
135144 }
136145
137- if ( axCnt > 0 ) {
146+ if ( axCnt ) {
138147 assertLabelContent (
139148 d3 . select ( axSelector + '> text' ) ,
140- axExpectation ,
149+ expectation . axis ,
141150 axMsg
142151 ) ;
143152 } 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+ }
145156 }
146157} ;
147158
0 commit comments