当前位置 : 主页 > 网页制作 > React >

reactjs – JestJS – > Invariant Violation:无法在“Connect(Portfolio)”的上下文或道具

来源:互联网 收集:自由互联 发布时间:2021-06-15
完整的错误消息: Invariant Violation: Could not find “store” in either the context or props of “Connect(Portfolio)”. Either wrap the root component in a , or explicitly pass “store” as a prop to “Connect(Portfolio)”.
完整的错误消息:

Invariant Violation: Could not find “store” in either the context or props of “Connect(Portfolio)”. Either wrap the root component in a , or explicitly pass “store” as a prop to “Connect(Portfolio)”.

我不确定为什么我在我的Jest测试中收到此错误,因为我的应用程序正在运行,我可以通过调度操作更改我的状态.

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, applyMiddleware, compose } from 'redux'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
import App from './App'

const element = document.getElementById('coinhover');

const store = createStore(reducer, compose(
    applyMiddleware(thunk),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
));

ReactDOM.render(
    <Provider store={ store }>
        <App />
    </Provider>, element);

组合组件

import React from 'react'
import { connect } from 'react-redux'
import SocialMediaFooter from '../common/SocialMediaFooter'
import AssetsTable from '../assetsTable/AssetsTable'
import local_coins from '../../coins.json'
import * as api from '../../services/api'

const mapStateToProps = ({ portfolio }) => ({
    portfolio
});

let localCoins = local_coins;

class Portfolio extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            loading: true,
            assets: props.portfolio,
            total: 0
        };
    }

    componentDidMount() {
        this.setState({ loading: false });
    }

    render() {
        const assets = this.state.assets;
        const total  = this.state.total;

        return (
            <div className="app-bg">
                <section className="portfolio">
                    <header>
                        <h1><span className="plus">+</span>COINHOVER</h1>
                        <h2>Watch your cryptocurrency asset balances in once place.</h2>
                        <em className="num">${ total }</em>
                    </header>
                    { this.state.loading ? (
                        <div className="loading">
                            <div className="loader"></div>
                            <span>Loading coin data...</span>
                        </div>
                    ) : (
                        <AssetsTable assets={ assets }/>
                    )}
                    <SocialMediaFooter />
                </section>
            </div>
        )
    }
}

export default connect(mapStateToProps, null)(Portfolio)
根据错误消息,您需要确保连接组件的测试可以实际访问商店实例.因此,在您的测试代码中,您还应该使用< Provider store = {store}>< ConnectedPortfolio />< / Provider>或< ConnectedPortfolio store = {store} />.或者,您可以单独导出普通的Portfolio组件,并测试该组件,而不是连接的版本.

有关详细信息,请参阅Redux docs on testing,以及React/Redux links list中Redux testing approaches的文章.

网友评论