Skip to content

Commit 8f6b489

Browse files
author
Simen Owesen-Lein
committed
test: add test for revalidateIfStale used with suspense
1 parent 9f36b40 commit 8f6b489

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/use-swr-suspense.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ describe('useSWR - suspense', () => {
163163
await screen.findByText('hello, error') // get error with cache
164164
})
165165

166+
it('should not fetch when cached data is present and `revalidateIfStale` is false', async () => {
167+
const key = createKey()
168+
mutate(key, 'cached')
169+
170+
let fetchCount = 0
171+
172+
function Section() {
173+
const { data } = useSWR(key, () => createResponse(++fetchCount), {
174+
suspense: true,
175+
revalidateIfStale: false
176+
})
177+
return <div>{data}</div>
178+
}
179+
180+
renderWithGlobalCache(
181+
<Suspense fallback={<div>fallback</div>}>
182+
<Section />
183+
</Suspense>
184+
)
185+
186+
screen.getByText('cached')
187+
await act(() => sleep(50)) // Wait to confirm fetch is not triggered
188+
expect(fetchCount).toBe(0)
189+
})
190+
166191
it('should pause when key changes', async () => {
167192
const renderedResults = []
168193
const initialKey = createKey()

0 commit comments

Comments
 (0)