Javascript 无法持久存储连接的组件-React Redux测试

Javascript 无法持久存储连接的组件-React Redux测试,javascript,reactjs,redux,tdd,react-redux,Javascript,Reactjs,Redux,Tdd,React Redux,我不明白这为什么不起作用: -spec.js it.only('passes props to children', () => { const state = { company: { name: 'Company A' } }, store = fakeStore(state), container = <HomePageContainer store={store} />

我不明白这为什么不起作用:

-spec.js

it.only('passes props to children', () => {
    const state = {
        company: {
            name: 'Company A'
        }
    },
        store = fakeStore(state),
        container = <HomePageContainer store={store} />;

    const homePageContainer = shallow(container),
        homePage = homePageContainer.find(HomePage);

    expect(homePage.props.company.name).to.equal(state.company.name)
});

const fakeStore = state => {
    return {
        default: () => {},
        subscribe: () => {},
        dispatch: () => {},
        getState: () => { return { state };
        },
    };
};
import React from 'react';
import { connect } from 'react-redux'
import HomePage from '../../client/components/HomePage';

export const mapStateToProps = state => {
    company: state.company
}

export { HomePage }
export default connect(mapStateToProps)(HomePage);
import React, { Component, PropTypes } from 'react';

export default class HomePage extends Component {
    render(){
        return (
            <div className='homepage'>
                {/*{this.props.company.name}*/}
            </div>
        )
    }
}
HomePage.js

it.only('passes props to children', () => {
    const state = {
        company: {
            name: 'Company A'
        }
    },
        store = fakeStore(state),
        container = <HomePageContainer store={store} />;

    const homePageContainer = shallow(container),
        homePage = homePageContainer.find(HomePage);

    expect(homePage.props.company.name).to.equal(state.company.name)
});

const fakeStore = state => {
    return {
        default: () => {},
        subscribe: () => {},
        dispatch: () => {},
        getState: () => { return { state };
        },
    };
};
import React from 'react';
import { connect } from 'react-redux'
import HomePage from '../../client/components/HomePage';

export const mapStateToProps = state => {
    company: state.company
}

export { HomePage }
export default connect(mapStateToProps)(HomePage);
import React, { Component, PropTypes } from 'react';

export default class HomePage extends Component {
    render(){
        return (
            <div className='homepage'>
                {/*{this.props.company.name}*/}
            </div>
        )
    }
}
当assert试图读取
expect(homePage.props.company.name)

我注意到,当在mapStateToProps中放置断点时,出于某种原因,它仍然没有从存储中拾取状态对象:

我知道你可以通过道具传递商店…如果上下文中没有任何内容,connect()将能够通过商店道具找到它。例如,在另一个测试套件中,该测试通过得很好:

it('shallow render container and dive into child', () => {
    const container = shallow(<ExampleContainer store={fakeStore({})}/>);

    expect(container.find(Example).dive().text()).to.equal('Andy');
});
it('浅渲染容器并深入子对象',()=>{
常量容器=浅();
expect(container.find(Example.dive().text()).to.equal('Andy');
});
问题 在这一行,您将商店作为道具传递到

更新的
MapStateTops

export const mapStateToProps = state => ({
    company: state.company
});

发现问题真的是我的帮手

这是在传递一个包含属性“state”的对象。不是我想要的。这意味着mapStateToProps必须使用state.state.somePropName引用这些道具

const fakeStore = (state) => {
    return {
            default: () => {},
            subscribe: () => {},
            dispatch: () => {},
            getState: () => { return { state };
        },
    };
};
将其更改为此,现在mapStateToProps工作正常,它可以使用道具作为对象文本的根级别访问它,我从测试中传入的对象:

const fakeStore=(州)=>({


我很确定,如果您不使用Provider(或在Ezyme的选项中设置上下文),那么connect会直接从道具中获取它…?我还尝试过在没有luckNo的情况下使用,
connect
取决于通过上下文传递的存储。仍然不工作:store=fakeStore(state),container=;const homePageContainer=shallow(容器,{context:{store}),我不同意你必须在测试中通过上下文。你可以-,这是一种方法,但我之前已经让它通过直板道具,在几秒钟后看到我的更新,我将在这里发布。所以非常确定没有上下文的直板道具应该是好的,我甚至会收到酶错误告诉我。所以这就是为什么这是如此奇怪,这一次在这里其他测试套件不起作用…我发现了问题。要访问道具,您可以使用
wrapper.props()
获取道具对象。我将更新答案。
    default: () => {},
    subscribe: () => {},
    dispatch: () => {},
    getState: () => ({ ...state })
});