Skip to content

Commit

Permalink
fix: call createPortal() on null tartget (#255)
Browse files Browse the repository at this point in the history
* fix: call createPortal() on null target

fixes #204

* Add tests

---------

Co-authored-by: Ryan Seddon <[email protected]>
  • Loading branch information
deathemperor and ryanseddon authored May 24, 2024
1 parent 1b44f0a commit 759ccf1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Frame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class Frame extends Component {

const mountTarget = this.getMountTarget();

if (!mountTarget) {
return null;
}

return [
ReactDOM.createPortal(this.props.head, this.getDoc().head),
ReactDOM.createPortal(contents, mountTarget)
Expand Down
17 changes: 17 additions & 0 deletions test/Frame.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,23 @@ describe('The Frame Component', () => {
);
});

it("should render null when `this.getMountTarget()` can't resolve", done => {
const didMount = sinon.spy();
const getMountTarget = sinon
.stub(Frame.prototype, 'getMountTarget')
.returns(null);

div = document.body.appendChild(document.createElement('div'));

ReactDOM.render(<Frame contentDidMount={didMount} />, div);

setTimeout(() => {
expect(didMount.callCount).to.equal(0);
done();
getMountTarget.restore();
}, 100);
});

it('should not error when parent components are reused', done => {
div = document.body.appendChild(document.createElement('div'));

Expand Down

0 comments on commit 759ccf1

Please sign in to comment.