Skip to content

Commit

Permalink
test: add test for revalidateIfStale used with suspense
Browse files Browse the repository at this point in the history
  • Loading branch information
simowe committed Feb 17, 2022
1 parent 9f36b40 commit 8f6b489
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/use-swr-suspense.test.tsx
Expand Up @@ -163,6 +163,31 @@ describe('useSWR - suspense', () => {
await screen.findByText('hello, error') // get error with cache
})

it('should not fetch when cached data is present and `revalidateIfStale` is false', async () => {
const key = createKey()
mutate(key, 'cached')

let fetchCount = 0

function Section() {
const { data } = useSWR(key, () => createResponse(++fetchCount), {
suspense: true,
revalidateIfStale: false
})
return <div>{data}</div>
}

renderWithGlobalCache(
<Suspense fallback={<div>fallback</div>}>
<Section />
</Suspense>
)

screen.getByText('cached')
await act(() => sleep(50)) // Wait to confirm fetch is not triggered
expect(fetchCount).toBe(0)
})

it('should pause when key changes', async () => {
const renderedResults = []
const initialKey = createKey()
Expand Down

0 comments on commit 8f6b489

Please sign in to comment.