score:1
Try to render your component like this:
render(
<Provider store={store}>
<ChooseIndex />
</Provider>
)
And pass the actual store you use in your app. In this way, you're testing the real logic that you'll use in production. You also don't care what actions get dispatched and what's in the state. You look at what gets rendered and interact with the UI—which is what matters in the end.
Separating the component from Redux and testing the two in isolation is against the whole point of react-testing-library
. You want to test your app as a real user would.
score:1
If you check out the writing tests section of the redux docs, there is an example of testing a connected component.
when you import it [A redux connected component], you're actually holding the wrapper component returned by connect(), and not the App component itself. If you want to test its interaction with Redux, this is good news: you can wrap it in a with a store created specifically for this unit test. But sometimes you want to test just the rendering of the component, without a Redux store.
In order to be able to test the App component itself without having to deal with the decorator, we recommend you to also export the undecorated component
As with most unit tests, you really want to be testing your components, and not that redux is working correctly. So the solution for you is to export both the component and the connected component, while only testing the component itself, and providing whatever props redux is passing to your component.
import { connect } from 'react-redux'
// Use named export for unconnected component (for tests)
export class App extends Component {
/* ... */
}
// Use default export for the connected component (for app)
export default connect(mapStateToProps)(App)
Source: stackoverflow.com
Related Query
- Mocking Redux store when testing React components?
- Testing in React with preloadedState: redux store gets out of sync when having a manual import of store in a function
- When do I choose React state Vs Redux Store
- Nested components testing with Enzyme inside of React & Redux
- Cannot read property '.then' of undefined when testing async action creators with redux and react
- What is the necessity of "export default connect" when you are connecting your react component to redux store
- Where to store WebRTC streams when building React app with redux
- Access Redux store outside React when using next-redux-wrapper
- How to use the updated value in redux store immediately after the action is dispatched in react js functional components
- how to delay checking redux store when authenticating using Higher Order Components
- Redux store not being populated when mocking API call
- "TypeError: react.useContext is not a function" when mocking context Providers with React Testing Library
- How React Testing Library can assert children props and Redux store updates?
- Error when testing react components with jest and css-modules
- React routes not automatically updating when nested under 'smart' Redux container components
- Redux with React - right way to share the store with components
- React Redux "Cannot read property 'status' of undefined" error when trying to access store variable
- Redux in React App duplicating in store when it should not be
- react / redux toolkit useSelector not updating when store state changed
- How to resolve "Cannot find module" Error when testing react components with mocha
- Error when testing a React Parent that contains a Child React Redux Component using Enzyme Mount
- Render array of objects from Redux store as React components
- How to get the data from Redux store using class components in React
- Unit testing React components that access a global store
- I can't access the redux store properly in react components
- react redux work unexpected when store state change
- React component not re-render when a store variable is dispatched in redux
- How to get the value of redux store in other components on typescript react hooks project
- Mobx state is lost when other React components access the store
- Errors when mocking firebase within a React app using Jest and React Testing Library
More Query from same tag
- Change route without reloading page with Next.js
- React forwardRef: ref and other props
- Eventhandling in combination with the .map function
- Adding dynamic meta tags to a React static site, created using StaticSiteGeneratorPlugin
- How to use a ternary with a boolean in TypeScript?
- Use GraphQL data in gatsby-browser?
- Redux action not firing - no errors
- how to change default port in react app and define specific port to it
- How to structure Department-Employee data
- How can I make state updating asynchronous in React+Redux
- TypeError: fs.existsSync is not a function
- cross domain at axios
- React update state from the axios response
- Get rid of warning when dynamically rendering React styled component
- How To Pass Props To Function Component use react-router-dom v6
- React Router Dom doesn't recognize url with query params
- How to call a class component using onclick event handler in react.js
- Order Array of objects by parentId
- Unable to type password in input box
- React not rendering array after fetching data
- How to pass parameter in a function on onClick event
- Bootstrap navbar isn't refreshing in react
- React Router - Route not working on Refresh in webpack production build
- History object issue with React-Router
- How to access more than one element of Dom in react using React.createRef()
- Communicating between nested child components in React.js
- How to do multiple optional params with react-router?
- How to make Reactjs not rerender certain components
- Removing d3-flame-graph when loading new data
- How to set property asynchronously using useEffect for later use in function within a React component?