@@ -126,10 +126,10 @@ describe('element', () => {
126
126
const Div = getElementComponent ( 'div' ) ;
127
127
128
128
const TestComponent = ( ) => {
129
- const testRef = React . useRef ( null ) ;
129
+ const testRef = React . useRef < HTMLDivElement > ( null ) ;
130
130
131
131
useEffect ( ( ) => {
132
- testRef . current . classList . add ( 'some-class-from-ref' ) ;
132
+ testRef . current ! . classList . add ( 'some-class-from-ref' ) ;
133
133
} ) ;
134
134
135
135
return < Div ref = { testRef } data-some-element-test /> ;
@@ -167,35 +167,43 @@ describe('element', () => {
167
167
) . toBe ( true ) ;
168
168
} ) ;
169
169
170
+ it ( 'given functional ref, when rendered, when rerendered with a different prop changed, does not call again' , ( ) => {
171
+ const Div = getElementComponent ( 'div' ) ;
172
+ const someRef = jest . fn ( ) ;
173
+
174
+ const rendered = render ( < Div ref = { someRef } data-some-element-test /> ) ;
175
+
176
+ someRef . mockClear ( ) ;
177
+ rendered . rerender ( < Div ref = { someRef } /> ) ;
178
+
179
+ expect ( someRef ) . not . toHaveBeenCalled ( ) ;
180
+ } ) ;
181
+
170
182
describe ( 'given multiple plugins with ref' , ( ) => {
171
183
let rendered : RenderResult ;
172
184
let discover : Discover ;
173
185
174
186
beforeEach ( ( ) => {
175
- const somePlugin1 = getPlugin < { $somePlugin1Input ?: boolean } > (
176
- ( props : any ) : any => {
177
- const ref = useRef ( null ) ;
178
-
179
- useEffect ( ( ) => {
180
- ref . current . classList . add ( 'some-class-from-hook-ref' ) ;
181
- } , [ ] ) ;
182
-
183
- return {
184
- $somePlugin1Input : undefined ,
185
- ref,
186
- } ;
187
- } ,
188
- ) ;
187
+ const somePlugin1 = getPlugin < { $somePlugin1Input ?: boolean } > ( ( ) => {
188
+ const ref = useRef < HTMLElement > ( null ) ;
189
189
190
- const somePlugin2 = getPlugin < { $somePlugin2Input ?: boolean } > (
191
- ( props ) : any => ( {
192
- $somePlugin2Input : undefined ,
190
+ useEffect ( ( ) => {
191
+ ref . current ! . classList . add ( 'some-class-from-hook-ref' ) ;
192
+ } , [ ] ) ;
193
193
194
- ref : node => {
195
- node ?. classList . add ( 'some-class-from-functional-ref' ) ;
196
- } ,
197
- } ) ,
198
- ) ;
194
+ return {
195
+ $somePlugin1Input : undefined ,
196
+ ref,
197
+ } ;
198
+ } ) ;
199
+
200
+ const somePlugin2 = getPlugin < { $somePlugin2Input ?: boolean } > ( ( ) => ( {
201
+ $somePlugin2Input : undefined ,
202
+
203
+ ref : node => {
204
+ node ?. classList . add ( 'some-class-from-functional-ref' ) ;
205
+ } ,
206
+ } ) ) ;
199
207
200
208
const Div = getElementComponent ( 'div' , somePlugin2 , somePlugin1 ) ;
201
209
0 commit comments