Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions test/use-swr-preload.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, screen } from '@testing-library/react'
import React, { Suspense } from 'react'
import { act, fireEvent, screen } from '@testing-library/react'
import React, { Suspense, useEffect, useState } from 'react'
import useSWR, { preload, useSWRConfig } from 'swr'
import { createKey, createResponse, renderWithConfig, sleep } from './utils'

Expand Down Expand Up @@ -50,6 +50,41 @@ describe('useSWR - preload', () => {
expect(count).toBe(1)
})

it('should be able to prealod resources in effects', async () => {
const key = createKey()
let count = 0

const fetcher = () => {
++count
return createResponse('foo')
}

function Comp() {
const { data } = useSWR(key, fetcher)
return <div>data:{data}</div>
}

function Page() {
const [show, setShow] = useState(false)
useEffect(() => {
preload(key, fetcher)
}, [])
return show ? (
<Comp />
) : (
<button onClick={() => setShow(true)}>click</button>
)
}

renderWithConfig(<Page />)
expect(count).toBe(1)

fireEvent.click(screen.getByText('click'))

await screen.findByText('data:foo')
expect(count).toBe(1)
})

it('preload the fetcher function with the suspense mode', async () => {
const key = createKey()
let count = 0
Expand Down