Skip to content

Commit

Permalink
Move ReactDOM.Server/TestUtils to ReactDOMServer/ReactDOMTestUtils (#803
Browse files Browse the repository at this point in the history
)
  • Loading branch information
davesnx authored Oct 31, 2023
1 parent 18a8fa2 commit 9e5a888
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 362 deletions.
10 changes: 5 additions & 5 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Welcome to the source of ReasonReact

We want to expose as minimum modules to the toplevel as possible, and at the same time want to map closely to the npm packages. So each module maps to a npm package, and each sub-module maps to a sub-module. For example: `react-dom` -> `ReactDOM` and `react-dom/server` -> `ReactDOM.Server`.
We want to expose the minimum amount of top-level modules possible, and at the same time want to map closely to the npm packages. So each module maps to a npm package, and each sub-module maps to a sub-module. For example: `react-dom` -> `ReactDOM` and `react-dom/server` -> `ReactDOMServer`.

Files overview:

Expand All @@ -10,12 +10,12 @@ Files overview:
- `React.Event`: bindings to React's custom events system
- `React.Context`: bindings to React's Context
- `ReactDOM`: bindings to ReactDOM
- `ReactDOM.Server`: bindings to ReactDOMServer
- `ReactDOM.Style`: bindings to create `style` objects
- `ReactDOM.TestUtils`: helpers for testing your components
- `ReactDOMServer`: bindings to ReactDOMServer
- `ReactDOMTestUtils`: helpers for testing your components

## Extra (not part of react)
- `ErrorBoundary`: component to catch errors within your component tree
- `ReasonReactErrorBoundary`: component to catch errors within your component tree
- `ReasonReactRouter`: a simple, yet fully featured router with minimal memory allocations

Eventually `React.ErrorBoundary` and `ReasonReactRouter` could live into their own packages, but for now they are part of ReasonReact (and we will keep them here for backwards compatibility).
Eventually `ReasonReactErrorBoundary` and `ReasonReactRouter` could live into their own packages, but for now they are part of ReasonReact (and we will keep them here for backwards compatibility).
230 changes: 0 additions & 230 deletions src/ReactDOM.re
Original file line number Diff line number Diff line change
Expand Up @@ -1547,233 +1547,3 @@ external jsxs: (string, domProps) => React.element = "jsxs";
[@mel.module "react/jsx-runtime"]
external jsxsKeyed: (string, domProps, ~key: string=?, unit) => React.element =
"jsxs";

module Server = {
[@mel.module "react-dom/server"]
external renderToString: React.element => string = "renderToString";

[@mel.module "react-dom/server"]
external renderToStaticMarkup: React.element => string =
"renderToStaticMarkup";

[@deriving abstract]
type options = {
[@mel.optional]
bootstrapScriptContent: option(string),
[@mel.optional]
bootstrapScripts: option(array(string)),
[@mel.optional]
bootstrapModules: option(array(string)),
[@mel.optional]
identifierPrefix: option(string),
[@mel.optional]
namespaceURI: option(string),
[@mel.optional]
nonce: option(string),
[@mel.optional]
onAllReady: option(unit => unit),
[@mel.optional]
onError: option(Js.Exn.t => unit),
[@mel.optional]
onShellReady: option(unit => unit),
[@mel.optional]
onShellError: option(Js.Exn.t => unit),
[@mel.optional]
progressiveChunkSize: option(int),
};

type pipeableStream = {
/* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */
pipe: Js.t({.}) => unit,
abort: unit => unit,
};

[@mel.module "react-dom/server"]
external renderToPipeableStream: (React.element, options) => pipeableStream =
"renderToPipeableStream";

let renderToPipeableStream =
(
~bootstrapScriptContent=?,
~bootstrapScripts=?,
~bootstrapModules=?,
~identifierPrefix=?,
~namespaceURI=?,
~nonce=?,
~onAllReady=?,
~onError=?,
~onShellReady=?,
~onShellError=?,
~progressiveChunkSize=?,
element,
) =>
renderToPipeableStream(
element,
options(
~bootstrapScriptContent?,
~bootstrapScripts?,
~bootstrapModules?,
~identifierPrefix?,
~namespaceURI?,
~nonce?,
~onAllReady?,
~onError?,
~onShellReady?,
~onShellError?,
~progressiveChunkSize?,
(),
),
);
};

