@@ -5920,6 +5920,7 @@ describe('useQuery', () => {
5920
5920
it ( 'should be able to toggle subscribed' , async ( ) => {
5921
5921
const key = queryKey ( )
5922
5922
const queryFn = vi . fn ( ( ) => Promise . resolve ( 'data' ) )
5923
+
5923
5924
function Page ( ) {
5924
5925
const [ subscribed , setSubscribed ] = React . useState ( true )
5925
5926
const { data } = useQuery ( {
@@ -5964,6 +5965,7 @@ describe('useQuery', () => {
5964
5965
it ( 'should not be attached to the query when subscribed is false' , async ( ) => {
5965
5966
const key = queryKey ( )
5966
5967
const queryFn = vi . fn ( ( ) => Promise . resolve ( 'data' ) )
5968
+
5967
5969
function Page ( ) {
5968
5970
const { data } = useQuery ( {
5969
5971
queryKey : key ,
@@ -5992,6 +5994,7 @@ describe('useQuery', () => {
5992
5994
it ( 'should not re-render when data is added to the cache when subscribed is false' , async ( ) => {
5993
5995
const key = queryKey ( )
5994
5996
let renders = 0
5997
+
5995
5998
function Page ( ) {
5996
5999
const { data } = useQuery ( {
5997
6000
queryKey : key ,
@@ -6191,6 +6194,7 @@ describe('useQuery', () => {
6191
6194
await sleep ( 5 )
6192
6195
return { numbers : { current : { id } } }
6193
6196
}
6197
+
6194
6198
function Test ( ) {
6195
6199
const [ id , setId ] = React . useState ( 1 )
6196
6200
@@ -6256,6 +6260,7 @@ describe('useQuery', () => {
6256
6260
await sleep ( 5 )
6257
6261
return { numbers : { current : { id } } }
6258
6262
}
6263
+
6259
6264
function Test ( ) {
6260
6265
const [ id , setId ] = React . useState ( 1 )
6261
6266
@@ -6761,10 +6766,12 @@ describe('useQuery', () => {
6761
6766
it ( 'should console.error when there is no queryFn' , ( ) => {
6762
6767
const consoleErrorMock = vi . spyOn ( console , 'error' )
6763
6768
const key = queryKey ( )
6769
+
6764
6770
function Example ( ) {
6765
6771
useQuery ( { queryKey : key } )
6766
6772
return < > </ >
6767
6773
}
6774
+
6768
6775
renderWithClient ( queryClient , < Example /> )
6769
6776
6770
6777
expect ( consoleErrorMock ) . toHaveBeenCalledTimes ( 1 )
@@ -6774,4 +6781,56 @@ describe('useQuery', () => {
6774
6781
6775
6782
consoleErrorMock . mockRestore ( )
6776
6783
} )
6784
+
6785
+ it ( 'should retry on mount when throwOnError returns false' , async ( ) => {
6786
+ const key = queryKey ( )
6787
+ let fetchCount = 0
6788
+ const queryFn = vi . fn ( ) . mockImplementation ( ( ) => {
6789
+ fetchCount ++
6790
+ console . log ( `Fetching... (attempt ${ fetchCount } )` )
6791
+ return Promise . reject ( new Error ( 'Simulated 500 error' ) )
6792
+ } )
6793
+
6794
+ function Component ( ) {
6795
+ const { status, error } = useQuery ( {
6796
+ queryKey : key ,
6797
+ queryFn,
6798
+ throwOnError : ( ) => false ,
6799
+ retryOnMount : true ,
6800
+ staleTime : Infinity ,
6801
+ retry : false ,
6802
+ } )
6803
+
6804
+ return (
6805
+ < div >
6806
+ < div data-testid = "status" > { status } </ div >
6807
+ { error && < div data-testid = "error" > { error . message } </ div > }
6808
+ </ div >
6809
+ )
6810
+ }
6811
+
6812
+ const { unmount, getByTestId } = renderWithClient (
6813
+ queryClient ,
6814
+ < Component /> ,
6815
+ )
6816
+
6817
+ await vi . waitFor ( ( ) =>
6818
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6819
+ )
6820
+ expect ( getByTestId ( 'error' ) ) . toHaveTextContent ( 'Simulated 500 error' )
6821
+ expect ( fetchCount ) . toBe ( 1 )
6822
+
6823
+ unmount ( )
6824
+
6825
+ const initialFetchCount = fetchCount
6826
+
6827
+ renderWithClient ( queryClient , < Component /> )
6828
+
6829
+ await vi . waitFor ( ( ) =>
6830
+ expect ( getByTestId ( 'status' ) ) . toHaveTextContent ( 'error' ) ,
6831
+ )
6832
+
6833
+ expect ( fetchCount ) . toBe ( initialFetchCount + 1 )
6834
+ expect ( queryFn ) . toHaveBeenCalledTimes ( 2 )
6835
+ } )
6777
6836
} )
0 commit comments