Skip to content

[fixed] Allow ReactDOM.createPortal to be mocked in tests #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions specs/Modal.testability.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env mocha */
import ReactDOM from "react-dom";
import sinon from "sinon";
import { mcontent, renderModal, emptyDOM } from "./helper";

export default () => {
afterEach("cleaned up all rendered modals", emptyDOM);

it("renders as expected, initially", () => {
const modal = renderModal({ isOpen: true }, "hello");
mcontent(modal).should.be.ok();
});

it("allows ReactDOM.createPortal to be overridden in real-time", () => {
const createPortalSpy = sinon.spy(ReactDOM, "createPortal");
renderModal({ isOpen: true }, "hello");
createPortalSpy.called.should.be.ok();
ReactDOM.createPortal.restore();
});
};
2 changes: 2 additions & 0 deletions specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import ModalState from "./Modal.spec";
import ModalEvents from "./Modal.events.spec";
import ModalStyle from "./Modal.style.spec";
import ModalHelpers from "./Modal.helpers.spec";
import ModalTestability from "./Modal.testability.spec";

describe("State", ModalState);
describe("Style", ModalStyle);
describe("Events", ModalEvents);
describe("Helpers", ModalHelpers);
describe("Testability", ModalTestability);
10 changes: 7 additions & 3 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export const portalClassName = "ReactModalPortal";
export const bodyOpenClassName = "ReactModal__Body--open";

const isReact16 = ReactDOM.createPortal !== undefined;
const createPortal = isReact16
? ReactDOM.createPortal
: ReactDOM.unstable_renderSubtreeIntoContainer;

const getCreatePortal = () =>
isReact16
? ReactDOM.createPortal
: ReactDOM.unstable_renderSubtreeIntoContainer;

function getParentElement(parentSelector) {
return parentSelector();
Expand Down Expand Up @@ -180,6 +182,7 @@ class Modal extends Component {
};

renderPortal = props => {
const createPortal = getCreatePortal();
const portal = createPortal(
this,
<ModalPortal defaultStyles={Modal.defaultStyles} {...props} />,
Expand All @@ -197,6 +200,7 @@ class Modal extends Component {
this.node = document.createElement("div");
}

const createPortal = getCreatePortal();
return createPortal(
<ModalPortal
ref={this.portalRef}
Expand Down