module TestUtils = {
type undefined = Js.undefined(unit);

let undefined: undefined = Js.Undefined.empty;

[@mel.module "react-dom/test-utils"]
external reactAct: ((. unit) => undefined) => unit = "act";

let act: (unit => unit) => unit =
func => {
let reactFunc =
(.) => {
func();
undefined;
};
reactAct(reactFunc);
};

[@mel.module "react-dom/test-utils"]
external reactActAsync:
((. unit) => Js.Promise.t('a)) => Js.Promise.t(unit) =
"act";

let actAsync = func => {
let reactFunc =
(.) => {
func();
};
reactActAsync(reactFunc);
};

[@mel.module "react-dom/test-utils"]
external isElement: 'element => bool = "isElement";

[@mel.module "react-dom/test-utils"]
external isElementOfType: ('element, React.component('props)) => bool =
"isElementOfType";

[@mel.module "react-dom/test-utils"]
external isDOMComponent: 'element => bool = "isDOMComponent";

[@mel.module "react-dom/test-utils"]
external isCompositeComponent: 'element => bool = "isCompositeComponent";

[@mel.module "react-dom/test-utils"]
external isCompositeComponentWithType:
('element, React.component('props)) => bool =
"isCompositeComponentWithType";

module Simulate = {
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external click: Dom.element => unit = "click";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external clickWithEvent: (Dom.element, 'event) => unit = "click";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external change: Dom.element => unit = "change";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external blur: Dom.element => unit = "blur";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external changeWithEvent: (Dom.element, 'event) => unit = "change";
let changeWithValue = (element, value) => {
let event = {
"target": {
"value": value,
},
};
changeWithEvent(element, event);
};
let changeWithChecked = (element, value) => {
let event = {
"target": {
"checked": value,
},
};
changeWithEvent(element, event);
};
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external canPlay: Dom.element => unit = "canPlay";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external timeUpdate: Dom.element => unit = "timeUpdate";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external ended: Dom.element => unit = "ended";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external focus: Dom.element => unit = "focus";
};

external document: Dom.document = "document";

[@mel.send]
external querySelector: (Dom.element, string) => option(Dom.element) =
"querySelector";

[@mel.send]
external querySelectorAll:
(Dom.element, string) => Js.Array.array_like(Dom.element) =
"querySelectorAll";

[@mel.get] external textContent: Dom.element => string = "textContent";
[@mel.get] external body: Dom.document => option(Dom.element) = "body";
[@mel.send]
external createElement: (Dom.document, string) => Dom.element =
"createElement";
[@mel.send] external remove: Dom.element => unit = "remove";
[@mel.send]
external appendChild: (Dom.element, Dom.element) => Dom.element =
"appendChild";

let querySelectorAll = (element, string) => {
Js.Array.from(querySelectorAll(element, string));
};

module DOM = {
[@mel.return nullable] [@mel.get]
external value: Dom.element => option(string) = "value";

let findBySelector = (element, selector) =>
querySelector(element, selector);

let findByAllSelector = (element, selector) =>
querySelectorAll(element, selector);

let findBySelectorAndTextContent = (element, selector, content) =>
querySelectorAll(element, selector)
|> Array.find_opt(node => node->textContent === content);

let findBySelectorAndPartialTextContent = (element, selector, content) =>
querySelectorAll(element, selector)
|> Array.find_opt(node =>
node->textContent->Js.String2.includes(content)
);
};

let prepareContainer = (container: ref(option(Dom.element)), ()) => {
let containerElement = document->createElement("div");
let _: option(_) =
Option.map(
body => body->appendChild(containerElement),
document->body,
);
container := Some(containerElement);
};

let cleanupContainer = (container: ref(option(Dom.element)), ()) => {
let _: option(_) = Option.map(remove, container^);
container := None;
};

let getContainer = container => {
container.contents->Option.get;
};
};
121 changes: 0 additions & 121 deletions src/ReactDOM.rei
Original file line number Diff line number Diff line change
Expand Up @@ -433,64 +433,6 @@ module Style: {
"Object.assign";
};

