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 {
81
81
return true ;
82
82
}
83
83
84
- const nextData = this . transformDataProp ( )
84
+ const nextData = this . transformDataProp ( nextProps )
85
85
return ! isEqual ( this . shadowDataProp , nextData ) ;
86
86
}
87
87
88
88
componentWillUnmount ( ) {
89
89
this . chart_instance . destroy ( ) ;
90
90
}
91
91
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
+ }
96
100
}
97
101
98
102
// Chart.js directly mutates the data.dataset objects by adding _meta proprerty
@@ -104,7 +108,7 @@ class ChartComponent extends React.Component {
104
108
return ;
105
109
}
106
110
107
- const data = this . transformDataProp ( ) ;
111
+ const data = this . transformDataProp ( this . props ) ;
108
112
109
113
this . shadowDataProp = {
110
114
...data ,
@@ -117,7 +121,7 @@ class ChartComponent extends React.Component {
117
121
updateChart ( ) {
118
122
const { options} = this . props ;
119
123
120
- const data = this . memoizeDataProps ( ) ;
124
+ const data = this . memoizeDataProps ( this . props ) ;
121
125
122
126
if ( ! this . chart_instance ) return ;
123
127
Original file line number Diff line number Diff line change @@ -209,4 +209,17 @@ describe('<Chart />', () => {
209
209
210
210
expect ( onElementsClick . called ) . to . equal ( true ) ;
211
211
} ) ;
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
+ } )
212
225
} ) ;
You can’t perform that action at this time.
0 commit comments