bruderstein / unexpected-react

Plugin for http://unexpected.js.org to enable testing the full React virtual DOM, and also the shallow renderer
http://bruderstein.github.io/unexpected-react
MIT License
186 stars 19 forks source link

Support lifecycle methods #46

Open albertfdp opened 7 years ago

albertfdp commented 7 years ago

Hi!

I have some piece of code I'd like to test, however it's on componentWillReceiveProps. Currently, there doesn't seem to be a way to test this using unexpected-react.

A solution is to re-render the same component on the same DOM node, which React will re-render instead of remount. Example

I think adding this assertion could probably do the job: <RenderedReactElement> when receiving new props <object> <assertion>

What do you think? Thanks!

bruderstein commented 7 years ago

There are patterns to do this with unexpected-react now. You can render the component yourself (without using when [deeply] rendered), then render the component again as you said with new props. Then use the renderer or component instance in the assertion.

const renderer = TestUtils.createRenderer()
renderer.render(<MyComponent prop="first" />)
renderer.render(<MyComponent prop="second" />)
expect(renderer, 'to have rendered', ...)

We can try and add this new assertion, as I think it would be quite useful as it means you can use the when rendered and similar assertions, but just wanted to point out it can be done :)

On Tue, 1 Aug 2017, 22:50 Albert Fernández, notifications@github.com wrote:

Hi!

I have some piece of code I'd like to test, however it's on componentWillReceiveProps. Currently, there doesn't seem to be a way to test this using unexpected-react.

A solution is to re-render the same component on the same DOM node, which React will re-render instead of remount. Example https://stackoverflow.com/a/30616091/1794589

I think adding this assertion could probably do the job:

when receiving new props What do you think? Thanks! — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub , or mute the thread .
albertfdp commented 7 years ago

Nice, I didn't know it was as easy. The example I pointed was a lot more boilerplate.

I think it would be quite useful to add, testing these re-render lifecycles should have first-level support. Happy to add to do a PR for this if you can point me a bit on where to start.

Thanks!