If you're writing a unit test (and I assume that this is what you're doing), you should test the behavior, not the implementation details.

If I had to write a test for such component I'd use smth like that:

  • positive scenario: component renders an iframe with passed src
  • negative scenario: component renders null (depends on business logic) if no src passed in props

This test handles both: correct and incorrect behavior. See Defensive programming for more details.

If we're talking about the test implementation, I'd use Jest and toMatchSnapshot method to check the rendering result on both scenarios.


The problem is still actual. And the answer is that jsdom provides contentWindow/contentDocument only for attached iframe, and enzyme by default doesn't attach nodes. There is option for mount which directs enzyme to attach node:

el = document.createElement('div');
wrapper = mount(<MyReactNode />, { attachTo: el });

Related Query

More Query from same tag