I have a simple test file that is almost identical to the file used when creating the reaction-application:
App.test.js
import React from 'react';
import ReactDOM from 'react-dom';
import { App } from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
When I start yarn test
, I keep getting this error message:
Invariant Violation: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a <Provider>, or explicitly pass "st
ore" as a prop to "Connect(App)".
I tried to do a separate export in my App.js file and import a non-Redux component, but I still can't get this to work. Here are my other two files:
App.js
import React, { Component } from 'react';
import axios from 'axios';
import { connect } from 'react-redux';
import TableList from './containers/table_list';
import Header from './components/header';
import Footer from './components/footer';
import './style/App.css';
// use named export for unconnected component (for tests)
export class App extends Component {
constructor(props) {
super(props);
this.state = {
recentUsers: [],
allTimeUsers: []
}
}
componentWillMount() {
axios.all([this.fetchRecentUsers(), this.fetchAllTimeUsers()])
.then(axios.spread((recentUsers, allTimeUsers) => {
this.setState({ recentUsers: recentUsers.data, allTimeUsers: allTimeUsers.data });
}))
.catch((error) => {
console.log(error)
});
}
fetchRecentUsers() {
return axios.get(RECENT);
}
fetchAllTimeUsers() {
return axios.get(ALLTIME);
}
render() {
return (
<div>
<Header />
<div className="container">
<TableList users={this.state.recentUsers} />
</div>
<Footer />
</div>
)
}
}
const mapStateToProps = state => (
{ recentUsers: state.recentUsers, allTimeUsers: state.allTimeUsers }
)
// use default export for the connected component (for app)
export default connect(mapStateToProps)(App);
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from './reducers/index';
import App from './App';
import './style/index.css';
const store = createStore(rootReducer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));
What am I seeing here? The application works properly on its own, but I can’t understand what my life is, why the test fails.
source
share