@@ -374,4 +374,64 @@ describe('useSWR - refresh', () => {
374374 expect ( fetcherWithToken ) . toBeCalledTimes ( 5 )
375375 expect ( fetcherWithToken ) . toHaveBeenLastCalledWith ( `1-${ key } ` )
376376 } )
377+
378+ it ( 'should allow using function as an interval' , async ( ) => {
379+ let count = 0
380+
381+ const key = createKey ( )
382+ function Page ( ) {
383+ const { data } = useSWR ( key , ( ) => count ++ , {
384+ refreshInterval : ( ) => 200 ,
385+ dedupingInterval : 100
386+ } )
387+ return < div > count: { data } </ div >
388+ }
389+
390+ renderWithConfig ( < Page /> )
391+
392+ // hydration
393+ screen . getByText ( 'count:' )
394+
395+ // mount
396+ await screen . findByText ( 'count: 0' )
397+
398+ await act ( ( ) => advanceTimers ( 200 ) ) // update
399+ screen . getByText ( 'count: 1' )
400+ await act ( ( ) => advanceTimers ( 50 ) ) // no update
401+ screen . getByText ( 'count: 1' )
402+ await act ( ( ) => advanceTimers ( 150 ) ) // update
403+ screen . getByText ( 'count: 2' )
404+ } )
405+
406+ it ( 'should pass updated data to refreshInterval' , async ( ) => {
407+ let count = 1
408+
409+ const key = createKey ( )
410+ function Page ( ) {
411+ const { data } = useSWR ( key , ( ) => count ++ , {
412+ refreshInterval : updatedCount => updatedCount * 1000 ,
413+ dedupingInterval : 100
414+ } )
415+ return < div > count: { data } </ div >
416+ }
417+
418+ renderWithConfig ( < Page /> )
419+
420+ // hydration
421+ screen . getByText ( 'count:' )
422+
423+ // mount
424+ await screen . findByText ( 'count: 1' )
425+
426+ await act ( ( ) => advanceTimers ( 1000 ) ) // updated after 1s
427+ screen . getByText ( 'count: 2' )
428+ await act ( ( ) => advanceTimers ( 1000 ) ) // no update
429+ screen . getByText ( 'count: 2' )
430+ await act ( ( ) => advanceTimers ( 1000 ) ) // updated after 2s
431+ screen . getByText ( 'count: 3' )
432+ await act ( ( ) => advanceTimers ( 2000 ) ) // no update
433+ screen . getByText ( 'count: 3' )
434+ await act ( ( ) => advanceTimers ( 1000 ) ) // updated after 3s
435+ screen . getByText ( 'count: 4' )
436+ } )
377437} )
0 commit comments