import { act , screen } from '@testing-library/react' ; import React , { useState } from 'react' ; import useSWR from 'swr' ; import { createKey , renderWithConfig , nextTick } from './utils' ; describe ( 'useSWR - fetcher' , ( ) => { it ( 'should use the latest fetcher reference' , async ( ) => { const key = createKey ( ) ; let fetcher = ( ) => 'foo' ; let mutate ; let rerender ; function Page ( ) { const { data , mutate : boundMutate } = useSWR ( key , fetcher ) ; rerender = useState ( { } ) [ 1 ] ; mutate = boundMutate ; return < div > data : { data } < /div>; } renderWithConfig ( < Page />); await nextTick ( ) ; screen . getByText ( 'data:foo' ) ; fetcher = ( ) => 'bar' ; act ( ( ) => rerender ( { } ) ) ; await act ( ( ) => mutate ( ) ) ; await screen . findByText ( 'data:bar' ) ; } ) ; } ) ;