Skip to content

Commit

Permalink
test: add a test for the behavior of revalidateOnMount when the key h…
Browse files Browse the repository at this point in the history
…as been changed (#1847)
  • Loading branch information
koba04 committed Feb 9, 2022
1 parent f98da66 commit ef400ea
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ describe('useSWR', () => {
expect(fetch).not.toHaveBeenCalled()
})

it('should call fetch function when revalidateOnMount is false and key has been changed', async () => {
const fetch = jest.fn(() => 'SWR')

function Page() {
const [key, setKey] = useState(createKey())
const { data } = useSWR(key, fetch, {
revalidateOnMount: false
})
return <div onClick={() => setKey(createKey)}>hello,{data}</div>
}

renderWithConfig(<Page />)

await screen.findByText('hello,')
expect(fetch).not.toHaveBeenCalled()

// the key has been changed
fireEvent.click(screen.getByText('hello,'))

await screen.findByText('hello,SWR')
})

it('should call fetch function when revalidateOnMount is true even if fallbackData is set', async () => {
const fetch = jest.fn(() => 'SWR')

Expand Down

0 comments on commit ef400ea

Please sign in to comment.