@@ -6027,6 +6027,7 @@ describe('useQuery', () => {
6027
6027
it ( 'should be able to toggle subscribed' , async ( ) => {
6028
6028
const key = queryKey ( )
6029
6029
const queryFn = vi . fn ( ( ) => Promise . resolve ( 'data' ) )
6030
+
6030
6031
function Page ( ) {
6031
6032
const [ subscribed , setSubscribed ] = React . useState ( true )
6032
6033
const { data } = useQuery ( {
@@ -6069,6 +6070,7 @@ describe('useQuery', () => {
6069
6070
it ( 'should not be attached to the query when subscribed is false' , async ( ) => {
6070
6071
const key = queryKey ( )
6071
6072
const queryFn = vi . fn ( ( ) => Promise . resolve ( 'data' ) )
6073
+
6072
6074
function Page ( ) {
6073
6075
const { data } = useQuery ( {
6074
6076
queryKey : key ,
@@ -6095,6 +6097,7 @@ describe('useQuery', () => {
6095
6097
it ( 'should not re-render when data is added to the cache when subscribed is false' , async ( ) => {
6096
6098
const key = queryKey ( )
6097
6099
let renders = 0
6100
+
6098
6101
function Page ( ) {
6099
6102
const { data } = useQuery ( {
6100
6103
queryKey : key ,
@@ -6297,6 +6300,7 @@ describe('useQuery', () => {
6297
6300
await sleep ( 5 )
6298
6301
return { numbers : { current : { id } } }
6299
6302
}
6303
+
6300
6304
function Test ( ) {
6301
6305
const [ id , setId ] = React . useState ( 1 )
6302
6306
@@ -6357,6 +6361,7 @@ describe('useQuery', () => {
6357
6361
await sleep ( 5 )
6358
6362
return { numbers : { current : { id } } }
6359
6363
}
6364
+
6360
6365
function Test ( ) {
6361
6366
const [ id , setId ] = React . useState ( 1 )
6362
6367
@@ -6854,10 +6859,12 @@ describe('useQuery', () => {
6854
6859
it ( 'should console.error when there is no queryFn' , ( ) => {
6855
6860
const consoleErrorMock = vi . spyOn ( console , 'error' )
6856
6861
const key = queryKey ( )
6862
+
6857
6863
function Example ( ) {
6858
6864
useQuery ( { queryKey : key } )
6859
6865
return < > </ >
6860
6866
}
6867
+
6861
6868
renderWithClient ( queryClient , < Example /> )
6862
6869
6863
6870
expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 )
@@ -6867,4 +6874,56 @@ describe('useQuery', () => {
6867
6874
6868
6875
consoleErrorMock . mockRestore ( )
6869
6876
} )
6877
+
6878
+ it ( 'should retry on mount when throwOnError returns false' , async ( ) => {
6879
+ const key = queryKey ( )
6880
+ let fetchCount = 0
6881
+ const queryFn = vi . fn ( ) . mockImplementation ( ( ) => {
6882
+ fetchCount ++
6883
+ console . log ( `Fetching... (attempt ${ fetchCount } )` )
6884
+ return Promise . reject ( new Error ( 'Simulated 500 error' ) )
6885
+ } )
6886
+
6887
+ function Component ( ) {
6888
+ const { status, error } = useQuery ( {
6889
+ queryKey : key ,
6890
+ queryFn,
6891
+ throwOnError : ( ) => false ,
6892
+ retryOnMount : true ,
6893
+ staleTime : Infinity ,
6894
+ retry : false ,
6895
+ } )
6896
+
6897
+ return (
6898
+ < div >
6899
+ < div data-testid = "status" > { status } </ div >
6900
+ { error && < div data-testid = "error" > { error . message } </ div > }
6901
+ </ div >
6902
+ )
6903
+ }
6904
+
6905
+ const { unmount, getByTestId } = renderWithClient (
6906
+ queryClient ,
6907
+ < Component /> ,
6908
+ )
6909
+
6910
+ await vi . waitFor ( ( ) =>
6911
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6912
+ )
6913
+ expect ( getByTestId ( 'error' ) ) . toHaveTextContent ( 'Simulated 500 error' )
6914
+ expect ( fetchCount ) . toBe ( 1 )
6915
+
6916
+ unmount ( )
6917
+
6918
+ const initialFetchCount = fetchCount
6919
+
6920
+ renderWithClient ( queryClient , < Component /> )
6921
+
6922
+ await vi . waitFor ( ( ) =>
6923
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6924
+ )
6925
+
6926
+ expect ( fetchCount ) . toBe ( initialFetchCount + 1 )
6927
+ expect ( queryFn ) . toHaveBeenCalledTimes ( 2 )
6928
+ } )
6870
6929
} )
0 commit comments