Skip to content

Commit

Permalink
test: add the return data when a request is in flight
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Feb 4, 2021
1 parent dff1aa1 commit b174b9a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ describe('useSWR', () => {
})

it('should revalidate even if initialData is provided', async () => {
const fetcher = key => key
const fetcher = async key => {
await sleep(50)
return key
}

function Page() {
const [key, setKey] = useState('initial-data-with-initial-data')
Expand All @@ -321,7 +324,7 @@ describe('useSWR', () => {
})
return (
<div onClick={() => setKey('initial-data-with-initial-data-update')}>
hello, {data}
{data ? `hello, ${data}` : 'loading'}
</div>
)
}
Expand All @@ -331,10 +334,20 @@ describe('useSWR', () => {
// render with the initial data
await screen.findByText('hello, Initial')

await act(() => sleep(1))
fireEvent.focus(window)

await screen.findByText('hello, initial-data-with-initial-data')

// change the key
await act(() => sleep(1))
fireEvent.click(container.firstElementChild)

// a request is still in flight
await act(() => sleep(10))
// THIS SHOULD BE FAILED, BUT IT'S PASSED
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"loading"`)

// render with data the fetcher returns
await screen.findByText('hello, initial-data-with-initial-data-update')
})
Expand Down

0 comments on commit b174b9a

Please sign in to comment.