@@ -978,6 +978,123 @@ describe('queryClient', () => {
978978 expect ( second ) . toBe ( first )
979979 } )
980980
981+ test ( 'should throw when disabled and no cached data exists' , async ( ) => {
982+ const key = queryKey ( )
983+ const queryFn = vi . fn ( ( ) => Promise . resolve ( 'data' ) )
984+
985+ await expect (
986+ queryClient . query ( {
987+ queryKey : key ,
988+ queryFn,
989+ enabled : false ,
990+ } ) ,
991+ ) . rejects . toThrowError ( )
992+
993+ expect ( queryFn ) . not . toHaveBeenCalled ( )
994+ } )
995+
996+ test ( 'should return cached data when disabled and apply select' , async ( ) => {
997+ const key = queryKey ( )
998+ const queryFn = vi . fn ( ( ) => Promise . resolve ( 'fetched-data' ) )
999+
1000+ queryClient . setQueryData ( key , 'cached-data' )
1001+
1002+ const result = await queryClient . query ( {
1003+ queryKey : key ,
1004+ queryFn,
1005+ enabled : false ,
1006+ staleTime : 0 ,
1007+ select : ( data ) => `${ data } -selected` ,
1008+ } )
1009+
1010+ expect ( result ) . toBe ( 'cached-data-selected' )
1011+ expect ( queryFn ) . not . toHaveBeenCalled ( )
1012+ } )
1013+
1014+ test ( 'should throw when skipToken is provided and no cached data exists' , async ( ) => {
1015+ const key = queryKey ( )
1016+ const select = vi . fn ( ( data : unknown ) => ( data as string ) . length )
1017+
1018+ await expect (
1019+ queryClient . query ( {
1020+ queryKey : key ,
1021+ queryFn : skipToken ,
1022+ select,
1023+ } ) ,
1024+ ) . rejects . toThrowError ( )
1025+
1026+ expect ( select ) . not . toHaveBeenCalled ( )
1027+ } )
1028+
1029+ test ( 'should return cached data when skipToken is provided' , async ( ) => {
1030+ const key = queryKey ( )
1031+
1032+ queryClient . setQueryData ( key , 'cached-data' )
1033+
1034+ const result = await queryClient . query ( {
1035+ queryKey : key ,
1036+ queryFn : skipToken ,
1037+ select : ( data : unknown ) => ( data as string ) . length ,
1038+ } )
1039+
1040+ expect ( result ) . toBe ( 'cached-data' . length )
1041+ } )
1042+
1043+ test ( 'should return cached data when skipToken and enabled false are both provided' , async ( ) => {
1044+ const key = queryKey ( )
1045+
1046+ queryClient . setQueryData ( key , { value : 'cached-data' } )
1047+
1048+ const result = await queryClient . query ( {
1049+ queryKey : key ,
1050+ queryFn : skipToken ,
1051+ enabled : false ,
1052+ select : ( data : { value : string } ) => data . value . toUpperCase ( ) ,
1053+ } )
1054+
1055+ expect ( result ) . toBe ( 'CACHED-DATA' )
1056+ } )
1057+
1058+ test ( 'should throw when enabled resolves true and skipToken are provided with no cached data' , async ( ) => {
1059+ await expect (
1060+ queryClient . query ( {
1061+ queryKey : queryKey ( ) ,
1062+ queryFn : skipToken ,
1063+ enabled : true ,
1064+ } ) ,
1065+ ) . rejects . toThrowError ( )
1066+ } )
1067+
1068+ test ( 'should return cached data when enabled resolves false and skipToken are provided' , async ( ) => {
1069+ const key1 = queryKey ( )
1070+ queryClient . setQueryData ( key1 , { value : 'cached-data' } )
1071+
1072+ const booleanDisabledResult = await queryClient . query ( {
1073+ queryKey : key1 ,
1074+ queryFn : skipToken ,
1075+ enabled : false ,
1076+ select : ( data : { value : string } ) => data . value . length ,
1077+ } )
1078+
1079+ expect ( booleanDisabledResult ) . toBe ( 'cached-data' . length )
1080+ } )
1081+
1082+ test ( 'should return cached data when enabled callback returns false even if queryFn would return different data' , async ( ) => {
1083+ const key = queryKey ( )
1084+ const queryFn = vi . fn ( ( ) => Promise . resolve ( 'fetched-data' ) )
1085+
1086+ queryClient . setQueryData ( key , 'cached-data' )
1087+
1088+ const result = await queryClient . query ( {
1089+ queryKey : key ,
1090+ queryFn,
1091+ enabled : ( ) => false ,
1092+ } )
1093+
1094+ expect ( result ) . toBe ( 'cached-data' )
1095+ expect ( queryFn ) . not . toHaveBeenCalled ( )
1096+ } )
1097+
9811098 test ( 'should read from cache with static staleTime even if invalidated' , async ( ) => {
9821099 const key = queryKey ( )
9831100
0 commit comments