I usually use jest.fn()
to define mock functions that need to be inspected for the number of calls. For async action creators using redux-thunk, I usually mock the dispatch and look for how many types it was called and what arguments were passed to it. To test functions passed as props to components, I use jest.fn()
to mock them and inject them as props; then I check whether they're called properly from within the component. It would be helpful if your question was more specific as to what type of thing you are trying to test? Otherwise, don't use something you don't need
If your component requires some props, you definitely have to pass them when you mount. Make an object like
const props = { ... };
with all the necessary properties, be they strings, numbers, arrays, or object.
For functions, you can create variables with mock functions:
const someFunction = jest.fn();
and then include it in your props
object:
const props = { ..., someFunction };
Then, mount the component like <Component {...props} />
. I recommend using shallow since nested components should be tested on their own, in a separate set of tests.
If you're using something like Redux, you can just export the component without being connected. Write the following, in addition to the export to the decorated component:
export default connect(...)(YourComponent);
// For testing purposes:
export { YourComponent };
Then you can define whatever is necessary from mapDispatchToProps
in your props
object I mentioned above. Also make sure whatever functions are being called are actually being passed to props. I once was testing some functions and realized they're component method functions and were not passed via props. In that case, use wrapper.instance.someMethod()
to call/reference them.