Skip to content

Commit 0179035

Browse files
committed
tests(react-query): allow multiple types in query returns
1 parent cba33ee commit 0179035

File tree

2 files changed

+87
-28
lines changed

2 files changed

+87
-28
lines changed

packages/react-query/src/__tests__/usePrefetchQueries.test.tsx

+16-28
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
66

77
import type { UseSuspenseQueryOptions } from '..'
88

9-
const generateQueryFn = (data: string) =>
10-
vi
11-
.fn<(...args: Array<any>) => Promise<string>>()
12-
.mockImplementation(async () => {
13-
await sleep(10)
9+
const generateQueryFn = <T,>(data: T) =>
10+
vi.fn<(...args: Array<any>) => Promise<T>>().mockImplementation(async () => {
11+
await sleep(10)
1412

15-
return data
16-
})
13+
return data
14+
})
1715

1816
describe('usePrefetchQueries', () => {
1917
const queryCache = new QueryCache()
2018
const queryClient = createQueryClient({ queryCache })
2119

22-
function Suspended<TData = unknown>(props: {
23-
queriesOpts: Array<
24-
UseSuspenseQueryOptions<TData, Error, TData, Array<string>>
25-
>
20+
function Suspended(props: {
21+
queriesOpts: Array<UseSuspenseQueryOptions>
2622
children?: React.ReactNode
2723
}) {
2824
const state = useSuspenseQueries({
@@ -46,7 +42,7 @@ describe('usePrefetchQueries', () => {
4642

4743
const queryOpts2 = {
4844
queryKey: queryKey(),
49-
queryFn: generateQueryFn('prefetchQuery2'),
45+
queryFn: generateQueryFn(2),
5046
}
5147

5248
const componentQueryOpts1 = {
@@ -56,7 +52,7 @@ describe('usePrefetchQueries', () => {
5652

5753
const componentQueryOpts2 = {
5854
...queryOpts2,
59-
queryFn: generateQueryFn('useSuspenseQuery2'),
55+
queryFn: generateQueryFn(2),
6056
}
6157

6258
function App() {
@@ -73,9 +69,7 @@ describe('usePrefetchQueries', () => {
7369

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

76-
await waitFor(() =>
77-
rendered.getByText('data: prefetchQuery1, prefetchQuery2'),
78-
)
72+
await waitFor(() => rendered.getByText('data: prefetchQuery1, 2'))
7973
expect(queryOpts1.queryFn).toHaveBeenCalledTimes(1)
8074
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
8175
})
@@ -88,7 +82,7 @@ describe('usePrefetchQueries', () => {
8882

8983
const queryOpts2 = {
9084
queryKey: queryKey(),
91-
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 2'),
85+
queryFn: generateQueryFn(2),
9286
}
9387

9488
function App() {
@@ -112,9 +106,7 @@ describe('usePrefetchQueries', () => {
112106

113107
expect(rendered.queryByText('fetching: true')).not.toBeInTheDocument()
114108
await waitFor(() =>
115-
rendered.getByText(
116-
'data: The usePrefetchQueries hook is smart! 1, The usePrefetchQueries hook is smart! 2',
117-
),
109+
rendered.getByText('data: The usePrefetchQueries hook is smart! 1, 2'),
118110
)
119111
expect(queryOpts1.queryFn).not.toHaveBeenCalled()
120112
expect(queryOpts2.queryFn).not.toHaveBeenCalled()
@@ -128,7 +120,7 @@ describe('usePrefetchQueries', () => {
128120

129121
const queryOpts2 = {
130122
queryKey: queryKey(),
131-
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 2'),
123+
queryFn: generateQueryFn(2),
132124
}
133125

134126
function App() {
@@ -150,9 +142,7 @@ describe('usePrefetchQueries', () => {
150142
const rendered = renderWithClient(queryClient, <App />)
151143

152144
await waitFor(() =>
153-
rendered.getByText(
154-
'data: The usePrefetchQueries hook is smart! 1, The usePrefetchQueries hook is smart! 2',
155-
),
145+
rendered.getByText('data: The usePrefetchQueries hook is smart! 1, 2'),
156146
)
157147
expect(queryOpts1.queryFn).not.toHaveBeenCalled()
158148
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
@@ -166,7 +156,7 @@ describe('usePrefetchQueries', () => {
166156

167157
const queryOpts2 = {
168158
queryKey: queryKey(),
169-
queryFn: generateQueryFn('prefetchedQuery2'),
159+
queryFn: generateQueryFn(2),
170160
}
171161

172162
function Prefetch({ children }: { children: React.ReactNode }) {
@@ -187,9 +177,7 @@ describe('usePrefetchQueries', () => {
187177
}
188178

189179
const rendered = renderWithClient(queryClient, <App />)
190-
await waitFor(() =>
191-
rendered.getByText('data: prefetchedQuery1, prefetchedQuery2'),
192-
)
180+
await waitFor(() => rendered.getByText('data: prefetchedQuery1, 2'))
193181
expect(queryOpts1.queryFn).toHaveBeenCalledTimes(1)
194182
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
195183
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"allowSyntheticDefaultImports": true,
6+
"allowUnreachableCode": false,
7+
"allowUnusedLabels": false,
8+
"checkJs": true,
9+
"composite": true,
10+
"customConditions": [
11+
"@tanstack/custom-condition"
12+
],
13+
"declaration": true,
14+
"declarationMap": true,
15+
"emitDeclarationOnly": false,
16+
"esModuleInterop": true,
17+
"forceConsistentCasingInFileNames": true,
18+
"incremental": true,
19+
"isolatedModules": true,
20+
"lib": [
21+
"DOM",
22+
"DOM.Iterable",
23+
"ES2022"
24+
],
25+
"module": "es2022",
26+
"moduleResolution": "bundler",
27+
"noEmit": false,
28+
"noImplicitReturns": true,
29+
"noUncheckedIndexedAccess": true,
30+
"noUnusedLocals": true,
31+
"noUnusedParameters": true,
32+
"resolveJsonModule": true,
33+
"skipLibCheck": true,
34+
"strict": true,
35+
"target": "es2020",
36+
"outDir": "./dist-ts",
37+
"rootDir": "./",
38+
"baseUrl": "./",
39+
"jsx": "react-jsx",
40+
"noImplicitAny": true,
41+
"noImplicitThis": true,
42+
"strictNullChecks": true,
43+
"strictFunctionTypes": true,
44+
"strictBindCallApply": true,
45+
"strictPropertyInitialization": true,
46+
"strictBuiltinIteratorReturn": true,
47+
"alwaysStrict": true,
48+
"useUnknownInCatchVariables": true,
49+
"resolvePackageJsonExports": true,
50+
"resolvePackageJsonImports": true,
51+
"preserveConstEnums": true,
52+
"tsBuildInfoFile": "/home/dogpawhat/Development/tanstack/query/node_modules/.pnpm/vitest@3.1.1_@types+debug@4.1.12_@types+node@22.14.0_jiti@2.4.2_jsdom@25.0.1_less@4.2.2_d19a37a9b627b186f6ea2a1304dc7011/node_modules/vitest/dist/chunks/tsconfig.tmp.tsbuildinfo"
53+
},
54+
"include": [
55+
"src",
56+
"*.config.js",
57+
"*.config.ts",
58+
"package.json"
59+
],
60+
"references": [
61+
{
62+
"path": "../query-core"
63+
},
64+
{
65+
"path": "../query-persist-client-core"
66+
}
67+
],
68+
"exclude": [
69+
"./dist-ts"
70+
]
71+
}

0 commit comments

Comments
 (0)