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