3

For My project I am using one custom hook for navigation some of my screens as from one screen to another screen based on the parameters provide to the function of custom hook. How could I Unit Test it for React Native Custom

const {result} = renderHook(() => {useShoppingCartNavigator()});

The problem is I am getting result.current as void and unable to call function of the hook

But according to the doc it should be like

result.current.customHookFn();
todofixthis
  • 1,072
  • 1
  • 12
  • 25
rchgupta
  • 89
  • 1
  • 1
  • 6
  • Does this help answer your question: [Testing return value of a custom hook](https://stackoverflow.com/a/66176284/1870780)? – juliomalves Mar 08 '21 at 14:26
  • Yes This helped me out for this error, Although now I am getting some other error.. TypeError: (0 , _stack.createStackNavigator) is not a function const ProductStack = createStackNavigator(); When I am trying to test my navigator,If I will mock this then will get another other due to mocking the function which I have to test ... Thanks a lot for your help... – rchgupta Mar 17 '21 at 17:11

1 Answers1

1

The callback inside your renderHook is not returning anything because you wrapped your customHook on curlybraces. It should be

const {result} = renderHook(() => useShoppingCartNavigator());
jpnieto
  • 11
  • 1
  • What effect did adding the curly braces there have? – ryanwebjackson Apr 26 '22 at 04:19
  • 2
    @ryanwebjackson omitting the curly braces causes an implicit return of the expression after the arrow. Using curly braces tells the function to expect an explicit `return` statement, otherwise it returns `undefined`. – J. Sheets Jul 21 '22 at 03:44
  • RenderHook API reference: https://github.com/testing-library/react-hooks-testing-library/blob/3f944e36b9811ecb5d4518492b928946cde0947d/docs/api-reference.md – ryanwebjackson Jul 21 '22 at 18:13