treznik treznik - 2 months ago 53
React JSX Question

Unmount / destroy Component in jsdom test

Is there a way to unmount and garbage collect a React Component that was mounted using

TestUtils.renderIntoDocument
inside a jsdom test?

I'm trying to test something that happens on
componentWillUnmount
and
TestUtils.renderIntoDocument
doesn't return any DOM node to call
React. unmountComponentAtNode
on.

Answer

No, but you can simply use renderComponent manually:

var container = document.createElement('div');
React.renderComponent(<Component />, container);
// ...
React.unmountComponentAtNode(container);

This is exactly what ReactTestUtils does anyway, so there's no reason not to do it this way if you need a reference to the container.