Skip to content

Instantly share code, notes, and snippets.

@aackerman
Last active August 29, 2015 14:23
Show Gist options
  • Save aackerman/87f1a9da4a5fe2eb54ee to your computer and use it in GitHub Desktop.
Save aackerman/87f1a9da4a5fe2eb54ee to your computer and use it in GitHub Desktop.
Auto-unmounting React components in Jasmine tests
import React from 'react';
import {mountNodes} from 'testing/render_test_utils';
// setup afterEach method to unmount all mounted nodes
jasmine.getEnv().topSuite().afterEach({
fn: () => {
while ( mountNodes.length ) {
let mountNode = mountNodes.pop();
React.unmountComponentAtNode(mountNode);
}
}
});
import React from 'react';
let mountNodes = [];
let render = (component, mountNode) => {
if (mountNode === undefined) throw new Error('mountNode was undefined');
mountNodes.push(mountNode);
return React.render(component, mountNode);
};
let renderIntoDocument = (component) => {
let mountNode = document.createElement('div');
return render(component, mountNode);
};
export default {
// expose a reference to allow testing utils to cleanup
mountNodes,
renderIntoDocument,
render
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment