在“ Connect(Signup)”的上下文或道具中找不到“ store”

时间:2018-10-09 16:39:42

标签: reactjs jestjs enzyme

我正在尝试进行酶测试,但遇到此错误,

  

永久违反:在上下文或以下环境中均找不到“存储”   “连接(注册)”的道具。可以将根组件包装在   ,或显式地将“商店”作为道具传递给“连接(注册)”。

我在应用程序中使用Redux,它在浏览器中运行良好,但是无法在酶和玩笑中运行测试。

下面是我的代码:

下面的测试文件是 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);
    ReactDOM.unmountComponentAtNode(div);
});

Signup.js

import React, { Component } from 'react';
import  NavBar from './subcomponents/NavBar'
import SignUpForm from './subcomponents/SignUpForm'
import { connect } from 'react-redux'
import {userSignUpRequest} from "./actions/signUpActions";

import PropTypes from 'prop-types';

class Signup extends Component {

    render() {

        const {userSignUpRequest} = this.props;

        return (
            <div>
                <NavBar/>
                <SignUpForm userSignUpRequest={userSignUpRequest}/>

            </div>

        );
    }
}

Signup.propTypes = {

    userSignUpRequest: PropTypes.func.isRequired
}

export default connect(null, { userSignUpRequest })(Signup);

SignUpForm.js

import React, {Component} from 'react';
import {Input, Icon, Row, Card, Button, ProgressBar, Col, Preloader} from 'react-materialize'
import  '../css/signup.css'
import PropTypes from 'prop-types';

class SignUpForm extends Component {

    constructor(props) {
        super(props);

        this.state = {

                username: '',
                email:'',
                password:'',
                confirm_password:'',
                visible: true


        }

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleChange(event) {

        const {name,value}  = event.target;
        this.setState({[name]: value});


    }

    handleSubmit(event) {
        event.preventDefault();
        console.log('my state ', this.state);
        this.setState({visible: true});

        if(this.state.password.trim() === this.state.confirm_password.trim()) {

             this.props.userSignUpRequest(this.state);


        }

        else{
            alert('No  they don\'t  match');
            this.setState({visible: true});
        }


    }


    componentDidMount() {

        console.log('Parent did mount.');
        document.getElementById('text_message').style.visibility = "hidden";
        this.setState({visible: false});



    }

    render() {
        const isVisible = this.state.visible;
        return (
            <div>
                <Card className="card-effects right">

                    {isVisible && <ProgressBar  id="progress_Bar" name="progress_Bar"/>}
                    <Row>
                        <label className="text-message" id="text_message" name="text_message"><i>text message</i></label>
                    </Row>

                    <form className="card-form-signup" onSubmit={this.handleSubmit}>
                        <Row>
                            <label className="signup-header"><b>Signup to Authors Haven</b></label>
                        </Row>
                        <Row>
                            <Input s={12} placeholder="Username" name="username" value={this.state.username} onChange={this.handleChange} validate>
                                <Icon className="icon-styles">account_box</Icon></Input>
                        </Row>
                        <Row>
                            <Input s={12} type='text' name="email" value={this.state.email}  onChange={this.handleChange} placeholder="Email"    validate><Icon className="green darken-4">email</Icon></Input>
                        </Row>
                        <Row>
                            <Input s={12} type='password'  name="password" placeholder="Password"  value={this.state.password} onChange={this.handleChange}  validate>
                                <Icon className="icon-styles">vpn_key</Icon></Input>
                        </Row>
                        <Row>
                            <Input s={12} type='password' name="confirm_password" placeholder="Confirm password" value={this.state.confirm_password} onChange={this.handleChange} validate>
                                <Icon className="icon-styles">vpn_key</Icon></Input>
                        </Row>
                        <Row>
                            <label >Already have an account ? </label>
                        </Row>

                        <Row>
                            <Button className='button-effects' type="submit" value="Submit" > Sign up </Button>
                        </Row>


                    </form>
                </Card>
            </div>

        );
    }

}

SignUpForm.propTypes = {

    userSignUpRequest: PropTypes.func.isRequired
}



export default SignUpForm;

1 个答案:

答案 0 :(得分:0)

在测试中,您仍然需要将组件安装在<Provider>内,以便App下游connect()的所有用法都可以正常工作。

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