Last active
August 29, 2015 14:23
-
-
Save aackerman/87f1a9da4a5fe2eb54ee to your computer and use it in GitHub Desktop.
Auto-unmounting React components in Jasmine tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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