Skip to content

Commit 5035d6e

Browse files
committed
feat: support query v4
1 parent fdfac47 commit 5035d6e

File tree

9 files changed

+636
-36
lines changed

9 files changed

+636
-36
lines changed

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
"posttest:vue-2.7": "vue-demi-switch 3"
5757
},
5858
"peerDependencies": {
59-
"@tanstack/query-core": "^5.*",
60-
"@tanstack/react-query": "^5.*",
61-
"@tanstack/vue-query": "^5.*",
59+
"@tanstack/query-core": "^4.* || ^5.*",
60+
"@tanstack/react-query": "^4.* || ^5.*",
61+
"@tanstack/vue-query": "^4.* || ^5.*",
6262
"@vue/composition-api": "^1.1.0",
6363
"react": "^18.0.0",
6464
"vue": "^2.6.12 || ^3.2.0"
@@ -86,6 +86,9 @@
8686
"@tanstack/query-core": "^5.18.1",
8787
"@tanstack/react-query": "^5.18.1",
8888
"@tanstack/vue-query": "^5.18.1",
89+
"@tanstack/query-core-v4": "npm:@tanstack/query-core@^4.36.1",
90+
"@tanstack/react-query-v4": "npm:@tanstack/react-query@^4.36.1",
91+
"@tanstack/vue-query-v4": "npm:@tanstack/vue-query@^4.37.1",
8992
"@testing-library/react": "14.2.1",
9093
"@testing-library/vue": "^8.0.1",
9194
"@tsconfig/node18": "^18.2.2",

pnpm-lock.yaml

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

src/core/index-v4.test.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { QueryClient } from '@tanstack/query-core-v4'
3+
import { subscribeQueryCallbacks } from './index'
4+
5+
vi.mock('@tanstack/query-core', () => import('@tanstack/query-core-v4'))
6+
7+
describe('core (v4)', () => {
8+
let queryClient: QueryClient
9+
10+
beforeEach(() => {
11+
queryClient = new QueryClient()
12+
queryClient.mount()
13+
})
14+
15+
afterEach(() => {
16+
queryClient.clear()
17+
})
18+
19+
it('should call onSuccess & onSettled', async () => {
20+
const onSuccess = vi.fn()
21+
const onSettled = vi.fn()
22+
const QUERY_KEY = ['foo']
23+
24+
subscribeQueryCallbacks({
25+
queryKey: QUERY_KEY,
26+
queryClient: queryClient as any,
27+
onSuccess,
28+
onSettled,
29+
})
30+
31+
await queryClient.fetchQuery({
32+
queryKey: QUERY_KEY,
33+
queryFn: () => Promise.resolve('bar'),
34+
})
35+
36+
expect(onSuccess).toBeCalledTimes(1)
37+
expect(onSuccess).toBeCalledWith('bar')
38+
expect(onSettled).toBeCalledTimes(1)
39+
expect(onSettled).toBeCalledWith('bar', null)
40+
})
41+
42+
it('should call onError, onSettled', async () => {
43+
const onError = vi.fn()
44+
const onSettled = vi.fn()
45+
const QUERY_KEY = ['foo']
46+
47+
subscribeQueryCallbacks({
48+
queryKey: QUERY_KEY,
49+
queryClient: queryClient as any,
50+
onError,
51+
onSettled,
52+
})
53+
54+
await queryClient.fetchQuery({
55+
queryKey: QUERY_KEY,
56+
// eslint-disable-next-line prefer-promise-reject-errors
57+
queryFn: () => Promise.reject('bar'),
58+
}).catch(() => {})
59+
60+
expect(onError).toBeCalledTimes(1)
61+
expect(onError).toBeCalledWith('bar')
62+
expect(onSettled).toBeCalledTimes(1)
63+
expect(onSettled).toBeCalledWith(undefined, 'bar')
64+
})
65+
66+
it('should not call anything when fetch other query', async () => {
67+
const onError = vi.fn()
68+
const onSettled = vi.fn()
69+
const onSuccess = vi.fn()
70+
const QUERY_KEY = ['foo']
71+
const OTHER_QUERY_KEY = ['bar']
72+
73+
subscribeQueryCallbacks({
74+
queryKey: QUERY_KEY,
75+
queryClient: queryClient as any,
76+
onSuccess,
77+
onError,
78+
onSettled,
79+
})
80+
81+
await queryClient.prefetchQuery({
82+
queryKey: OTHER_QUERY_KEY,
83+
// eslint-disable-next-line prefer-promise-reject-errors
84+
queryFn: () => Promise.reject('bar'),
85+
}).catch(() => {})
86+
87+
expect(onSuccess).not.toBeCalled()
88+
expect(onError).not.toBeCalled()
89+
expect(onSettled).not.toBeCalled()
90+
})
91+
92+
it('should not call anything when unsubscribed', async () => {
93+
const onSuccess = vi.fn()
94+
const onError = vi.fn()
95+
const onSettled = vi.fn()
96+
const QUERY_KEY = ['foo']
97+
98+
const unsubscribe = subscribeQueryCallbacks({
99+
queryKey: QUERY_KEY,
100+
queryClient: queryClient as any,
101+
onSuccess,
102+
onError,
103+
onSettled,
104+
})
105+
106+
unsubscribe()
107+
108+
await queryClient.fetchQuery({
109+
queryKey: QUERY_KEY,
110+
queryFn: () => Promise.resolve('bar'),
111+
}).catch(() => {})
112+
113+
expect(onSuccess).not.toBeCalled()
114+
expect(onError).not.toBeCalled()
115+
expect(onSettled).not.toBeCalled()
116+
})
117+
118+
it('should return empty func', async () => {
119+
const QUERY_KEY = ['foo']
120+
121+
const unsubscribe = subscribeQueryCallbacks({
122+
queryKey: QUERY_KEY,
123+
queryClient: queryClient as any,
124+
})
125+
126+
expect(unsubscribe).toBeTypeOf('function')
127+
})
128+
})

0 commit comments

Comments
 (0)