@@ -54,6 +54,13 @@ const api = createApi({
54
54
body : { name : 'Timmy' } ,
55
55
} ) ,
56
56
} ) ,
57
+ getUserAndForceError : build . query < { name : string } , number > ( {
58
+ query : ( ) => ( {
59
+ body : {
60
+ forceError : true ,
61
+ } ,
62
+ } ) ,
63
+ } ) ,
57
64
getIncrementedAmount : build . query < any , void > ( {
58
65
query : ( ) => ( {
59
66
url : '' ,
@@ -915,6 +922,111 @@ describe('hooks tests', () => {
915
922
expect ( screen . queryByText ( / A n e r r o r h a s o c c u r r e d / i) ) . toBeNull ( )
916
923
expect ( screen . queryByText ( 'Request was aborted' ) ) . toBeNull ( )
917
924
} )
925
+
926
+ test ( 'unwrapping the useLazyQuery trigger result does not throw on ConditionError and instead returns the aggregate error' , async ( ) => {
927
+ function User ( ) {
928
+ const [ getUser , { data, error } ] =
929
+ api . endpoints . getUserAndForceError . useLazyQuery ( )
930
+
931
+ const [ unwrappedError , setUnwrappedError ] = React . useState < any > ( )
932
+
933
+ const handleClick = async ( ) => {
934
+ const res = getUser ( 1 )
935
+
936
+ try {
937
+ await res . unwrap ( )
938
+ } catch ( error ) {
939
+ setUnwrappedError ( error )
940
+ }
941
+ }
942
+
943
+ return (
944
+ < div >
945
+ < button onClick = { handleClick } > Fetch User</ button >
946
+ < div data-testid = "result" > { JSON . stringify ( data ) } </ div >
947
+ < div data-testid = "error" > { JSON . stringify ( error ) } </ div >
948
+ < div data-testid = "unwrappedError" >
949
+ { JSON . stringify ( unwrappedError ) }
950
+ </ div >
951
+ </ div >
952
+ )
953
+ }
954
+
955
+ render ( < User /> , { wrapper : storeRef . wrapper } )
956
+
957
+ const fetchButton = screen . getByRole ( 'button' , { name : 'Fetch User' } )
958
+ fireEvent . click ( fetchButton )
959
+ fireEvent . click ( fetchButton ) // This technically dispatches a ConditionError, but we don't want to see that here. We want the real error to resolve.
960
+
961
+ await waitFor ( ( ) => {
962
+ const errorResult = screen . getByTestId ( 'error' ) ?. textContent
963
+ const unwrappedErrorResult =
964
+ screen . getByTestId ( 'unwrappedError' ) ?. textContent
965
+
966
+ errorResult &&
967
+ unwrappedErrorResult &&
968
+ expect ( JSON . parse ( errorResult ) ) . toMatchObject ( {
969
+ status : 500 ,
970
+ data : null ,
971
+ } ) &&
972
+ expect ( JSON . parse ( unwrappedErrorResult ) ) . toMatchObject (
973
+ JSON . parse ( errorResult )
974
+ )
975
+ } )
976
+
977
+ expect ( screen . getByTestId ( 'result' ) . textContent ) . toBe ( '' )
978
+ } )
979
+
980
+ test ( 'useLazyQuery does not throw on ConditionError and instead returns the aggregate result' , async ( ) => {
981
+ function User ( ) {
982
+ const [ getUser , { data, error } ] = api . endpoints . getUser . useLazyQuery ( )
983
+
984
+ const [ unwrappedResult , setUnwrappedResult ] = React . useState <
985
+ undefined | { name : string }
986
+ > ( )
987
+
988
+ const handleClick = async ( ) => {
989
+ const res = getUser ( 1 )
990
+
991
+ const result = await res . unwrap ( )
992
+ setUnwrappedResult ( result )
993
+ }
994
+
995
+ return (
996
+ < div >
997
+ < button onClick = { handleClick } > Fetch User</ button >
998
+ < div data-testid = "result" > { JSON . stringify ( data ) } </ div >
999
+ < div data-testid = "error" > { JSON . stringify ( error ) } </ div >
1000
+ < div data-testid = "unwrappedResult" >
1001
+ { JSON . stringify ( unwrappedResult ) }
1002
+ </ div >
1003
+ </ div >
1004
+ )
1005
+ }
1006
+
1007
+ render ( < User /> , { wrapper : storeRef . wrapper } )
1008
+
1009
+ const fetchButton = screen . getByRole ( 'button' , { name : 'Fetch User' } )
1010
+ fireEvent . click ( fetchButton )
1011
+ fireEvent . click ( fetchButton ) // This technically dispatches a ConditionError, but we don't want to see that here. We want the real result to resolve and ignore the error.
1012
+
1013
+ await waitFor ( ( ) => {
1014
+ const dataResult = screen . getByTestId ( 'error' ) ?. textContent
1015
+ const unwrappedDataResult =
1016
+ screen . getByTestId ( 'unwrappedResult' ) ?. textContent
1017
+
1018
+ dataResult &&
1019
+ unwrappedDataResult &&
1020
+ expect ( JSON . parse ( dataResult ) ) . toMatchObject ( {
1021
+ name : 'Timmy' ,
1022
+ } ) &&
1023
+ expect ( JSON . parse ( unwrappedDataResult ) ) . toMatchObject (
1024
+ JSON . parse ( dataResult )
1025
+ )
1026
+ } )
1027
+
1028
+ expect ( screen . getByTestId ( 'error' ) . textContent ) . toBe ( '' )
1029
+ } )
918
1030
} )
919
1031
920
1032
describe ( 'useMutation' , ( ) => {
0 commit comments