Full code here: https://gist.github.com/js08/0ec3d70dfda76d7e9fb4
Hi,
in the end of your Index.js need to add this Code:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
///its your redux ex
import productReducer from './redux/reducer/admin/product/produt.reducer.js'
const rootReducer = combineReducers({
adminProduct: productReducer
})
const composeEnhancers = window._REDUX_DEVTOOLS_EXTENSION_COMPOSE_ || compose;
const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
const app = (
<Provider store={store}>
<BrowserRouter basename='/'>
<App />
</BrowserRouter >
</Provider>
);
ReactDOM.render(app, document.getElementById('root'));
It's pretty simple. You're trying to test the wrapper component generated by calling connect()(MyPlainComponent)
. That wrapper component expects to have access to a Redux store. Normally that store is available as context.store
, because at the top of your component hierarchy you'd have a <Provider store={myStore} />
. However, you're rendering your connected component by itself, with no store, so it's throwing an error.
You've got a few options:
<Provider>
around your connected component<MyConnectedComponent store={store} />
, as the connected component will also accept "store" as a propmapStateToProps
function, you can safely assume the connected version will work correctly.You probably want to read through the "Testing" page in the Redux docs: https://redux.js.org/recipes/writing-tests.
edit:
After actually seeing that you posted source, and re-reading the error message, the real problem is not with the SportsTopPane component. The problem is that you're trying to "fully" render SportsTopPane, which also renders all of its children, rather than doing a "shallow" render like you were in the first case. The line searchComponent = <SportsDatabase sportsWholeFramework="desktop" />;
is rendering a component that I assume is also connected, and therefore expects a store to be available in React's "context" feature.
At this point, you have two new options:
Overall, I would note that you might be trying to do too much in this one component and might want to consider breaking it into smaller pieces with less logic per component.
As the official docs of redux suggest, better to export the unconnected component as well.
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:
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)
Since the default export is still the decorated component, the import statement pictured above will work as before so you won't have to change your application code. However, you can now import the undecorated App components in your test file like this:
// Note the curly braces: grab the named export instead of default export
import { App } from './App'
And if you need both:
import ConnectedApp, { App } from './App'
In the app itself, you would still import it normally:
import App from './App'
You would only use the named export for tests.