module Server: {
[@mel.module "react-dom/server"]
external renderToString: React.element => string = "renderToString";

[@mel.module "react-dom/server"]
external renderToStaticMarkup: React.element => string =
"renderToStaticMarkup";

[@deriving abstract]
type options = {
[@mel.optional]
bootstrapScriptContent: option(string),
[@mel.optional]
bootstrapScripts: option(array(string)),
[@mel.optional]
bootstrapModules: option(array(string)),
[@mel.optional]
identifierPrefix: option(string),
[@mel.optional]
namespaceURI: option(string),
[@mel.optional]
nonce: option(string),
[@mel.optional]
onAllReady: option(unit => unit),
[@mel.optional]
onError: option(Js.Exn.t => unit),
[@mel.optional]
onShellReady: option(unit => unit),
[@mel.optional]
onShellError: option(Js.Exn.t => unit),
[@mel.optional]
progressiveChunkSize: option(int),
};

type pipeableStream = {
/* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */
pipe: Js.t({.}) => unit,
abort: unit => unit,
};

let renderToPipeableStream:
(
~bootstrapScriptContent: string=?,
~bootstrapScripts: array(string)=?,
~bootstrapModules: array(string)=?,
~identifierPrefix: string=?,
~namespaceURI: string=?,
~nonce: string=?,
~onAllReady: unit => unit=?,
~onError: Js.Exn.t => unit=?,
~onShellReady: unit => unit=?,
~onShellError: Js.Exn.t => unit=?,
~progressiveChunkSize: int=?,
React.element
) =>
pipeableStream;
};

[@mel.return nullable]
external querySelector: string => option(Dom.element) =
"document.querySelector";
Expand Down Expand Up @@ -1606,66 +1548,3 @@ external jsxs: (string, domProps) => React.element = "jsxs";
[@mel.module "react/jsx-runtime"]
external jsxsKeyed: (string, domProps, ~key: string=?, unit) => React.element =
"jsxs";

module TestUtils: {
let act: (unit => unit) => unit;

let actAsync: (unit => Js.Promise.t('a)) => Js.Promise.t(unit);

[@mel.module "react-dom/test-utils"]
external isElement: 'element => bool = "isElement";

[@mel.module "react-dom/test-utils"]
external isElementOfType: ('element, React.component('props)) => bool =
"isElementOfType";

[@mel.module "react-dom/test-utils"]
external isDOMComponent: 'element => bool = "isDOMComponent";

[@mel.module "react-dom/test-utils"]
external isCompositeComponent: 'element => bool = "isCompositeComponent";

[@mel.module "react-dom/test-utils"]
external isCompositeComponentWithType:
('element, React.component('props)) => bool =
"isCompositeComponentWithType";

module Simulate: {
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external click: Dom.element => unit = "click";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external clickWithEvent: (Dom.element, 'event) => unit = "click";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external change: Dom.element => unit = "change";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external blur: Dom.element => unit = "blur";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external changeWithEvent: (Dom.element, 'event) => unit = "change";
let changeWithValue: (Dom.element, string) => unit;
let changeWithChecked: (Dom.element, bool) => unit;
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external canPlay: Dom.element => unit = "canPlay";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external timeUpdate: Dom.element => unit = "timeUpdate";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external ended: Dom.element => unit = "ended";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
external focus: Dom.element => unit = "focus";
};

module DOM: {
[@mel.return nullable] [@mel.get]
external value: Dom.element => option(string) = "value";

let findBySelector: (Dom.element, string) => option(Dom.element);
let findByAllSelector: (Dom.element, string) => array(Dom.element);
let findBySelectorAndTextContent:
(Dom.element, string, string) => option(Dom.element);
let findBySelectorAndPartialTextContent:
(Dom.element, string, string) => option(Dom.element);
};

let prepareContainer: (Stdlib.ref(option(Dom.element)), unit) => unit;
let cleanupContainer: (Stdlib.ref(option(Dom.element)), unit) => unit;
let getContainer: Stdlib.ref(option(Dom.element)) => Dom.element;
};
Loading

0 comments on commit 9e5a888

Please sign in to comment.