@tanstack/query-core 5.0.0-alpha.2 → 5.0.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/lib/focusManager.esm.js +31 -15
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +31 -15
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/hydration.d.ts +2 -6
- package/build/lib/hydration.esm.js +9 -20
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +9 -20
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +4 -18
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +3 -3
- package/build/lib/infiniteQueryBehavior.d.ts +2 -2
- package/build/lib/infiniteQueryBehavior.esm.js +52 -48
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +52 -48
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +48 -45
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +6 -6
- package/build/lib/infiniteQueryObserver.esm.js +7 -8
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +7 -8
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +4 -6
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +2 -1
- package/build/lib/mutation.esm.js +134 -101
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +134 -101
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +10 -2
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +2 -2
- package/build/lib/mutationCache.esm.js +34 -20
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +34 -20
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +5 -4
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.esm.js +81 -50
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +81 -50
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +1 -0
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/onlineManager.esm.js +29 -14
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +29 -14
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/queriesObserver.esm.js +95 -65
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +95 -65
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +4 -3
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +4 -3
- package/build/lib/query.esm.js +210 -161
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +210 -161
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +1 -1
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +2 -2
- package/build/lib/queryCache.esm.js +21 -13
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +21 -13
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +5 -4
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.esm.js +107 -66
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +107 -66
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +5 -4
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +2 -4
- package/build/lib/queryObserver.esm.js +299 -211
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +297 -209
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +11 -23
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.esm.js +13 -6
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +13 -6
- package/build/lib/removable.js.map +1 -1
- package/build/lib/retryer.esm.js +15 -14
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +15 -14
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/tests/utils.d.ts +4 -5
- package/build/lib/types.d.ts +5 -15
- package/build/lib/utils.esm.js +1 -1
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/umd/index.development.js +97 -109
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/hydration.ts +18 -37
- package/src/index.ts +2 -4
- package/src/infiniteQueryBehavior.ts +52 -60
- package/src/infiniteQueryObserver.ts +15 -10
- package/src/mutation.ts +14 -5
- package/src/mutationCache.ts +6 -6
- package/src/mutationObserver.ts +1 -0
- package/src/queriesObserver.ts +8 -8
- package/src/query.ts +13 -8
- package/src/queryCache.ts +9 -8
- package/src/queryClient.ts +2 -4
- package/src/queryObserver.ts +17 -31
- package/src/tests/focusManager.test.tsx +12 -14
- package/src/tests/hydration.test.tsx +22 -17
- package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
- package/src/tests/infiniteQueryObserver.test.tsx +62 -1
- package/src/tests/mutationCache.test.tsx +11 -10
- package/src/tests/mutationObserver.test.tsx +3 -2
- package/src/tests/mutations.test.tsx +41 -9
- package/src/tests/notifyManager.test.tsx +7 -6
- package/src/tests/onlineManager.test.tsx +12 -17
- package/src/tests/queriesObserver.test.tsx +18 -17
- package/src/tests/query.test.tsx +18 -17
- package/src/tests/queryCache.test.tsx +14 -13
- package/src/tests/queryClient.test.tsx +49 -48
- package/src/tests/queryObserver.test.tsx +65 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +9 -13
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
import { MutationObserver, QueryObserver } from '..'
|
|
12
12
|
import { focusManager, onlineManager } from '..'
|
|
13
13
|
import { noop } from '../utils'
|
|
14
|
+
import { vi } from 'vitest'
|
|
14
15
|
|
|
15
16
|
describe('queryClient', () => {
|
|
16
17
|
let queryClient: QueryClient
|
|
@@ -223,7 +224,7 @@ describe('queryClient', () => {
|
|
|
223
224
|
test('should accept an update function', () => {
|
|
224
225
|
const key = queryKey()
|
|
225
226
|
|
|
226
|
-
const updater =
|
|
227
|
+
const updater = vi.fn((oldData) => `new data + ${oldData}`)
|
|
227
228
|
|
|
228
229
|
queryClient.setQueryData(key, 'test data')
|
|
229
230
|
queryClient.setQueryData(key, updater)
|
|
@@ -802,7 +803,7 @@ describe('queryClient', () => {
|
|
|
802
803
|
describe('refetchQueries', () => {
|
|
803
804
|
test('should not refetch if all observers are disabled', async () => {
|
|
804
805
|
const key = queryKey()
|
|
805
|
-
const queryFn =
|
|
806
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
806
807
|
await queryClient.fetchQuery({ queryKey: key, queryFn })
|
|
807
808
|
const observer1 = new QueryObserver(queryClient, {
|
|
808
809
|
queryKey: key,
|
|
@@ -816,7 +817,7 @@ describe('queryClient', () => {
|
|
|
816
817
|
})
|
|
817
818
|
test('should refetch if at least one observer is enabled', async () => {
|
|
818
819
|
const key = queryKey()
|
|
819
|
-
const queryFn =
|
|
820
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
820
821
|
await queryClient.fetchQuery({ queryKey: key, queryFn })
|
|
821
822
|
const observer1 = new QueryObserver(queryClient, {
|
|
822
823
|
queryKey: key,
|
|
@@ -838,8 +839,8 @@ describe('queryClient', () => {
|
|
|
838
839
|
test('should refetch all queries when no arguments are given', async () => {
|
|
839
840
|
const key1 = queryKey()
|
|
840
841
|
const key2 = queryKey()
|
|
841
|
-
const queryFn1 =
|
|
842
|
-
const queryFn2 =
|
|
842
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
843
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
843
844
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
844
845
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
845
846
|
const observer1 = new QueryObserver(queryClient, {
|
|
@@ -864,8 +865,8 @@ describe('queryClient', () => {
|
|
|
864
865
|
test('should be able to refetch all fresh queries', async () => {
|
|
865
866
|
const key1 = queryKey()
|
|
866
867
|
const key2 = queryKey()
|
|
867
|
-
const queryFn1 =
|
|
868
|
-
const queryFn2 =
|
|
868
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
869
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
869
870
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
870
871
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
871
872
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -883,8 +884,8 @@ describe('queryClient', () => {
|
|
|
883
884
|
test('should be able to refetch all stale queries', async () => {
|
|
884
885
|
const key1 = queryKey()
|
|
885
886
|
const key2 = queryKey()
|
|
886
|
-
const queryFn1 =
|
|
887
|
-
const queryFn2 =
|
|
887
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
888
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
888
889
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
889
890
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
890
891
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -903,8 +904,8 @@ describe('queryClient', () => {
|
|
|
903
904
|
test('should be able to refetch all stale and active queries', async () => {
|
|
904
905
|
const key1 = queryKey()
|
|
905
906
|
const key2 = queryKey()
|
|
906
|
-
const queryFn1 =
|
|
907
|
-
const queryFn2 =
|
|
907
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
908
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
908
909
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
909
910
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
910
911
|
queryClient.invalidateQueries({ queryKey: key1 })
|
|
@@ -925,8 +926,8 @@ describe('queryClient', () => {
|
|
|
925
926
|
test('should be able to refetch all active and inactive queries', async () => {
|
|
926
927
|
const key1 = queryKey()
|
|
927
928
|
const key2 = queryKey()
|
|
928
|
-
const queryFn1 =
|
|
929
|
-
const queryFn2 =
|
|
929
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
930
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
930
931
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
931
932
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
932
933
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -944,8 +945,8 @@ describe('queryClient', () => {
|
|
|
944
945
|
test('should be able to refetch all active and inactive queries', async () => {
|
|
945
946
|
const key1 = queryKey()
|
|
946
947
|
const key2 = queryKey()
|
|
947
|
-
const queryFn1 =
|
|
948
|
-
const queryFn2 =
|
|
948
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
949
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
949
950
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
950
951
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
951
952
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -963,8 +964,8 @@ describe('queryClient', () => {
|
|
|
963
964
|
test('should be able to refetch only active queries', async () => {
|
|
964
965
|
const key1 = queryKey()
|
|
965
966
|
const key2 = queryKey()
|
|
966
|
-
const queryFn1 =
|
|
967
|
-
const queryFn2 =
|
|
967
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
968
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
968
969
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
969
970
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
970
971
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -982,8 +983,8 @@ describe('queryClient', () => {
|
|
|
982
983
|
test('should be able to refetch only inactive queries', async () => {
|
|
983
984
|
const key1 = queryKey()
|
|
984
985
|
const key2 = queryKey()
|
|
985
|
-
const queryFn1 =
|
|
986
|
-
const queryFn2 =
|
|
986
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
987
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
987
988
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
988
989
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
989
990
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1025,8 +1026,8 @@ describe('queryClient', () => {
|
|
|
1025
1026
|
test('should refetch active queries by default', async () => {
|
|
1026
1027
|
const key1 = queryKey()
|
|
1027
1028
|
const key2 = queryKey()
|
|
1028
|
-
const queryFn1 =
|
|
1029
|
-
const queryFn2 =
|
|
1029
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1030
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1030
1031
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
1031
1032
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
1032
1033
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1044,8 +1045,8 @@ describe('queryClient', () => {
|
|
|
1044
1045
|
test('should not refetch inactive queries by default', async () => {
|
|
1045
1046
|
const key1 = queryKey()
|
|
1046
1047
|
const key2 = queryKey()
|
|
1047
|
-
const queryFn1 =
|
|
1048
|
-
const queryFn2 =
|
|
1048
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1049
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1049
1050
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
1050
1051
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
1051
1052
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1063,8 +1064,8 @@ describe('queryClient', () => {
|
|
|
1063
1064
|
test('should not refetch active queries when "refetch" is "none"', async () => {
|
|
1064
1065
|
const key1 = queryKey()
|
|
1065
1066
|
const key2 = queryKey()
|
|
1066
|
-
const queryFn1 =
|
|
1067
|
-
const queryFn2 =
|
|
1067
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1068
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1068
1069
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
1069
1070
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
1070
1071
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1085,8 +1086,8 @@ describe('queryClient', () => {
|
|
|
1085
1086
|
test('should refetch inactive queries when "refetch" is "inactive"', async () => {
|
|
1086
1087
|
const key1 = queryKey()
|
|
1087
1088
|
const key2 = queryKey()
|
|
1088
|
-
const queryFn1 =
|
|
1089
|
-
const queryFn2 =
|
|
1089
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1090
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1090
1091
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
1091
1092
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
1092
1093
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1109,8 +1110,8 @@ describe('queryClient', () => {
|
|
|
1109
1110
|
test('should refetch active and inactive queries when "refetch" is "all"', async () => {
|
|
1110
1111
|
const key1 = queryKey()
|
|
1111
1112
|
const key2 = queryKey()
|
|
1112
|
-
const queryFn1 =
|
|
1113
|
-
const queryFn2 =
|
|
1113
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1114
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1114
1115
|
await queryClient.fetchQuery({ queryKey: key1, queryFn: queryFn1 })
|
|
1115
1116
|
await queryClient.fetchQuery({ queryKey: key2, queryFn: queryFn2 })
|
|
1116
1117
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -1129,7 +1130,7 @@ describe('queryClient', () => {
|
|
|
1129
1130
|
|
|
1130
1131
|
test('should cancel ongoing fetches if cancelRefetch option is set (default value)', async () => {
|
|
1131
1132
|
const key = queryKey()
|
|
1132
|
-
const abortFn =
|
|
1133
|
+
const abortFn = vi.fn()
|
|
1133
1134
|
let fetchCount = 0
|
|
1134
1135
|
const observer = new QueryObserver(queryClient, {
|
|
1135
1136
|
queryKey: key,
|
|
@@ -1152,7 +1153,7 @@ describe('queryClient', () => {
|
|
|
1152
1153
|
|
|
1153
1154
|
test('should not cancel ongoing fetches if cancelRefetch option is set to false', async () => {
|
|
1154
1155
|
const key = queryKey()
|
|
1155
|
-
const abortFn =
|
|
1156
|
+
const abortFn = vi.fn()
|
|
1156
1157
|
let fetchCount = 0
|
|
1157
1158
|
const observer = new QueryObserver(queryClient, {
|
|
1158
1159
|
queryKey: key,
|
|
@@ -1178,7 +1179,7 @@ describe('queryClient', () => {
|
|
|
1178
1179
|
test('should notify listeners when a query is reset', async () => {
|
|
1179
1180
|
const key = queryKey()
|
|
1180
1181
|
|
|
1181
|
-
const callback =
|
|
1182
|
+
const callback = vi.fn()
|
|
1182
1183
|
|
|
1183
1184
|
await queryClient.prefetchQuery({ queryKey: key, queryFn: () => 'data' })
|
|
1184
1185
|
|
|
@@ -1231,8 +1232,8 @@ describe('queryClient', () => {
|
|
|
1231
1232
|
test('should refetch all active queries', async () => {
|
|
1232
1233
|
const key1 = queryKey()
|
|
1233
1234
|
const key2 = queryKey()
|
|
1234
|
-
const queryFn1 =
|
|
1235
|
-
const queryFn2 =
|
|
1235
|
+
const queryFn1 = vi.fn<unknown[], string>().mockReturnValue('data1')
|
|
1236
|
+
const queryFn2 = vi.fn<unknown[], string>().mockReturnValue('data2')
|
|
1236
1237
|
const observer1 = new QueryObserver(queryClient, {
|
|
1237
1238
|
queryKey: key1,
|
|
1238
1239
|
queryFn: queryFn1,
|
|
@@ -1258,15 +1259,15 @@ describe('queryClient', () => {
|
|
|
1258
1259
|
const testClient = createQueryClient()
|
|
1259
1260
|
testClient.mount()
|
|
1260
1261
|
|
|
1261
|
-
const queryCacheOnFocusSpy =
|
|
1262
|
+
const queryCacheOnFocusSpy = vi.spyOn(
|
|
1262
1263
|
testClient.getQueryCache(),
|
|
1263
1264
|
'onFocus',
|
|
1264
1265
|
)
|
|
1265
|
-
const queryCacheOnOnlineSpy =
|
|
1266
|
+
const queryCacheOnOnlineSpy = vi.spyOn(
|
|
1266
1267
|
testClient.getQueryCache(),
|
|
1267
1268
|
'onOnline',
|
|
1268
1269
|
)
|
|
1269
|
-
const mutationCacheResumePausedMutationsSpy =
|
|
1270
|
+
const mutationCacheResumePausedMutationsSpy = vi.spyOn(
|
|
1270
1271
|
testClient.getMutationCache(),
|
|
1271
1272
|
'resumePausedMutations',
|
|
1272
1273
|
)
|
|
@@ -1291,15 +1292,15 @@ describe('queryClient', () => {
|
|
|
1291
1292
|
const testClient = createQueryClient()
|
|
1292
1293
|
testClient.mount()
|
|
1293
1294
|
|
|
1294
|
-
const queryCacheOnFocusSpy =
|
|
1295
|
+
const queryCacheOnFocusSpy = vi.spyOn(
|
|
1295
1296
|
testClient.getQueryCache(),
|
|
1296
1297
|
'onFocus',
|
|
1297
1298
|
)
|
|
1298
|
-
const queryCacheOnOnlineSpy =
|
|
1299
|
+
const queryCacheOnOnlineSpy = vi.spyOn(
|
|
1299
1300
|
testClient.getQueryCache(),
|
|
1300
1301
|
'onOnline',
|
|
1301
1302
|
)
|
|
1302
|
-
const mutationCacheResumePausedMutationsSpy =
|
|
1303
|
+
const mutationCacheResumePausedMutationsSpy = vi.spyOn(
|
|
1303
1304
|
testClient.getMutationCache(),
|
|
1304
1305
|
'resumePausedMutations',
|
|
1305
1306
|
)
|
|
@@ -1321,7 +1322,7 @@ describe('queryClient', () => {
|
|
|
1321
1322
|
})
|
|
1322
1323
|
|
|
1323
1324
|
test('should resume paused mutations when coming online', async () => {
|
|
1324
|
-
const consoleMock =
|
|
1325
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
1325
1326
|
consoleMock.mockImplementation(() => undefined)
|
|
1326
1327
|
onlineManager.setOnline(false)
|
|
1327
1328
|
|
|
@@ -1351,7 +1352,7 @@ describe('queryClient', () => {
|
|
|
1351
1352
|
})
|
|
1352
1353
|
|
|
1353
1354
|
test('should resume paused mutations one after the other when invoked manually at the same time', async () => {
|
|
1354
|
-
const consoleMock =
|
|
1355
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
1355
1356
|
consoleMock.mockImplementation(() => undefined)
|
|
1356
1357
|
onlineManager.setOnline(false)
|
|
1357
1358
|
|
|
@@ -1401,15 +1402,15 @@ describe('queryClient', () => {
|
|
|
1401
1402
|
testClient.mount()
|
|
1402
1403
|
testClient.unmount()
|
|
1403
1404
|
|
|
1404
|
-
const queryCacheOnFocusSpy =
|
|
1405
|
+
const queryCacheOnFocusSpy = vi.spyOn(
|
|
1405
1406
|
testClient.getQueryCache(),
|
|
1406
1407
|
'onFocus',
|
|
1407
1408
|
)
|
|
1408
|
-
const queryCacheOnOnlineSpy =
|
|
1409
|
+
const queryCacheOnOnlineSpy = vi.spyOn(
|
|
1409
1410
|
testClient.getQueryCache(),
|
|
1410
1411
|
'onOnline',
|
|
1411
1412
|
)
|
|
1412
|
-
const mutationCacheResumePausedMutationsSpy =
|
|
1413
|
+
const mutationCacheResumePausedMutationsSpy = vi.spyOn(
|
|
1413
1414
|
testClient.getMutationCache(),
|
|
1414
1415
|
'resumePausedMutations',
|
|
1415
1416
|
)
|
|
@@ -1436,15 +1437,15 @@ describe('queryClient', () => {
|
|
|
1436
1437
|
testClient.unmount()
|
|
1437
1438
|
testClient.unmount()
|
|
1438
1439
|
|
|
1439
|
-
const queryCacheOnFocusSpy =
|
|
1440
|
+
const queryCacheOnFocusSpy = vi.spyOn(
|
|
1440
1441
|
testClient.getQueryCache(),
|
|
1441
1442
|
'onFocus',
|
|
1442
1443
|
)
|
|
1443
|
-
const queryCacheOnOnlineSpy =
|
|
1444
|
+
const queryCacheOnOnlineSpy = vi.spyOn(
|
|
1444
1445
|
testClient.getQueryCache(),
|
|
1445
1446
|
'onOnline',
|
|
1446
1447
|
)
|
|
1447
|
-
const mutationCacheResumePausedMutationsSpy =
|
|
1448
|
+
const mutationCacheResumePausedMutationsSpy = vi.spyOn(
|
|
1448
1449
|
testClient.getMutationCache(),
|
|
1449
1450
|
'resumePausedMutations',
|
|
1450
1451
|
)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { sleep, queryKey, expectType, createQueryClient } from './utils'
|
|
2
2
|
import type { QueryClient, QueryObserverResult } from '..'
|
|
3
3
|
import { QueryObserver, focusManager } from '..'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('queryObserver', () => {
|
|
6
7
|
let queryClient: QueryClient
|
|
@@ -16,7 +17,7 @@ describe('queryObserver', () => {
|
|
|
16
17
|
|
|
17
18
|
test('should trigger a fetch when subscribed', async () => {
|
|
18
19
|
const key = queryKey()
|
|
19
|
-
const queryFn =
|
|
20
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
20
21
|
const observer = new QueryObserver(queryClient, { queryKey: key, queryFn })
|
|
21
22
|
const unsubscribe = observer.subscribe(() => undefined)
|
|
22
23
|
await sleep(1)
|
|
@@ -348,7 +349,7 @@ describe('queryObserver', () => {
|
|
|
348
349
|
|
|
349
350
|
test('should not trigger a fetch when subscribed and disabled', async () => {
|
|
350
351
|
const key = queryKey()
|
|
351
|
-
const queryFn =
|
|
352
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
352
353
|
const observer = new QueryObserver(queryClient, {
|
|
353
354
|
queryKey: key,
|
|
354
355
|
queryFn,
|
|
@@ -362,7 +363,7 @@ describe('queryObserver', () => {
|
|
|
362
363
|
|
|
363
364
|
test('should not trigger a fetch when not subscribed', async () => {
|
|
364
365
|
const key = queryKey()
|
|
365
|
-
const queryFn =
|
|
366
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
366
367
|
new QueryObserver(queryClient, { queryKey: key, queryFn })
|
|
367
368
|
await sleep(1)
|
|
368
369
|
expect(queryFn).toHaveBeenCalledTimes(0)
|
|
@@ -370,8 +371,8 @@ describe('queryObserver', () => {
|
|
|
370
371
|
|
|
371
372
|
test('should be able to watch a query without defining a query function', async () => {
|
|
372
373
|
const key = queryKey()
|
|
373
|
-
const queryFn =
|
|
374
|
-
const callback =
|
|
374
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
375
|
+
const callback = vi.fn()
|
|
375
376
|
const observer = new QueryObserver(queryClient, {
|
|
376
377
|
queryKey: key,
|
|
377
378
|
enabled: false,
|
|
@@ -385,7 +386,7 @@ describe('queryObserver', () => {
|
|
|
385
386
|
|
|
386
387
|
test('should accept unresolved query config in update function', async () => {
|
|
387
388
|
const key = queryKey()
|
|
388
|
-
const queryFn =
|
|
389
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
389
390
|
const observer = new QueryObserver(queryClient, {
|
|
390
391
|
queryKey: key,
|
|
391
392
|
enabled: false,
|
|
@@ -407,7 +408,7 @@ describe('queryObserver', () => {
|
|
|
407
408
|
|
|
408
409
|
test('should be able to handle multiple subscribers', async () => {
|
|
409
410
|
const key = queryKey()
|
|
410
|
-
const queryFn =
|
|
411
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
411
412
|
const observer = new QueryObserver<string>(queryClient, {
|
|
412
413
|
queryKey: key,
|
|
413
414
|
enabled: false,
|
|
@@ -567,7 +568,7 @@ describe('queryObserver', () => {
|
|
|
567
568
|
|
|
568
569
|
test('should not refetch in background if refetchIntervalInBackground is false', async () => {
|
|
569
570
|
const key = queryKey()
|
|
570
|
-
const queryFn =
|
|
571
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
571
572
|
|
|
572
573
|
focusManager.setFocused(false)
|
|
573
574
|
const observer = new QueryObserver(queryClient, {
|
|
@@ -690,6 +691,61 @@ describe('queryObserver', () => {
|
|
|
690
691
|
expect(observer.getCurrentResult().isPlaceholderData).toBe(false)
|
|
691
692
|
})
|
|
692
693
|
|
|
694
|
+
test('should pass the correct previous data to placeholderData function params when select function is used in conjunction', async () => {
|
|
695
|
+
const results: QueryObserverResult[] = []
|
|
696
|
+
|
|
697
|
+
const key1 = queryKey()
|
|
698
|
+
const key2 = queryKey()
|
|
699
|
+
|
|
700
|
+
const data1 = { value: 'data1' }
|
|
701
|
+
const data2 = { value: 'data2' }
|
|
702
|
+
|
|
703
|
+
const observer = new QueryObserver(queryClient, {
|
|
704
|
+
queryKey: key1,
|
|
705
|
+
queryFn: () => data1,
|
|
706
|
+
placeholderData: (prev) => prev,
|
|
707
|
+
select: (data) => data.value,
|
|
708
|
+
})
|
|
709
|
+
|
|
710
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
711
|
+
results.push(result)
|
|
712
|
+
})
|
|
713
|
+
|
|
714
|
+
await sleep(1)
|
|
715
|
+
|
|
716
|
+
observer.setOptions({
|
|
717
|
+
queryKey: key2,
|
|
718
|
+
queryFn: () => data2,
|
|
719
|
+
placeholderData: (prev) => prev,
|
|
720
|
+
select: (data) => data.value,
|
|
721
|
+
})
|
|
722
|
+
|
|
723
|
+
await sleep(1)
|
|
724
|
+
unsubscribe()
|
|
725
|
+
|
|
726
|
+
expect(results.length).toBe(4)
|
|
727
|
+
expect(results[0]).toMatchObject({
|
|
728
|
+
data: undefined,
|
|
729
|
+
status: 'pending',
|
|
730
|
+
fetchStatus: 'fetching',
|
|
731
|
+
}) // Initial fetch
|
|
732
|
+
expect(results[1]).toMatchObject({
|
|
733
|
+
data: 'data1',
|
|
734
|
+
status: 'success',
|
|
735
|
+
fetchStatus: 'idle',
|
|
736
|
+
}) // Successful fetch
|
|
737
|
+
expect(results[2]).toMatchObject({
|
|
738
|
+
data: 'data1',
|
|
739
|
+
status: 'success',
|
|
740
|
+
fetchStatus: 'fetching',
|
|
741
|
+
}) // Fetch for new key, but using previous data as placeholder
|
|
742
|
+
expect(results[3]).toMatchObject({
|
|
743
|
+
data: 'data2',
|
|
744
|
+
status: 'success',
|
|
745
|
+
fetchStatus: 'idle',
|
|
746
|
+
}) // Successful fetch for new key
|
|
747
|
+
})
|
|
748
|
+
|
|
693
749
|
test('setOptions should notify cache listeners', async () => {
|
|
694
750
|
const key = queryKey()
|
|
695
751
|
|
|
@@ -697,7 +753,7 @@ describe('queryObserver', () => {
|
|
|
697
753
|
queryKey: key,
|
|
698
754
|
})
|
|
699
755
|
|
|
700
|
-
const spy =
|
|
756
|
+
const spy = vi.fn()
|
|
701
757
|
const unsubscribe = queryClient.getQueryCache().subscribe(spy)
|
|
702
758
|
observer.setOptions({ enabled: false })
|
|
703
759
|
|
package/src/tests/utils.test.tsx
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from '../utils'
|
|
12
12
|
import { Mutation } from '../mutation'
|
|
13
13
|
import { createQueryClient } from './utils'
|
|
14
|
+
import { vi } from 'vitest'
|
|
14
15
|
|
|
15
16
|
describe('core/utils', () => {
|
|
16
17
|
describe('isPlainObject', () => {
|
|
@@ -343,7 +344,7 @@ describe('core/utils', () => {
|
|
|
343
344
|
|
|
344
345
|
describe('scheduleMicrotask', () => {
|
|
345
346
|
it('should defer execution of callback', async () => {
|
|
346
|
-
const callback =
|
|
347
|
+
const callback = vi.fn()
|
|
347
348
|
|
|
348
349
|
scheduleMicrotask(callback)
|
|
349
350
|
expect(callback).not.toHaveBeenCalled()
|
package/src/tests/utils.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { act } from '@testing-library/react'
|
|
2
|
+
import { vi } from 'vitest'
|
|
2
3
|
|
|
3
|
-
import type { MutationOptions, QueryClientConfig } from '
|
|
4
|
-
import { QueryClient } from '
|
|
4
|
+
import type { MutationOptions, QueryClientConfig } from '..'
|
|
5
|
+
import { QueryClient } from '..'
|
|
5
6
|
import * as utils from '../utils'
|
|
6
7
|
|
|
7
8
|
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
@@ -9,11 +10,11 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
12
|
-
return
|
|
13
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export function mockNavigatorOnLine(value: boolean) {
|
|
16
|
-
return
|
|
17
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
let queryKeyCount = 0
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { RetryValue, RetryDelayValue } from './retryer'
|
|
|
6
6
|
import type { QueryFilters, QueryTypeFilter } from './utils'
|
|
7
7
|
import type { QueryCache } from './queryCache'
|
|
8
8
|
import type { MutationCache } from './mutationCache'
|
|
9
|
+
import type { FetchDirection } from './query'
|
|
9
10
|
|
|
10
11
|
export interface Register {
|
|
11
12
|
// defaultError: Error
|
|
@@ -40,6 +41,7 @@ export type QueryFunctionContext<
|
|
|
40
41
|
queryKey: TQueryKey
|
|
41
42
|
signal: AbortSignal
|
|
42
43
|
pageParam: TPageParam
|
|
44
|
+
direction: FetchDirection
|
|
43
45
|
meta: QueryMeta | undefined
|
|
44
46
|
}
|
|
45
47
|
|
|
@@ -62,11 +64,15 @@ export type QueryKeyHashFunction<TQueryKey extends QueryKey> = (
|
|
|
62
64
|
export type GetPreviousPageParamFunction<TPageParam, TQueryFnData = unknown> = (
|
|
63
65
|
firstPage: TQueryFnData,
|
|
64
66
|
allPages: TQueryFnData[],
|
|
67
|
+
firstPageParam: TPageParam,
|
|
68
|
+
allPageParams: TPageParam[],
|
|
65
69
|
) => TPageParam | undefined
|
|
66
70
|
|
|
67
71
|
export type GetNextPageParamFunction<TPageParam, TQueryFnData = unknown> = (
|
|
68
72
|
lastPage: TQueryFnData,
|
|
69
73
|
allPages: TQueryFnData[],
|
|
74
|
+
lastPageParam: TPageParam,
|
|
75
|
+
allPageParams: TPageParam[],
|
|
70
76
|
) => TPageParam | undefined
|
|
71
77
|
|
|
72
78
|
export interface InfiniteData<TData> {
|
|
@@ -253,18 +259,6 @@ export interface QueryObserverOptions<
|
|
|
253
259
|
* By default, access to properties will be tracked, and the component will only re-render when one of the tracked properties change.
|
|
254
260
|
*/
|
|
255
261
|
notifyOnChangeProps?: Array<keyof InfiniteQueryObserverResult> | 'all'
|
|
256
|
-
/**
|
|
257
|
-
* This callback will fire any time the query successfully fetches new data.
|
|
258
|
-
*/
|
|
259
|
-
onSuccess?: (data: TData) => void
|
|
260
|
-
/**
|
|
261
|
-
* This callback will fire if the query encounters an error and will be passed the error.
|
|
262
|
-
*/
|
|
263
|
-
onError?: (err: TError) => void
|
|
264
|
-
/**
|
|
265
|
-
* This callback will fire any time the query is either successfully fetched or errors and be passed either the data or error.
|
|
266
|
-
*/
|
|
267
|
-
onSettled?: (data: TData | undefined, error: TError | null) => void
|
|
268
262
|
/**
|
|
269
263
|
* Whether errors should be thrown instead of setting the `error` property.
|
|
270
264
|
* If set to `true` or `suspense` is `true`, all errors will be thrown to the error boundary.
|
|
@@ -329,13 +323,15 @@ export type DefaultedInfiniteQueryObserverOptions<
|
|
|
329
323
|
TData = TQueryFnData,
|
|
330
324
|
TQueryData = TQueryFnData,
|
|
331
325
|
TQueryKey extends QueryKey = QueryKey,
|
|
326
|
+
TPageParam = unknown,
|
|
332
327
|
> = WithRequired<
|
|
333
328
|
InfiniteQueryObserverOptions<
|
|
334
329
|
TQueryFnData,
|
|
335
330
|
TError,
|
|
336
331
|
TData,
|
|
337
332
|
TQueryData,
|
|
338
|
-
TQueryKey
|
|
333
|
+
TQueryKey,
|
|
334
|
+
TPageParam
|
|
339
335
|
>,
|
|
340
336
|
'throwErrors' | 'refetchOnReconnect'
|
|
341
337
|
>
|