Skip to content

Instantly share code, notes, and snippets.

@TunedMystic
Forked from danmakenoise/test.js
Created November 27, 2018 07:00
Show Gist options
  • Save TunedMystic/fe2f8541e52b1ac8fea122af899b3fbe to your computer and use it in GitHub Desktop.
Save TunedMystic/fe2f8541e52b1ac8fea122af899b3fbe to your computer and use it in GitHub Desktop.
Unit Testing Children of React Context API Consumers with Enzyme
// Component.js
const Component = props => (
<MyContext.Consumer>
{(context) => (
<Foo
bar={props.bar}
baz={context.baz}
/>
)}
</MyContext.Consumer>
);
// Component.test.js
const outer = shallow(<Component bar="bar" />);
const Children = outer.props().children;
const wrapper = shallow(<Children baz="baz" />);
expect(wrapper.find(Foo)).toHaveLength(1);
expect(wrapper.find(Foo)).props().bar).toBe('bar');
expect(wrapper.find(Foo)).props().baz).toBe('baz');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment