Skip to content

Commit

Permalink
fix: disabling query in suspense mode (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugzpodder authored Jun 27, 2020
1 parent ac2a21f commit 53e8424
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/react/tests/suspense.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,29 @@ describe("useQuery's in Suspense mode", () => {

await waitFor(() => rendered.getByText('rendered'))
})

it('should not call the queryFn when not enabled', async () => {
const queryFn = jest.fn()
queryFn.mockImplementation(() => sleep(10))

function Page() {
const [enabled, setEnabled] = React.useState(false)
useQuery(['test'], queryFn, { suspense: true, enabled })

return <button aria-label="fire" onClick={() => setEnabled(true)} />
}

const rendered = render(
<React.Suspense fallback="loading">
<Page />
</React.Suspense>
)

expect(queryFn).toHaveBeenCalledTimes(0)

fireEvent.click(rendered.getByLabelText('fire'))

expect(queryFn).toHaveBeenCalledTimes(1)
await waitFor(() => rendered.getByLabelText('fire'))
})
})
6 changes: 5 additions & 1 deletion src/react/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export function handleSuspense(queryInfo) {
throw queryInfo.error
}

if (queryInfo.query.config.suspense && queryInfo.status !== statusSuccess) {
if (
queryInfo.query.config.suspense &&
queryInfo.status !== statusSuccess &&
queryInfo.query.config.enabled
) {
queryInfo.query.wasSuspended = true
throw queryInfo.query.fetch()
}
Expand Down

0 comments on commit 53e8424

Please sign in to comment.