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

Is it possible to unit test branches with shallow rendering? #95

Open
ghost opened this issue Jan 21, 2016 · 2 comments
Open

Is it possible to unit test branches with shallow rendering? #95

ghost opened this issue Jan 21, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 21, 2016

I'm very new to React so I may be missing something simple here ..

I have a component using the branch mixin that I would like to unit test in isolation, using enzyme for shallow rendering.

My first idea was to see if I could pass my component a Baobab tree during rendering, i.e.: shallowRender <MyBranchComponent tree={tree} />

However, this results in Warning: Failed Context Types: prop type 'tree' is invalid; it must be a Baobab tree and a test failure that I assume is because it can't find the tree (Cannot read property 'watch' of undefined).

The problem is that, with shallow rendering, if in the test I wrap <MyBranchComponent /> in a higher-order root component or <Root /> wrapper, it won't render the actual element I'm testing.

Is there a way to give a component with a branch mixin a tree without wrapping it? If not, does that make baobab-react incompatible with shallow rendering?

@Yomguithereal
Copy link
Owner

Hello @JoshuaRH. Generally, people tend to differentiate between "clever" and "dumb" components. "Clever" components, like the branched one, must have knowledge of the outside world and usually only serve as wrapper providing data to "dumb" one, that can work in isolation and can be generalized. Therefore, people usually only unit test "dumb" components.

If you still want to test the branched components, I must think a little on how this can be done. Let met check if react-redux (which actually works the same as this lib) does in this case.

@jemos
Copy link

jemos commented Feb 17, 2016

I'm also trying to understand how I could test my code that uses baobab trees and react. The way I was able to accomplish shallow testing was using enzyme.shallow and passing the context (because it supports that):

var AppTree = Baobab({});
var reactElem = shallow(<MyBaobabComponent/>, {context: {tree: AppTree}});
console.log(reactElem.debug());

Not sure if it helps..

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

No branches or pull requests

2 participants