Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test components which are props of a shallow rendered component #343

Closed
mattkauffman23 opened this issue Apr 22, 2016 · 3 comments
Closed

Comments

@mattkauffman23
Copy link

mattkauffman23 commented Apr 22, 2016

I'm testing a component with a structure like this:

<ComponentA foo={<ComponentB bar={something} />

I want to verify that ComponentA.foo is a ComponentB and that its props are set correctly.

My test looks something like this:

const a = shallow(<ComponentA {...props} />)
const b = a.prop('foo')
expect(b.is(ComponentB)).to.be.true
// etc...

This doesn't work because b isn't wrapped in a shallow render, so I can't use shallow's API to test stuff. b appears to be a react component. I tried wrapping it in a shallow myself like this:

const wrappedB = shallow(b)

But that was problematic because ComponentB is actually a redux connected component and attempting to render it fails since the store isn't available and if at all possible I don't want to mock that out.

How can I handle testing components with this structure in a reasonable way? Is there something like shallow.find which can select a prop for me that is still wrapped by shallow?

@ljharb
Copy link
Member

ljharb commented Apr 24, 2016

If shallow-rendering it with const wrappedB = shallow(b) fails, then so too would anything shallow itself would do to wrap it.

@aweary
Copy link
Collaborator

aweary commented Apr 28, 2016

One bit of advice I can give with redux connected components is to also export your base class as well as your connected component and import that directly instead.

export class BaseClass extends Component {
// ...
}

export default connect()(BaseClass)

Then you can just do

import {BaseClass} from './components/BaseClass'

and just mock the props that redux provides.

@blainekasten
Copy link
Contributor

#472

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants