Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 7, 2020
1 parent 601fe3e commit c97a351
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,9 @@ function useSWR<Data = any, Error = any>(
// need to start the request if it hasn't

if (!CONCURRENT_PROMISES[key]) {
if (key) {
// trigger revalidate immediately
// to get the promise
revalidate()
} else {
throw Promise.reject()
}
// trigger revalidate immediately
// to get the promise
revalidate()
}

if (
Expand All @@ -571,6 +567,10 @@ function useSWR<Data = any, Error = any>(
throw latestError
}

if (!key) {
throw new Promise(() => {})
}

// return the latest data / error from cache
// in case `key` has changed
return {
Expand Down
47 changes: 28 additions & 19 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1270,30 +1270,31 @@ describe('useSWR - suspense', () => {

// hold render when suspense
it('should pause when key is falsy', async () => {
function SectionContent({ swrKey, onClick }) {
function SectionContent({ swrKey }) {
const { data } = useSWR(
() => (swrKey % 2 === 0 ? null : swrKey),
k => new Promise(res => setTimeout(() => res(k), 30)),
() => (swrKey % 2 ? null : 'suspense-' + swrKey),
key =>
new Promise(res =>
setTimeout(() => res(key.replace('suspense-', '')), 100)
),
{
suspense: true
}
)

return (
<div onClick={onClick}>
{swrKey && data ? 'suspense-' + (9 + Number(data)) : undefined}
</div>
)
return <div>{data}</div>
}

const Section = () => {
const [key, setKey] = useState(0)
const [key, setKey] = useState(9)
const handleClick = () => setKey(key + 1)

return (
<Suspense fallback={<div onClick={handleClick}>fallback</div>}>
<SectionContent onClick={handleClick} swrKey={key} />
</Suspense>
<div onClick={handleClick}>
<Suspense fallback={<div>fallback</div>}>
<SectionContent swrKey={key} />
</Suspense>
</div>
)
}

Expand All @@ -1306,13 +1307,21 @@ describe('useSWR - suspense', () => {
)
}

expect(container.textContent).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(100)
expect(container.textContent).toMatchInlineSnapshot(`"suspense-10"`)
await clickAndWait(100)
expect(container.textContent).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(100)
expect(container.textContent).toMatchInlineSnapshot(`"suspense-10"`)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"10"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"fallback"`)
await clickAndWait(150)
expect(
container.firstElementChild.lastElementChild.textContent
).toMatchInlineSnapshot(`"12"`)
})
})

Expand Down

0 comments on commit c97a351

Please sign in to comment.