@@ -59,6 +59,92 @@ exports.assertHoverLabelStyle = function(g, expectation, msg, textSelector) {
5959 expect ( textStyle . fill ) . toBe ( expectation . fontColor , msg + ': font.color' ) ;
6060} ;
6161
62+ function assertLabelContent ( label , expectation , msg ) {
63+ var lines = label . selectAll ( 'tspan' ) ;
64+ var lineCnt = lines . size ( ) ;
65+ var expectMultiLine = Array . isArray ( expectation ) ;
66+
67+ function extract ( sel ) {
68+ return sel . node ( ) ? sel . html ( ) : null ;
69+ }
70+
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' ) ;
80+ }
81+ } 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+ }
87+ }
88+ }
89+
90+ function count ( selector ) {
91+ return d3 . selectAll ( selector ) . size ( ) ;
92+ }
93+
94+ exports . assertHoverLabelContent = function ( expectation , msg ) {
95+ if ( ! msg ) msg = '' ;
96+
97+ var ptSelector = 'g.hovertext' ;
98+ var ptExpectation = expectation [ 0 ] ;
99+ var ptMsg = msg + ' point hover label' ;
100+ var ptCnt = count ( ptSelector ) ;
101+
102+ var axSelector = 'g.axistext' ;
103+ var axExpectation = expectation [ 1 ] ;
104+ var axMsg = 'common axis hover label' ;
105+ var axCnt = count ( axSelector ) ;
106+
107+ if ( ptCnt === 1 ) {
108+ assertLabelContent (
109+ d3 . select ( ptSelector + '> text.nums' ) ,
110+ ptExpectation [ 0 ] ,
111+ ptMsg + ' (nums)'
112+ ) ;
113+ assertLabelContent (
114+ d3 . select ( ptSelector + '> text.name' ) ,
115+ ptExpectation [ 1 ] ,
116+ ptMsg + ' (name)'
117+ ) ;
118+ } else if ( ptCnt > 1 ) {
119+ expect ( ptCnt ) . toBe ( ptExpectation . length , ptMsg + ' # of visible nodes' ) ;
120+
121+ d3 . selectAll ( ptSelector ) . each ( function ( _ , i ) {
122+ assertLabelContent (
123+ d3 . select ( this ) . select ( 'text.nums' ) ,
124+ ptExpectation [ i ] [ 0 ] ,
125+ ptMsg + ' (nums ' + i + ')'
126+ ) ;
127+ assertLabelContent (
128+ d3 . select ( this ) . select ( 'text.name' ) ,
129+ ptExpectation [ i ] [ 1 ] ,
130+ ptMsg + ' (name ' + i + ')'
131+ ) ;
132+ } ) ;
133+ } else {
134+ expect ( ptExpectation ) . toBe ( null , ptMsg + ' should not be displayed' ) ;
135+ }
136+
137+ if ( axCnt > 0 ) {
138+ assertLabelContent (
139+ d3 . select ( axSelector + '> text' ) ,
140+ axExpectation ,
141+ axMsg
142+ ) ;
143+ } else {
144+ expect ( axExpectation ) . toBe ( null , axMsg + ' should not be displayed' ) ;
145+ }
146+ } ;
147+
62148exports . assertClip = function ( sel , isClipped , size , msg ) {
63149 expect ( sel . size ( ) ) . toBe ( size , msg + ' clip path (selection size)' ) ;
64150
0 commit comments