File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -81,18 +81,22 @@ class ChartComponent extends React.Component {
8181 return true ;
8282 }
8383
84- const nextData = this . transformDataProp ( )
84+ const nextData = this . transformDataProp ( nextProps )
8585 return ! isEqual ( this . shadowDataProp , nextData ) ;
8686 }
8787
8888 componentWillUnmount ( ) {
8989 this . chart_instance . destroy ( ) ;
9090 }
9191
92- transformDataProp ( ) {
93- const { data } = this . props ;
94- const node = ReactDOM . findDOMNode ( this ) ;
95- return ( typeof ( data ) == "function" ) ? data ( node ) : data ;
92+ transformDataProp ( props ) {
93+ const { data } = props ;
94+ if ( typeof ( data ) == "function" ) {
95+ const node = ReactDOM . findDOMNode ( this ) ;
96+ return data ( node )
97+ } else {
98+ return data ;
99+ }
96100 }
97101
98102 // Chart.js directly mutates the data.dataset objects by adding _meta proprerty
@@ -104,7 +108,7 @@ class ChartComponent extends React.Component {
104108 return ;
105109 }
106110
107- const data = this . transformDataProp ( ) ;
111+ const data = this . transformDataProp ( this . props ) ;
108112
109113 this . shadowDataProp = {
110114 ...data ,
@@ -117,7 +121,7 @@ class ChartComponent extends React.Component {
117121 updateChart ( ) {
118122 const { options} = this . props ;
119123
120- const data = this . memoizeDataProps ( ) ;
124+ const data = this . memoizeDataProps ( this . props ) ;
121125
122126 if ( ! this . chart_instance ) return ;
123127
Original file line number Diff line number Diff line change @@ -209,4 +209,17 @@ describe('<Chart />', () => {
209209
210210 expect ( onElementsClick . called ) . to . equal ( true ) ;
211211 } ) ;
212+
213+ describe ( 'props.data function' , ( ) => {
214+ it ( 'calls data func with canvas node' , ( ) => {
215+ const resultData = { test : 1 }
216+ const dataFn = sinon . spy ( ( canvas ) => resultData ) ;
217+ const wrapper = mountComponent ( { data : dataFn } ) ;
218+
219+ const canvas = wrapper . find ( 'canvas' ) . at ( 0 ) . node ;
220+
221+ expect ( dataFn . callCount ) . to . equal ( 1 ) ;
222+ expect ( dataFn . calledWith ( canvas ) ) . to . equal ( true ) ;
223+ } ) ;
224+ } )
212225} ) ;
You can’t perform that action at this time.
0 commit comments