unexpected-react icon indicating copy to clipboard operation
unexpected-react copied to clipboard

Support lifecycle methods

Open albertfdp opened this issue 8 years ago • 2 comments

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!

albertfdp avatar Aug 01 '17 20:08 albertfdp

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, [email protected] 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: <RenderedReactElement> 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 https://github.com/bruderstein/unexpected-react/issues/46, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFmRGO7xy0z4jpU7xafKkGivP3exjOPks5sT4-ggaJpZM4OqRsz .

bruderstein avatar Aug 02 '17 09:08 bruderstein

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!

albertfdp avatar Aug 02 '17 09:08 albertfdp