Skip to content
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
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
52 changes: 3 additions & 49 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, <div> "Hello world!"->React.string </div>);
});

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

Expand All @@ -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`
Expand Down
40 changes: 0 additions & 40 deletions src/ReactDOMTestUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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));
};
Expand Down Expand Up @@ -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;
};
13 changes: 0 additions & 13 deletions src/ReactDOMTestUtils.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading