Skip to content

Instantly share code, notes, and snippets.

@johnnolan
Last active February 11, 2019 09:19
Show Gist options
  • Save johnnolan/ced7a366454147fb01d1319e53a99aab to your computer and use it in GitHub Desktop.
Save johnnolan/ced7a366454147fb01d1319e53a99aab to your computer and use it in GitHub Desktop.
If you want to test a Connected Component within a Provider while using Enzyme's mount and set its props to test how it renders by using something like toMatchSnapshot then you can set the props like so.
import React from "react";
import { mount } from 'enzyme';
import renderer from 'react-test-renderer'
import { Provider } from 'react-redux'
describe("Given you have a component that is wrapped in a Redux store and mount it, then you want to update the props of the child component", () => {
it('renders correctly', () => {
const mockStore = configureStore();
let store = mockStore(initialState)
let wrapper = mount(<Provider store={ store }><ConnectedComponent /></Provider>)
wrapper.setProps(
{
children: React.cloneElement(
wrapper.props().children, {
propOne: acceptedStates[0],
propTwo: []
}
)
}
);
const tree = renderer
.create(wrapper).toJSON();
expect(tree).toMatchSnapshot();
})
});
// Answer found here https://github.com/airbnb/enzyme/issues/1384#issuecomment-347150970
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment