Skip to content

Testing react component with enzyme that are wrapped with Redux connect #21

Description

@danielmocan

I have this component that I want to test:
`
import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";

const CheckoutHeader = ( { newTitle } ) => {
const title = newTitle ? "First Title" : "Some title";

return (
    <header className="header-checkout">
        <div className="logo-info container">
            <div className="pull-left">
                    <span className=""/>
            </div>
            <div className="pull-right visible-md">
                <p className="claim"><Link to="/">{ title }</Link></p>
            </div>
        </div>
    </header>
);

};

const mapStateToProps = ( state ) => ( {
newTitle: state.general.title
} );

export default connect( mapStateToProps )( CheckoutHeader );
`

In order to test it with shallow rendering in need to send the store on the context

`import React from "react";
import expect from "expect.js";
import CheckoutHeader from "./checkoutHeader.react";
import { shallow } from "enzyme";
import configureStore from "../../../redux/store";

const store = configureStore( );

describe( "CheckoutHeader", ( ) => {
const props = { };

it( "renders without exploding", ( ) => {
    expect( shallow( <CheckoutHeader { ...props } />, { context: { store } } ).length ).to.equal( 1 );
} );

} );`

I order not to sent the store on the context I will have to do in the component 2 exports ( I would not like to do that )
export default CheckoutHeader;
export { CheckoutHeader };

and import the component in the test like this:
import { CheckoutHeader } from "./checkoutHeader.react";

How to you guys test components that are connected with redux ?
Is there a third option to my problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions