@@ -6832,4 +6832,130 @@ describe('useQuery', () => {
6832
6832
expect ( fetchCount ) . toBe ( initialFetchCount + 1 )
6833
6833
expect ( queryFn ) . toHaveBeenCalledTimes ( 2 )
6834
6834
} )
6835
+
6836
+ it ( 'should not retry on mount when throwOnError function returns true' , async ( ) => {
6837
+ const key = queryKey ( )
6838
+ let fetchCount = 0
6839
+ const queryFn = vi . fn ( ) . mockImplementation ( ( ) => {
6840
+ fetchCount ++
6841
+ console . log ( `Fetching... (attempt ${ fetchCount } )` )
6842
+ return Promise . reject ( new Error ( 'Simulated 500 error' ) )
6843
+ } )
6844
+
6845
+ function Component ( ) {
6846
+ const { status, error } = useQuery ( {
6847
+ queryKey : key ,
6848
+ queryFn,
6849
+ throwOnError : ( ) => true ,
6850
+ retryOnMount : true ,
6851
+ staleTime : Infinity ,
6852
+ retry : false ,
6853
+ } )
6854
+
6855
+ return (
6856
+ < div >
6857
+ < div data-testid = "status" > { status } </ div >
6858
+ { error && < div data-testid = "error" > { error . message } </ div > }
6859
+ </ div >
6860
+ )
6861
+ }
6862
+
6863
+ const { unmount, getByTestId } = renderWithClient (
6864
+ queryClient ,
6865
+ < ErrorBoundary
6866
+ fallbackRender = { ( { error } ) => (
6867
+ < div >
6868
+ < div data-testid = "status" > error</ div >
6869
+ < div data-testid = "error" > { error ?. message } </ div >
6870
+ </ div >
6871
+ ) }
6872
+ >
6873
+ < Component />
6874
+ </ ErrorBoundary > ,
6875
+ )
6876
+
6877
+ await vi . waitFor ( ( ) =>
6878
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6879
+ )
6880
+ expect ( getByTestId ( 'error' ) ) . toHaveTextContent ( 'Simulated 500 error' )
6881
+ expect ( fetchCount ) . toBe ( 1 )
6882
+
6883
+ unmount ( )
6884
+
6885
+ const initialFetchCount = fetchCount
6886
+
6887
+ renderWithClient ( queryClient ,
6888
+ < ErrorBoundary
6889
+ fallbackRender = { ( { error } ) => (
6890
+ < div >
6891
+ < div data-testid = "status" > error</ div >
6892
+ < div data-testid = "error" > { error ?. message } </ div >
6893
+ </ div >
6894
+ ) }
6895
+ >
6896
+ < Component />
6897
+ </ ErrorBoundary >
6898
+ )
6899
+
6900
+ await vi . waitFor ( ( ) =>
6901
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6902
+ )
6903
+
6904
+ // Should not retry because throwOnError returns true
6905
+ expect ( fetchCount ) . toBe ( initialFetchCount )
6906
+ expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
6907
+ } )
6908
+
6909
+ it ( 'should handle throwOnError function based on actual error state' , async ( ) => {
6910
+ const key = queryKey ( )
6911
+ let fetchCount = 0
6912
+ const queryFn = vi . fn ( ) . mockImplementation ( ( ) => {
6913
+ fetchCount ++
6914
+ console . log ( `Fetching... (attempt ${ fetchCount } )` )
6915
+ return Promise . reject ( new Error ( 'Simulated 500 error' ) )
6916
+ } )
6917
+
6918
+ function Component ( ) {
6919
+ const { status, error } = useQuery ( {
6920
+ queryKey : key ,
6921
+ queryFn,
6922
+ throwOnError : ( error ) => error . message . includes ( '404' ) ,
6923
+ retryOnMount : true ,
6924
+ staleTime : Infinity ,
6925
+ retry : false ,
6926
+ } )
6927
+
6928
+ return (
6929
+ < div >
6930
+ < div data-testid = "status" > { status } </ div >
6931
+ { error && < div data-testid = "error" > { error . message } </ div > }
6932
+ </ div >
6933
+ )
6934
+ }
6935
+
6936
+ const { unmount, getByTestId } = renderWithClient (
6937
+ queryClient ,
6938
+ < Component /> ,
6939
+ )
6940
+
6941
+ await vi . waitFor ( ( ) =>
6942
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6943
+ )
6944
+ expect ( getByTestId ( 'error' ) ) . toHaveTextContent ( 'Simulated 500 error' )
6945
+ expect ( fetchCount ) . toBe ( 1 )
6946
+
6947
+ unmount ( )
6948
+
6949
+ const initialFetchCount = fetchCount
6950
+
6951
+ renderWithClient ( queryClient , < Component /> )
6952
+
6953
+ await vi . waitFor ( ( ) =>
6954
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6955
+ )
6956
+
6957
+ // Should retry because throwOnError returns false (500 error doesn't include '404')
6958
+ expect ( fetchCount ) . toBe ( initialFetchCount + 1 )
6959
+ expect ( queryFn ) . toHaveBeenCalledTimes ( 2 )
6960
+ } )
6835
6961
} )
0 commit comments