From 1cb190d1c1846e5fdc9749241f47ddf9176f6194 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Fri, 27 Nov 2026 21:21:34 -0800 Subject: [PATCH] Remove deprecated ReactDOMTestUtils container helpers Signed-off-by: Antonio Nuno Monteiro --- CHANGES.md | 7 ++++++ docs/testing.md | 52 +++------------------------------------ src/ReactDOMTestUtils.re | 40 ------------------------------ src/ReactDOMTestUtils.rei | 13 ---------- 4 files changed, 10 insertions(+), 102 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index eb5cf76fc..007135741 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +# Unreleased + +* BREAKING: Remove the deprecated `ReactDOMTestUtils.prepareContainer`, + `cleanupContainer`, and `getContainer` helpers. Use `melange-testing-library` + for test setup and cleanup (@anmonteiro in + [#975](https://github.com/reasonml/reason-react/pull/975)). + # 0.17.0 * Support custom `data-*` attributes in DOM JSX, including optional string values (@anmonteiro in https://github.com/reasonml/reason-react/pull/933) diff --git a/docs/testing.md b/docs/testing.md index 8b389e472..17792731b 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -15,49 +15,9 @@ Pick the test framework you like the best (both use Jest under the hood): ### Test utilities -We provide test utilities in the [`ReactDOMTestUtils`](https://github.com/reasonml/reason-react/blob/main/src/ReactDOMTestUtils.re) module. - -If you want to have a look at a real example, here's what a test might look like from reason-react's test suite [Hooks__test](https://github.com/reasonml/reason-react/blob/main/test/Hooks__test.re). - -Here's is a basic example of what a test might look like: - -```reason -open ReactDOMTestUtils; -open TestFramework; -// TestFramework isn't a real module, just an imaginary set of bindings -// to a JavaScript testing framework - -describe("My basic test", ({test, beforeEach, afterEach}) => { - // Here, we prepare an empty ref that will eventually be - // the root node for our test - let container = ref(None); - - // Before each test, creates a new div root - beforeEach(prepareContainer(container)); - // After each test, removes the div - afterEach(cleanupContainer(container)); - - test("can render DOM elements", ({expect}) => { - // The following function gives us the div - let container = getContainer(container); - let root = ReactDOM.Client.createRoot(container); - - // Most of the ReactDOMTestUtils API is there - act(() => { - ReactDOM.Client.render(root,
"Hello world!"->React.string
); - }); - - expect.bool( - container - // We also provide some basic DOM querying utilities - // to ease your tests - ->DOM.findBySelectorAndTextContent("div", "Hello world!") - ->Option.isSome, - ). - toBeTrue(); - }); -}); -``` +The `ReactDOMTestUtils` module is deprecated. Prefer `melange-testing-library` for rendering, querying, and cleaning up components in tests, and `React.act` or `React.actAsync` when testing React updates directly. + +The deprecated `prepareContainer`, `cleanupContainer`, and `getContainer` helpers have been removed. See [Hooks__test](https://github.com/reasonml/reason-react/blob/main/test/Hooks__test.re) for examples of test setup using testing-library bindings. ### API @@ -73,12 +33,6 @@ Directly from the [React test utilities](https://reactjs.org/docs/test-utils.htm - `isCompositeComponent` - `isCompositeComponentWithType` -And utilities for setup and teardown: - -- `prepareContainer` -- `cleanupContainer` -- `getContainer` - #### `ReactDOMTestUtils.Simulate` - `Simulate.click` diff --git a/src/ReactDOMTestUtils.re b/src/ReactDOMTestUtils.re index 24dafbff3..2d02c2bd3 100644 --- a/src/ReactDOMTestUtils.re +++ b/src/ReactDOMTestUtils.re @@ -138,8 +138,6 @@ module Simulate = { external focus: Dom.element => unit = "focus"; }; -external document: Dom.document = "document"; - [@mel.return nullable] [@mel.send] external querySelector: (Dom.element, string) => option(Dom.element) = "querySelector"; @@ -151,19 +149,6 @@ external querySelectorAll: [@mel.get] external textContent: Dom.element => string = "textContent"; -[@mel.return nullable] [@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)); }; @@ -204,28 +189,3 @@ module DOM = { 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; -}; diff --git a/src/ReactDOMTestUtils.rei b/src/ReactDOMTestUtils.rei index bfab45e81..da97d669a 100644 --- a/src/ReactDOMTestUtils.rei +++ b/src/ReactDOMTestUtils.rei @@ -130,16 +130,3 @@ module DOM: { (Dom.element, string, string) => option(Dom.element) ); }; - -[@deprecated - "ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead." -] -let prepareContainer: (Stdlib.ref(option(Dom.element)), unit) => unit; -[@deprecated - "ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead." -] -let cleanupContainer: (Stdlib.ref(option(Dom.element)), unit) => unit; -[@deprecated - "ReactDOMTestUtils is deprecated, and will be removed in next version. Please use melange-testing-library instead." -] -let getContainer: Stdlib.ref(option(Dom.element)) => Dom.element;