Skip to content

Commit

Permalink
Add deprecations on ReactDOMTestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Nov 25, 2024
1 parent cb48b76 commit e78adcc
Showing 1 changed file with 209 additions and 67 deletions.
276 changes: 209 additions & 67 deletions src/ReactDOMTestUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,139 @@ type undefined = Js.undefined(unit);

let undefined: undefined = Js.Undefined.empty;

[@mel.module "react"]
[@deprecated "use React.act instead"] [@mel.module "react"]
external reactAct: ((. unit) => undefined) => unit = "act";

[@deprecated "use React.act instead"]
let act: (unit => unit) => unit =
func => {
let reactFunc =
(.) => {
func();
undefined;
};
reactAct(reactFunc);
};

[@mel.module "react"]
[@alert "-deprecated"]
(
func => {
let reactFunc =
(.) => {
func();
undefined;
};
reactAct(reactFunc);
}
);

[@deprecated "use React.actAsync instead"] [@mel.module "react"]
external reactActAsync: ((. unit) => Js.Promise.t('a)) => Js.Promise.t(unit) =
"act";

let actAsync = func => {
let reactFunc =
(.) => {
func();
};
reactActAsync(reactFunc);
};
[@deprecated "use React.actAsync instead"]
let actAsync =
[@alert "-deprecated"]
(
func => {
let reactFunc =
(.) => {
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"]

[@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 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);
};

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let changeWithValue =
[@alert "-deprecated"]
(
(element, value) => {
let event = {
"target": {
"value": value,
},
};
changeWithEvent(element, event);
}
);

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead."
]
let changeWithChecked =
[@alert "-deprecated"]
(
(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"]
Expand All @@ -84,65 +147,144 @@ module Simulate = {

external document: Dom.document = "document";

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

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.send]
external querySelectorAll:
(Dom.element, string) => Js.Array.array_like(Dom.element) =
"querySelectorAll";

[@mel.get] external textContent: Dom.element => string = "textContent";
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.get]
external textContent: Dom.element => string = "textContent";

[@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 body: Dom.document => option(Dom.element) = "body";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.send]
external createElement: (Dom.document, string) => Dom.element =
"createElement";
[@mel.send] external remove: Dom.element => unit = "remove";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.send]
external remove: Dom.element => unit = "remove";

[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
[@mel.send]
external appendChild: (Dom.element, Dom.element) => Dom.element =
"appendChild";

let querySelectorAll = (element, string) => {
Js.Array.from(querySelectorAll(element, string));
};
[@deprecated
"ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-webapi instead."
]
let querySelectorAll =
[@alert "-deprecated"]
(
(element, string) => {
Js.Array.from(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";

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

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

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

0 comments on commit e78adcc

Please sign in to comment.