I am writing a react component which will load a URL within an iframe, then when the iframe's onLoad event fires it will call contentWindow.postMessage(). I'd like to prove this functionality using Jest, Enzyme, and JSDOM.
My component wraps react-iframe and looks pretty simple:
export class FilteredIframe extends React.PureComponent<FilteredIframeProps> {
onload = (e:Window) => {
console.log("ONLOAD CALLED");
if (this.props.filters) {
e.postMessage(this.props.filters, this.props.url);
}
}
render() {
return (<Iframe url={this.props.url}
display="initial"
position="static"
onLoad={this.onload}
/>);
}
}
I'm trying to figure out how to get enzyme/jsdom to test this, but I'm failing:
test("Posts message once the frame has loaded", async () => {
const payLoad = { data: "data" };
const result = mount(<FilteredIframe url="https:///www.bing.com" filters={payLoad}/>);
})
When running this in jest, I never see the "ONLOAD CALLED" message in the console. Is there some sort of special thing I need to do for jsdom or enzyme to make it actually call onLoad?