Skip to content

Commit

Permalink
fix(core): remove enabled condition in queryObserver for suspense (#3093
Browse files Browse the repository at this point in the history
)

* fix(core): remove enabled condition in queryObserver

#2434

* chore: add missing test case

* chore: fix format
  • Loading branch information
JaeYeopHan committed Dec 14, 2021
1 parent 988ba10 commit 0781142
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,7 @@ function shouldFetchOptionally(
return (
options.enabled !== false &&
(query !== prevQuery || prevOptions.enabled === false) &&
(!options.suspense ||
query.state.status !== 'error' ||
prevOptions.enabled === false) &&
(!options.suspense || query.state.status !== 'error') &&
isStale(query, options)
)
}
Expand Down
70 changes: 70 additions & 0 deletions src/react/tests/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,76 @@ describe("useQuery's in Suspense mode", () => {
consoleMock.mockRestore()
})

it('should error catched in error boundary without infinite loop when enabled changed', async () => {
const consoleMock = mockConsoleError()

const succeed = false

function Page() {
const queryKeys = '1'
const [enabled, setEnabled] = React.useState(false)

const result = useQuery(
[queryKeys],
async () => {
await sleep(10)
if (!succeed) {
throw new Error('Suspense Error Bingo')
} else {
return 'data'
}
},
{
retry: false,
suspense: true,
enabled,
}
)
return (
<div>
<span>rendered</span> <span>{result.data}</span>
<button
aria-label="fail"
onClick={() => {
setEnabled(true)
}}
>
fail
</button>
</div>
)
}

function App() {
const { reset } = useQueryErrorResetBoundary()
return (
<ErrorBoundary
onReset={reset}
fallbackRender={() => <div>error boundary</div>}
>
<React.Suspense fallback="Loading...">
<Page />
</React.Suspense>
</ErrorBoundary>
)
}

const rendered = renderWithClient(queryClient, <App />)

// render empty data with 'rendered' when enabled is false
await waitFor(() => rendered.getByText('rendered'))

// change enabled to true
fireEvent.click(rendered.getByLabelText('fail'))

// render pending fallback
await waitFor(() => rendered.getByText('Loading...'))

// render error boundary fallback (error boundary)
await waitFor(() => rendered.getByText('error boundary'))
consoleMock.mockRestore()
})

it('should render the correct amount of times in Suspense mode when cacheTime is set to 0', async () => {
const key = queryKey()
let state: UseQueryResult<number> | null = null
Expand Down

1 comment on commit 0781142

@vercel
Copy link

@vercel vercel bot commented on 0781142 Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.