Skip to content

Commit 04ac1dd

Browse files
committed
test(solid-query): correctly manage dependent queries
1 parent 5b6765a commit 04ac1dd

File tree

3 files changed

+118
-37
lines changed

3 files changed

+118
-37
lines changed

packages/solid-query/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
},
7070
"devDependencies": {
7171
"@solidjs/testing-library": "^0.8.10",
72+
"@solidjs/router": "^0.15.3",
7273
"npm-run-all2": "^5.0.0",
7374
"solid-js": "^1.9.5",
7475
"tsup-preset-solid": "^2.2.0",

packages/solid-query/src/__tests__/useQuery.test.tsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, expectTypeOf, it, vi } from 'vitest'
22
import {
33
ErrorBoundary,
44
Match,
5+
Suspense,
56
Switch,
67
createEffect,
78
createMemo,
@@ -11,6 +12,7 @@ import {
1112
} from 'solid-js'
1213
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
1314
import { reconcile } from 'solid-js/store'
15+
import { MemoryRouter, Route, createMemoryHistory } from '@solidjs/router'
1416
import { QueryCache, QueryClientProvider, keepPreviousData, useQuery } from '..'
1517
import {
1618
Blink,
@@ -6045,4 +6047,79 @@ describe('useQuery', () => {
60456047

60466048
await waitFor(() => rendered.getByText('Status: custom client'))
60476049
})
6050+
6051+
// See https://github.com/tannerlinsley/react-query/issues/8469
6052+
it('should correctly manage dependent queries', async () => {
6053+
const queryCache = new QueryCache()
6054+
const queryClient = createQueryClient({ queryCache })
6055+
6056+
const history = createMemoryHistory()
6057+
6058+
const errorHandler = vi.fn<(err: unknown) => void>()
6059+
6060+
function App() {
6061+
return (
6062+
<ErrorBoundary
6063+
fallback={(err) => {
6064+
errorHandler(err)
6065+
return err.message
6066+
}}
6067+
>
6068+
<Suspense>
6069+
<MemoryRouter history={history}>
6070+
<Route path="/" component={Index} />
6071+
<Route path="/sub" component={Sub} />
6072+
</MemoryRouter>
6073+
</Suspense>
6074+
</ErrorBoundary>
6075+
)
6076+
}
6077+
6078+
queryClient.setQueryData(['parent'], { id: 123 })
6079+
6080+
function Index() {
6081+
return 'Index'
6082+
}
6083+
6084+
function Sub() {
6085+
const parent = useQuery(() => ({
6086+
queryKey: ['parent'],
6087+
async queryFn() {
6088+
await new Promise((r) => setTimeout(r, 100))
6089+
return {
6090+
id: 123,
6091+
}
6092+
},
6093+
}))
6094+
6095+
const childQuery = useQuery(() => ({
6096+
queryKey: ['sub', parent.data?.id],
6097+
async queryFn() {
6098+
await new Promise((r) => setTimeout(r, 200))
6099+
return Promise.resolve('child' + parent.data?.id)
6100+
},
6101+
}))
6102+
return <pre>{childQuery.data}</pre>
6103+
}
6104+
6105+
const rendered = render(() => (
6106+
<QueryClientProvider client={queryClient}>
6107+
<App />
6108+
</QueryClientProvider>
6109+
))
6110+
6111+
await waitFor(() => rendered.getByText('Index'))
6112+
6113+
history.set({
6114+
value: '/sub',
6115+
})
6116+
6117+
await sleep(200)
6118+
6119+
expect(errorHandler).not.toHaveBeenCalled()
6120+
6121+
await waitFor(() => {
6122+
expect(rendered.getByText('child123')).toBeInTheDocument()
6123+
})
6124+
})
60486125
})

pnpm-lock.yaml

Lines changed: 40 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)