@@ -5,14 +5,21 @@ import {
5
5
sleep ,
6
6
} from '@tanstack/query-test-utils'
7
7
import {
8
+ Query ,
8
9
QueryClient ,
9
10
QueryObserver ,
10
11
dehydrate ,
11
12
hydrate ,
12
13
isCancelledError ,
13
14
} from '..'
15
+ import { hashQueryKeyByOptions } from '../utils'
14
16
import { mockOnlineManagerIsOnline , setIsServer } from './utils'
15
- import type { QueryCache , QueryFunctionContext , QueryObserverResult } from '..'
17
+ import type {
18
+ QueryCache ,
19
+ QueryFunctionContext ,
20
+ QueryKey ,
21
+ QueryObserverResult ,
22
+ } from '..'
16
23
17
24
describe ( 'query' , ( ) => {
18
25
let queryClient : QueryClient
@@ -1019,4 +1026,73 @@ describe('query', () => {
1019
1026
await vi . advanceTimersByTimeAsync ( 10 )
1020
1027
expect ( query . state . status ) . toBe ( 'error' )
1021
1028
} )
1029
+
1030
+ test ( 'should use persister if provided' , async ( ) => {
1031
+ const key = queryKey ( )
1032
+
1033
+ await queryClient . prefetchQuery ( {
1034
+ queryKey : key ,
1035
+ queryFn : ( ) => 'data' ,
1036
+ persister : ( ) => Promise . resolve ( 'persisted data' ) ,
1037
+ } )
1038
+
1039
+ const query = queryCache . find ( { queryKey : key } ) !
1040
+ expect ( query . state . data ) . toBe ( 'persisted data' )
1041
+ } )
1042
+
1043
+ test ( 'should use queryFn from observer if not provided in options' , async ( ) => {
1044
+ const key = queryKey ( )
1045
+ const queryFn = ( ) => Promise . resolve ( 'data' )
1046
+ const observer = new QueryObserver ( queryClient , {
1047
+ queryKey : key ,
1048
+ queryFn : queryFn ,
1049
+ } )
1050
+
1051
+ const query = new Query ( {
1052
+ client : queryClient ,
1053
+ queryKey : key ,
1054
+ queryHash : hashQueryKeyByOptions ( key ) ,
1055
+ } )
1056
+
1057
+ query . addObserver ( observer )
1058
+
1059
+ await query . fetch ( )
1060
+ const result = await query . state . data
1061
+ expect ( result ) . toBe ( 'data' )
1062
+ expect ( query . options . queryFn ) . toBe ( queryFn )
1063
+ } )
1064
+
1065
+ test ( 'should log error when queryKey is not an array' , async ( ) => {
1066
+ const consoleMock = vi . spyOn ( console , 'error' )
1067
+ const key : unknown = 'string-key'
1068
+
1069
+ await queryClient . prefetchQuery ( {
1070
+ queryKey : key as QueryKey ,
1071
+ queryFn : ( ) => 'data' ,
1072
+ } )
1073
+
1074
+ expect ( consoleMock ) . toHaveBeenCalledWith (
1075
+ "As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']" ,
1076
+ )
1077
+
1078
+ consoleMock . mockRestore ( )
1079
+ } )
1080
+
1081
+ test ( 'should call initialData function when it is a function' , ( ) => {
1082
+ const key = queryKey ( )
1083
+ const initialDataFn = vi . fn ( ( ) => 'initial data' )
1084
+
1085
+ const query = new Query ( {
1086
+ client : queryClient ,
1087
+ queryKey : key ,
1088
+ queryHash : hashQueryKeyByOptions ( key ) ,
1089
+ options : {
1090
+ queryFn : ( ) => 'data' ,
1091
+ initialData : initialDataFn ,
1092
+ } ,
1093
+ } )
1094
+
1095
+ expect ( initialDataFn ) . toHaveBeenCalledTimes ( 1 )
1096
+ expect ( query . state . data ) . toBe ( 'initial data' )
1097
+ } )
1022
1098
} )
0 commit comments