Skip to content

Commit b97cd50

Browse files
prateek3255TkDodo
andauthoredApr 28, 2023
fix(devtools): Fix restore loading not working when clicked repeatedly (#5311)
* 🐛 Fix restore loading not working when clicked repeatedly * ✅ Add test for restore loading * 🚨 Fix formatting issues --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 373b5d5 commit b97cd50

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
 

‎packages/react-query-devtools/src/__tests__/devtools.test.tsx

+52
Original file line numberDiff line numberDiff line change
@@ -1091,4 +1091,56 @@ describe('ReactQueryDevtools', () => {
10911091
expect(screen.getByText('No error, success')).toBeInTheDocument()
10921092
})
10931093
})
1094+
1095+
it('should not refetch when already restoring a query', async () => {
1096+
const { queryClient } = createQueryClient()
1097+
1098+
let count = 0
1099+
let resolvePromise: (value: unknown) => void = () => undefined
1100+
1101+
function App() {
1102+
const { data } = useQuery(['key'], () => {
1103+
count++
1104+
1105+
// Resolve the promise immediately when
1106+
// the query is fetched for the first time
1107+
if (count === 1) {
1108+
return Promise.resolve('test')
1109+
}
1110+
1111+
return new Promise((resolve) => {
1112+
// Do not resolve immediately and store the
1113+
// resolve function to resolve the promise later
1114+
resolvePromise = resolve
1115+
})
1116+
})
1117+
1118+
return (
1119+
<div>
1120+
<h1>{typeof data === 'string' ? data : 'No data'}</h1>
1121+
</div>
1122+
)
1123+
}
1124+
1125+
renderWithClient(queryClient, <App />, {
1126+
initialIsOpen: true,
1127+
})
1128+
1129+
const loadingButton = await screen.findByRole('button', {
1130+
name: 'Trigger loading',
1131+
})
1132+
fireEvent.click(loadingButton)
1133+
1134+
await waitFor(() => {
1135+
expect(screen.getByText('Restore loading')).toBeInTheDocument()
1136+
})
1137+
1138+
// Click the restore loading button twice and only resolve query promise
1139+
// after the second click.
1140+
fireEvent.click(screen.getByRole('button', { name: /restore loading/i }))
1141+
fireEvent.click(screen.getByRole('button', { name: /restore loading/i }))
1142+
resolvePromise('test')
1143+
1144+
expect(count).toBe(2)
1145+
})
10941146
})

‎packages/react-query-devtools/src/devtools.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,15 @@ const ActiveQuery = ({
10351035
<Button
10361036
type="button"
10371037
onClick={() => {
1038+
// Return early if the query is already restoring
1039+
if (
1040+
activeQuery.state.fetchStatus === 'fetching' &&
1041+
typeof activeQuery.state.fetchMeta?.__previousQueryOptions ===
1042+
'undefined'
1043+
) {
1044+
return
1045+
}
1046+
10381047
if (activeQuery.state.data === undefined) {
10391048
restoreQueryAfterLoadingOrError()
10401049
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.