![baby mockshop baby mockshop](https://cdn-img.prettylittlething.com/9/3/b/8/93b892ec395ece3988671f11db276cb7f4bd7dfe_cmv2598_5.jpg)
But what about mapStateToProps? This can be exported and tested in isolation as a pure function if desired.And/or just search for the wrapped component: withStyles(Header). This is where an enhanced shallow function really shines in testing. While testing the redux wrapped (connected) HOC is not recommended, the reality is you will often to search for the existence of components wrapped with connect, withStyles etc.Remember how Eventing works in a proper React setup. You should be using state to store the current state of a given element element and base your rendering on that. onChange) matches the prop or state you pass in manually to your test. a checkbox)-just shallow-render your Component, and assert that appropriate attribute (i.e. There’s no need to test that React will trigger a “change” event when a element is clicked (i.e. Here’s a somewhat complicated async action creator we would like to test that does one or two factor authentication:Įxpect(reducer(undefined, ) I’ve found it preferable to simply use a mockServiceCreator function with a suite of body fixtures. You can also use fetch-mock to mock the HTTP requests, but that might be overkill. If needed, you can apply the middleware to said store using redux-mock-store. So unit tests should only know about actions/events and state.įor async action creators using Redux Thunk (or other middleware), mock the (minimally) required Redux store for testing. Actions & Async Action CreatorsĪll we care about here is that the correct action creator was called and it returned the right action.
![baby mockshop baby mockshop](https://cdn.shopify.com/s/files/1/0813/0713/products/image_ed226c3a-4ee5-4b7d-9edc-34822293e9bf_large.jpg)
This article assumes you are already happy and comfortable using React, Redux, and know the basic testing methods from Enzyme. What follows is a comprehensive set of best practices I’ve identified for circumspect React/Redux Unit Testing. I’ve become a huge fan and promoter of the practice, which has led me to examine and identify strategies to avoid interdependence, support common set-up and teardown logic, and bypass mucking about with implementation details. My work on a recent project has followed a very by-the-book Test-Driven Development (TDD) approach.