1- import { act , screen } from '@testing-library/react'
2- import useSWR , { mutate } from 'swr'
1+ import { act , render , screen } from '@testing-library/react'
2+ import useSWR , {
3+ mutate ,
4+ SWRConfig ,
5+ type SWRConfiguration ,
6+ useSWRConfig
7+ } from 'swr'
38import { createKey , createResponse , renderWithGlobalCache } from './utils'
9+ import { useCallback , useEffect , useState } from 'react'
410
511describe ( 'useSWR - context configs' , ( ) => {
612 it ( 'mutate before mount should not block rerender' , async ( ) => {
@@ -25,3 +31,46 @@ describe('useSWR - context configs', () => {
2531 await screen . findByText ( 'data' )
2632 } )
2733} )
34+
35+ describe ( 'useSWRConfig hook maintains stable reference across re-renders' , ( ) => {
36+ it ( 'should maintain the same swrConfig reference when counter updates' , ( ) => {
37+ const parentConfig : SWRConfiguration = {
38+ revalidateOnMount : true ,
39+ revalidateIfStale : false ,
40+ revalidateOnFocus : false ,
41+ revalidateOnReconnect : false
42+ }
43+ const counterButtonText = 'counter + 1'
44+ let useSWRConfigReferenceChangedTimes = 0
45+ function Page ( ) {
46+ return (
47+ < SWRConfig value = { parentConfig } >
48+ < ChildComponent />
49+ </ SWRConfig >
50+ )
51+ }
52+ function ChildComponent ( ) {
53+ const swrConfig = useSWRConfig ( )
54+ const [ , setCounter ] = useState ( 0 )
55+ const counterAddOne = useCallback (
56+ ( ) => setCounter ( prev => prev + 1 ) ,
57+ [ setCounter ]
58+ )
59+ useEffect ( ( ) => {
60+ useSWRConfigReferenceChangedTimes += 1
61+ } , [ swrConfig ] )
62+ return < button onClick = { counterAddOne } > { counterButtonText } </ button >
63+ }
64+ render ( < Page /> )
65+ act ( ( ) => {
66+ screen . getByText ( counterButtonText ) . click ( )
67+ } )
68+ act ( ( ) => {
69+ screen . getByText ( counterButtonText ) . click ( )
70+ } )
71+ act ( ( ) => {
72+ screen . getByText ( counterButtonText ) . click ( )
73+ } )
74+ expect ( useSWRConfigReferenceChangedTimes ) . toBe ( 1 )
75+ } )
76+ } )
0 commit comments