You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this component that I want to test:
`
import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
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";
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?
The text was updated successfully, but these errors were encountered:
There is a more comprehensive guide on their site, but basically you can wrap your component in <Provide/> and pass the store mocket with redux-mock-store to it.
You can always create a dumb component which you will use in you connected component. This will duplicate a lot of the functionality from the connected component so I don't see why you would do that.
Of course, there might be other ways of doing this, so please answer so we all learn a bit more :)
Just as a quick followup, what I usually do is a combination of the first two choices:
I export both the connected and unconnected components. In tests I use the unconnected component whenever I want to test something independent of redux (styling, default rendering et al).
When testing logic I use redux-mock-store. Besides injecting the store it allows for beautiful things like checking which actions were dispatched or dispatching actions on your own:
// Dispatch the action
store.dispatch(addTodo());
// Test if your store dispatched the expected actions
const actions = store.getActions();
You can then check the dispatched actions with the actions which were expected to be dispatched @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";
};
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 = { };
} );`
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?
The text was updated successfully, but these errors were encountered: