score:23
Wrap your component in <Provider>
when testing. It’s up to you whether to supply a real store or a mock with { dispatch, getState, subscribe }
to it. Wrapping the outermost component in <Provider store={store}>
will also make the store available to the child components at any level of nesting—just like in the app itself.
const store = createStore(reducer) // can also be a mock
ReactTestUtils.renderIntoDocument(
<Provider store={store}>
<OuterContainer />
</Provider>
)
score:0
Another approach is to export both the component to be connected and the container. The container as default, of course.
export const Comp = (props) => (<p>Whatever</p>)
export default connect(...)(Comp)
Hence, you can unit test Comp
.
score:0
Not sure if this is what your problem is, but I'm sure this will probably help a few people out there looking at this feed.
I had the same error and it was a simple fix:
I had forgotten to pass my component my store object in my entry file (using webpack).
I just added an attribute to the Root component "store={store}" see below:
document.addEventListener("DOMContentLoaded", () => {
const store = configureStore();
ReactDOM.render(<Root store={store} />,
document.getElementById('content'));
});
This was my root file code for reference as well:
import React from 'react';
import { Provider } from 'react-redux';
import App from './app';
const Root = ({ store }) => (
<Provider store={ store }>
<App />
</Provider>
);
export default Root;
Hope that helps someone!
score:0
Mock the Provider
component to return the child component.
Add this before describe()
.
jest.mock('Provider', () => ({children}) => children);
Source: stackoverflow.com
Related Query
- How to test a component with a nested container with React and Redux?
- How to test a React component that has Router, Redux and two HOCs... with Jest and Enzyme?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to test React Router params with Redux and enzyme
- How to test if a React component contains another component with Tape and Enzyme?
- How to test redux state update with react testing library and jest
- How to test with jest and typescript with types a basic react function component
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- How to test a React component that update over time with Jest and Enzyme?
- How to unit test the output of a React component method in a React Router / Redux app with Enzyme
- Jest with React - How do you render an entire component and test it?
- How To Unit Test React Component With react-intl, react-router-dom v4 and TypeScript
- How to test React component with children using jest and enzyme?
- How to mock a nested component import in react js and test using react testing library
- react redux how to trigger container function with action coming from non react component
- how to test component while passing boolean value with react testing library and jest
- Difference between component and container in react redux
- How to test a className with the Jest and React testing library
- How to mock React component methods with jest and enzyme
- How to test style for a React component attribute with Enzyme
- React & Jest, how to test changing state and checking for another component
- How to test a React component with RouteComponentProps?
- How to make react router work with static assets, html5 mode, history API and nested routes?
- How to test decorated React component with shallow rendering
- How to test whether a React Component contains another component with jest?
- How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined
- How to test component callback invoked by child component callback in React with Enzyme?
- How to test snapshots with Jest and new React lazy 16.6 API
- How to test Material UI Select Component with React Testing Library
- How to test the state of a functional component in React with Jest (No Enzyme)
More Query from same tag
- How change object value array to object value array object in react
- Re-rending using Setstate
- Function not getting all documents in a collection
- Esri maps with React and Typescript
- React Deleting checkbox item from list, strange behavior
- Algorithm to generate table from array?
- How to refetch queries from sibling component with react-query
- Use imported variable as a dinamic prop in React
- Can't get parameters from url with Redux ownProps
- Switch case return multiple component
- ReactJS dont take the css styles
- Why can I not open a React web app in a browser through an ingress when deploying it in a Kubernetes cluster
- SQL String Parsing with Hashtags
- delay in updating setState for an array React
- Mongodb updated password is not saved into the database
- Editing array in state adds a new element instead of updating the existing (react JS)
- "Options" of column chart not getting applied in react .Also I need to set width and height to 100% of div
- How can I run rails server with yarn start?
- objects sent to react state looping issue
- Netlify deployment of Gatsby/Wordpress site failing
- Set ID's If Found in Array in React
- Jest - mock function runs but .toHaveBeenCalledTimes() stays 0
- I received products data from fakestoreapi to the console but not receiving to the browser
- ReactJS: this is reserved word
- How to add spinner while loading data in react.js?
- Web app taking too long to determine whether user is authenticated or not
- mui Theme color not applying
- How to troubleshoot my JSX/CSS so that this mess doesn't happen?
- How to populate <SelectInput> choices with already fetched data
- Where do I place the ReCatptcha for firebase phone number login?