@@ -18,7 +18,6 @@ const fetch = global.fetch as FetchMock
18
18
19
19
const { NO_CACHE , NETWORK_ONLY } = CachePolicies
20
20
21
-
22
21
describe ( 'useFetch - BROWSER - basic functionality' , ( ) : void => {
23
22
const expected = {
24
23
name : 'Alex Cory' ,
@@ -742,11 +741,8 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
742
741
743
742
it ( 'should retryOn specific error codes' , async ( ) : Promise < void > => {
744
743
fetch . resetMocks ( )
745
- fetch . mockResponseOnce ( 'fail' , {
746
- status : 401
747
- } ) . mockResponseOnce ( 'fail' , {
748
- status : 400
749
- } )
744
+ fetch . mockResponseOnce ( 'fail' , { status : 401 } )
745
+ . mockResponseOnce ( 'fail' , { status : 400 } )
750
746
// should fail, then retry on 401, fail again, but not retry on 400
751
747
const { result, waitForNextUpdate } = renderHook (
752
748
( ) => useFetch ( 'url' , {
@@ -760,17 +756,14 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
760
756
761
757
it ( 'should retryOn custom function' , async ( ) : Promise < void > => {
762
758
fetch . resetMocks ( )
763
- fetch . mockResponseOnce ( 'fail' , {
764
- status : 401
765
- } ) . mockResponseOnce ( 'fail' , {
766
- status : 400
767
- } )
759
+ fetch . mockResponseOnce ( 'fail' , { status : 401 } )
760
+ . mockResponseOnce ( 'fail' , { status : 400 } )
768
761
// should fail, then retry on 401, fail again, but not retry on 400
769
762
const { result, waitForNextUpdate } = renderHook (
770
763
( ) => useFetch ( 'url-2' , {
771
764
retryOn ( { response } ) {
772
765
return ! ! ( response && response . status === 401 )
773
- } ,
766
+ }
774
767
} , [ ] )
775
768
)
776
769
await waitForNextUpdate ( )
@@ -780,15 +773,13 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
780
773
781
774
it ( 'should retry 3 times, fail all 3, then retry 3 more times when called again' , async ( ) : Promise < void > => {
782
775
fetch . resetMocks ( )
783
- fetch . mockResponse ( 'fail' , {
784
- status : 400
785
- } )
776
+ fetch . mockResponse ( 'fail' , { status : 400 } )
786
777
const { result, waitForNextUpdate } = renderHook (
787
778
( ) => useFetch ( 'url-12' , {
788
779
retryOn ( { response } ) {
789
780
return ! ! ( response && response . status === 400 )
790
781
} ,
791
- cachePolicy : CachePolicies . NO_CACHE ,
782
+ cachePolicy : CachePolicies . NO_CACHE
792
783
} , [ ] )
793
784
)
794
785
await waitForNextUpdate ( )
@@ -799,13 +790,11 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
799
790
expect ( fetch . mock . calls . length ) . toBe ( 6 )
800
791
} )
801
792
802
- it ( 'should retry with a `retryDelay` as a positive number 2 ' , async ( ) : Promise < void > => {
793
+ it ( 'should retry with a `retryDelay` as a positive number' , async ( ) : Promise < void > => {
803
794
fetch . resetMocks ( )
804
- fetch . mockResponseOnce ( 'fail' , {
805
- status : 401
806
- } ) . mockResponseOnce ( 'fail' , {
807
- status : 400
808
- } ) . mockResponseOnce ( JSON . stringify ( { no : 'way' } ) )
795
+ fetch . mockResponseOnce ( 'fail' , { status : 401 } )
796
+ . mockResponseOnce ( 'fail' , { status : 400 } )
797
+ . mockResponseOnce ( JSON . stringify ( { no : 'way' } ) )
809
798
810
799
const { result, waitForNextUpdate } = renderHook (
811
800
( ) => useFetch ( 'url-5' , {
@@ -819,31 +808,26 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
819
808
expect ( fetch . mock . calls . length ) . toBe ( 3 )
820
809
} )
821
810
822
- it ( 'should retry with a `retryDelay` as a positive number ' , async ( ) : Promise < void > => {
811
+ it ( 'should retry with a `retryDelay` as a function ' , async ( ) : Promise < void > => {
823
812
fetch . resetMocks ( )
824
- fetch . mockResponseOnce ( 'fail' , {
825
- status : 401
826
- } ) . mockResponseOnce ( 'fail' , {
827
- status : 400
828
- } )
829
-
813
+ fetch . mockResponse ( 'fail' , { status : 400 } )
830
814
const { result, waitForNextUpdate } = renderHook (
831
- ( ) => useFetch ( 'url-4 ' , {
815
+ ( ) => useFetch ( 'url-7 ' , {
832
816
retryOn : [ 401 , 400 ] ,
833
- retryDelay : 100 ,
834
- retries : 1
817
+ retryDelay ( ) {
818
+ return 100
819
+ }
835
820
} , [ ] )
836
821
)
837
822
await waitForNextUpdate ( )
838
823
expect ( result . current . error ) . toEqual ( { name : 400 , message : 'Bad Request' } )
839
- expect ( fetch . mock . calls . length ) . toBe ( 2 )
824
+ expect ( fetch . mock . calls . length ) . toBe ( 3 )
840
825
} )
841
826
842
827
// TODO: there is an issue with error testing some things
843
828
// see more detail here: https://github.com/testing-library/react-hooks-testing-library/issues/308
829
+ // we basically want to test if when we call the `retry` function, if the `delay` is a positive number
844
830
it ( 'should error with a `retryDelay` that is not a postive # or a function returning a positive #' , async ( ) : Promise < void > => {
845
- expect ( true ) . toBe ( true )
846
- console . log ( 'TODO: react-hooks-testing-library not handling errors properly' )
847
831
// fetch.resetMocks()
848
832
// fetch.mockResponse('fail', {
849
833
// status: 400
@@ -859,27 +843,21 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
859
843
// }
860
844
// render = () => !this.state.hasError && this.props.children
861
845
// }
862
-
863
846
// const wrapper = ({ children }: { children?: ReactNode }): ReactElement => <ErrorBoundary>{children}</ErrorBoundary>
864
-
865
- // const { result, waitForNextUpdate } = renderHook(() => useFetch('Z', { retryDelay: -1000 }, []), { wrapper })
866
-
867
- // await waitForNextUpdate()
868
- // console.log('result.error', result.error)
869
- // console.log('caughtError', caughtError)
870
- } )
871
-
872
- it ( 'should error if `retryDelay` is not a function returning a positive number' , async ( ) : Promise < void > => {
873
- expect ( true ) . toBe ( true )
874
- console . log ( 'TODO: react-hooks-testing-library not handling errors properly' )
847
+ // const { result } = renderHook(() => useFetch('Z', { retryDelay: -1000 }, []), { wrapper })
848
+ const { result } = renderHook ( ( ) => useFetch ( 'url-123124' , { retryDelay : - 1000 } , [ ] ) )
849
+ expect ( result . error . name ) . toBe ( 'Invariant Violation' )
875
850
} )
876
851
877
852
it ( 'should error if `retryOn` is not a function or an array of positive numbers' , async ( ) : Promise < void > => {
878
853
// TODO: should we check to see if they are valid http status codes?
879
854
// - regex: /^[1-5][0-9][0-9]$/
880
855
// - ts HttpStatusCodes enum: https://gist.github.com/RWOverdijk/6cef816cfdf5722228e01cc05fd4b094
881
- expect ( true ) . toBe ( true )
882
- console . log ( 'TODO: react-hooks-testing-library not handling errors properly' )
856
+ var { result } = renderHook ( ( ) => useFetch ( 'url-11211' , { retryOn : 1000 as any } , [ ] ) )
857
+ expect ( result . error . name ) . toBe ( 'Invariant Violation' )
858
+ // eslint-disable-next-line
859
+ var { result } = renderHook ( ( ) => useFetch ( 'url-11211' , { retryOn : [ 'c' ] as any } , [ ] ) )
860
+ expect ( result . error . name ) . toBe ( 'Invariant Violation' )
883
861
} )
884
862
} )
885
863
0 commit comments