Skip to content

Commit 48212e0

Browse files
committed
wip
1 parent e04afcc commit 48212e0

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

Suspense-tests-todo.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
- [x] test/use-swr-infinite.test.tsx — should update the getKey reference with the suspense mode (migrated to e2e/test/suspense-infinite-get-key.test.ts)
44
- [x] test/use-swr-suspense.test.tsx — should render fallback (migrated to e2e/test/suspense-render-fallback.test.ts)
55
- [x] test/use-swr-suspense.test.tsx — should render multiple SWR fallbacks (migrated to e2e/test/render-suspense-multiple-fallbacks.test.ts)
6-
- [ ] test/use-swr-suspense.test.tsx — should work for non-promises
7-
- [ ] test/use-swr-suspense.test.tsx — should throw errors
6+
- [x] test/use-swr-suspense.test.tsx — should work for non-promises (migrated to e2e/test/render-suspense-non-promise.test.ts)
7+
- [x] test/use-swr-suspense.test.tsx — should throw errors (migrated to e2e/test/render-suspense-error.test.ts)
88
- [ ] test/use-swr-suspense.test.tsx — should render cached data with error
99
- [ ] test/use-swr-suspense.test.tsx — should not fetch when cached data is present and `revalidateIfStale` is false
1010
- [ ] test/use-swr-suspense.test.tsx — should pause when key changes
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use client'
2+
3+
import { Suspense } from 'react'
4+
import useSWR from 'swr'
5+
import { ErrorBoundary } from 'react-error-boundary'
6+
import { OnlyRenderInClient } from '~/component/only-render-in-client'
7+
8+
function delay(ms: number) {
9+
return new Promise(resolve => setTimeout(resolve, ms))
10+
}
11+
12+
async function fetchWithError(): Promise<string> {
13+
await delay(120)
14+
throw new Error('error')
15+
}
16+
17+
function Section() {
18+
const { data } = useSWR('render-suspense-error', fetchWithError, {
19+
suspense: true
20+
})
21+
22+
return <div data-testid="data">{data}</div>
23+
}
24+
25+
function ErrorFallback(_: { error: Error; resetErrorBoundary: () => void }) {
26+
return <div data-testid="error">error boundary</div>
27+
}
28+
29+
export default function Page() {
30+
return (
31+
<OnlyRenderInClient>
32+
<ErrorBoundary FallbackComponent={ErrorFallback}>
33+
<Suspense fallback={<div data-testid="fallback">fallback</div>}>
34+
<Section />
35+
</Suspense>
36+
</ErrorBoundary>
37+
</OnlyRenderInClient>
38+
)
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use client'
2+
3+
import { Suspense } from 'react'
4+
import useSWR from 'swr'
5+
import { OnlyRenderInClient } from '~/component/only-render-in-client'
6+
7+
function Section() {
8+
const { data } = useSWR('render-suspense-non-promise', () => 'hello', {
9+
suspense: true
10+
})
11+
12+
return <div data-testid="data">{data}</div>
13+
}
14+
15+
export default function Page() {
16+
return (
17+
<OnlyRenderInClient>
18+
<Suspense fallback={<div data-testid="fallback">fallback</div>}>
19+
<Section />
20+
</Suspense>
21+
</OnlyRenderInClient>
22+
)
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from '@playwright/test'
2+
3+
test.describe('render-suspense-error', () => {
4+
test('surfaces boundary error after suspense fallback', async ({ page }) => {
5+
await page.goto('./render-suspense-error', { waitUntil: 'commit' })
6+
7+
await expect(page.getByTestId('fallback')).toBeVisible()
8+
await expect(page.getByTestId('error')).toBeVisible()
9+
})
10+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from '@playwright/test'
2+
3+
test.describe('render-suspense-non-promise', () => {
4+
test('renders synchronously resolved data without showing fallback', async ({
5+
page
6+
}) => {
7+
await page.goto('./render-suspense-non-promise', { waitUntil: 'commit' })
8+
9+
await expect(page.getByTestId('fallback')).toHaveCount(0)
10+
await expect(page.getByTestId('data')).toHaveText('hello')
11+
})
12+
})

0 commit comments

Comments
 (0)