Skip to content

Commit

Permalink
Merge branch '19' of github.com:/reasonml/reason-react into remove-ra…
Browse files Browse the repository at this point in the history
…ises-as-annotations

* '19' of github.com:/reasonml/reason-react:
  Enable ref as valid prop (#862)
  Replace react dom's testing library with ReactTestingLibrary (#859)
  Add deprecations on ReactDOMTestUtils
  Add uri comment back on action
  Update src/React.re
  Update src/React.re
  Update src/React.re
  Snapshot with lower {}
  • Loading branch information
davesnx committed Nov 25, 2024
2 parents d6a30a5 + 08f5e19 commit 9857fc3
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ format-check: ## Checks if format is correct
.PHONY: install
install: ## Update the package dependencies when new deps are added to dune-project
@opam install . --deps-only --with-test
# --force is needed because we are installing react@19 while other dependencies
# require react@18. It's a good workaround to bypass the npm validation error
# and test the rc versions of React
@npm install --force

.PHONY: init
Expand Down
2 changes: 1 addition & 1 deletion demo/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(alias melange-app)
(module_systems
(es6 mjs))
(libraries reason-react jest melange.belt melange.dom)
(libraries reason-react melange.belt melange.dom)
(runtime_deps index.html)
(preprocess
(pps melange.ppx reason-react-ppx)))
6 changes: 4 additions & 2 deletions ppx/reason_react_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,10 @@ let jsxMapper =
let expr = mapper#expression ctxt expr in
match expr.pexp_desc with
| Pexp_fun (Labelled "key", _, _, _) | Pexp_fun (Optional "key", _, _, _) ->
Location.raise_errorf ~loc:expr.pexp_loc
("~key cannot be accessed from the component props. Please set the key where the component is being used.")
raise
(Invalid_argument
"Key cannot be accessed inside of a component. Don't worry - you \
can always key a component from its parent!")
| Pexp_fun
( ((Optional label | Labelled label) as arg),
default,
Expand Down
10 changes: 9 additions & 1 deletion src/React.re
Original file line number Diff line number Diff line change
Expand Up @@ -885,16 +885,24 @@ external startTransition: ([@mel.uncurry] (unit => unit)) => unit =
external useDebugValue: ('value, ~format: 'value => string=?, unit) => unit =
"useDebugValue";

[@mel.module "react"]
external act: (unit => unit) => Js.Promise.t(unit) = "act";
[@mel.module "react"]
external actAsync: (unit => Js.Promise.t(unit)) => Js.Promise.t(unit) =
"act";

module Experimental = {
/* This module is used to bind to APIs for future versions of React. There is no guarantee of backwards compatibility or stability. */
/* https://react.dev/reference/react/use */
[@mel.module "react"] external usePromise: Js.Promise.t('a) => 'a = "use";
[@mel.module "react"] external useContext: Context.t('a) => 'a = "use";
/* https://react.dev/reference/react/useTransition */
[@mel.module "react"]
external useTransitionAsync:
unit => (bool, callbackAsync(callbackAsync(unit, unit), unit)) =
"useTransition";

/* https://es.react.dev/reference/react/useOptimistic */
/* https://react.dev/reference/react/useOptimistic */
[@mel.module "react"]
external useOptimistic:
('state, ('state, 'optimisticValue) => 'state) =>
Expand Down
6 changes: 6 additions & 0 deletions src/React.rei
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ external startTransition: ([@mel.uncurry] (unit => unit)) => unit =
external useTransition: unit => (bool, callback(callback(unit, unit), unit)) =
"useTransition";

[@mel.module "react"]
external act: (unit => unit) => Js.Promise.t(unit) = "act";
[@mel.module "react"]
external actAsync: (unit => Js.Promise.t(unit)) => Js.Promise.t(unit) =
"act";

module Experimental: {
/* This module is used to bind to APIs for future versions of React. There is no guarantee of backwards compatibility or stability. */
[@mel.module "react"] external usePromise: Js.Promise.t('a) => 'a = "use";
Expand Down
2 changes: 1 addition & 1 deletion src/ReactDOM.re
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ type domProps = {
[@mel.optional]
acceptCharset: option(string),
[@mel.optional]
action: option(string),
action: option(string), /* uri */
[@mel.optional]
allowFullScreen: option(bool),
[@mel.optional]
Expand Down
99 changes: 91 additions & 8 deletions src/ReactDOMTestUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let undefined: undefined = Js.Undefined.empty;
[@mel.module "react"]
external reactAct: ((. unit) => undefined) => unit = "act";

[@deprecated "use React.act instead"]
let act: (unit => unit) => unit =
func => {
let reactFunc =
Expand All @@ -19,6 +20,7 @@ let act: (unit => unit) => unit =
external reactActAsync: ((. unit) => Js.Promise.t('a)) => Js.Promise.t(unit) =
"act";

[@deprecated "use React.actAsync instead"]
let actAsync = func => {
let reactFunc =
(.) => {
Expand All @@ -27,35 +29,73 @@ let actAsync = func => {
reactActAsync(reactFunc);
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
external isElement: 'element => bool = "isElement";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
external isElementOfType: ('element, React.component('props)) => bool =
"isElementOfType";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
external isDOMComponent: 'element => bool = "isDOMComponent";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
external isCompositeComponent: 'element => bool = "isCompositeComponent";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@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"]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
[@mel.scope "Simulate"]
external click: Dom.element => unit = "click";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@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"]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
[@mel.scope "Simulate"]
external change: Dom.element => unit = "change";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@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";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let changeWithValue = (element, value) => {
let event = {
"target": {
Expand All @@ -64,6 +104,10 @@ module Simulate = {
};
changeWithEvent(element, event);
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let changeWithChecked = (element, value) => {
let event = {
"target": {
Expand All @@ -72,11 +116,23 @@ module Simulate = {
};
changeWithEvent(element, event);
};
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
[@mel.scope "Simulate"]
external canPlay: Dom.element => unit = "canPlay";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@mel.module "react-dom/test-utils"]
[@mel.scope "Simulate"]
external timeUpdate: Dom.element => unit = "timeUpdate";
[@mel.module "react-dom/test-utils"] [@mel.scope "Simulate"]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
[@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";
Expand All @@ -101,7 +157,9 @@ 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";
Expand All @@ -111,38 +169,63 @@ let querySelectorAll = (element, string) => {
};

module DOM = {
[@mel.return nullable] [@mel.get]
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.return nullable]
[@mel.get]
external value: Dom.element => option(string) = "value";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
let findBySelector = (element, selector) =>
querySelector(element, selector);

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
let findByAllSelector = (element, selector) =>
querySelectorAll(element, selector);

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
let findBySelectorAndTextContent = (element, selector, content) =>
querySelectorAll(element, selector)
|> Array.find_opt(node => node->textContent === content);

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
let findBySelectorAndPartialTextContent = (element, selector, content) =>
querySelectorAll(element, selector)
|> Array.find_opt(node =>
Js.String.includes(~search=content, node->textContent)
);
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
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);
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let cleanupContainer = (container: ref(option(Dom.element)), ()) => {
let _: option(_) = Option.map(remove, container^);
container := None;
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let getContainer = container => {
container.contents->Option.get;
};
Loading

0 comments on commit 9857fc3

Please sign in to comment.