Skip to content

Commit 4434bfd

Browse files
committed
Fix Form__test with RTL
1 parent 05aa7e1 commit 4434bfd

File tree

5 files changed

+107
-137
lines changed

5 files changed

+107
-137
lines changed

package-lock.json

Lines changed: 18 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
"homepage": "https://reasonml.github.io/reason-react/",
2727
"devDependencies": {
2828
"jest": "^26.0.1",
29-
"react": "^18.2.0",
30-
"react-dom": "^18.2.0",
31-
"react-test-renderer": "^18.2.0"
29+
"react": "19.0.0-rc.1",
30+
"react-dom": "19.0.0-rc.1"
3231
},
3332
"jest": {
3433
"moduleDirectories": [

test/Form__test.re

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
open Jest;
2-
open Jest.Expect;
3-
open ReactDOMTestUtils;
4-
open Belt;
52

63
module FormData = React.Experimental.FormData;
74

@@ -25,7 +22,11 @@ module Thread = {
2522
let (optimisticMessages, addOptimisticMessage) =
2623
React.Experimental.useOptimistic(messages, (state, newMessage) =>
2724
[
28-
{text: newMessage, sending: true, key: List.length(state) + 1},
25+
{
26+
text: newMessage,
27+
sending: true,
28+
key: List.length(state) + 1,
29+
},
2930
...state,
3031
]
3132
);
@@ -49,24 +50,26 @@ module Thread = {
4950
};
5051
};
5152
<>
52-
{{
53-
optimisticMessages->Belt.List.map(message =>
54-
<div key={Int.toString(message.key)}>
55-
{React.string(message.text)}
56-
{message.sending
57-
? React.null
58-
: <small> {React.string("(Enviando...)")} </small>}
59-
</div>
60-
);
61-
}
62-
->Belt.List.toArray
63-
->React.array}
53+
<div id="messages">
54+
{{
55+
optimisticMessages->Belt.List.map(message =>
56+
<span key={Belt.Int.toString(message.key)}>
57+
{React.string(message.text)}
58+
{message.sending
59+
? React.null
60+
: <small> {React.string("(Enviando...)")} </small>}
61+
</span>
62+
);
63+
}
64+
->Belt.List.toArray
65+
->React.array}
66+
</div>
6467
{React.cloneElement(
6568
ReactDOM.createElement(
6669
"form",
6770
~props=ReactDOM.domProps(~ref=ReactDOM.Ref.domRef(formRef), ()),
6871
[|
69-
<input type_="text" name="message" placeholder="Hola!" />,
72+
<input type_="text" name="message" placeholder="message" />,
7073
<button type_="submit"> {React.string("Enviar")} </button>,
7174
|],
7275
),
@@ -84,7 +87,15 @@ module App = {
8487
[@react.component]
8588
let make = () => {
8689
let (messages, setMessages) =
87-
React.useState(() => [{text: "¡Hola!", sending: false, key: 1}]);
90+
React.useState(() =>
91+
[
92+
{
93+
text: "Hola!",
94+
sending: false,
95+
key: 1,
96+
},
97+
]
98+
);
8899

89100
let sendMessage = formData => {
90101
let formMessage = FormData.get("message", formData);
@@ -95,7 +106,14 @@ module App = {
95106
| JSString(text) =>
96107
let _ =
97108
setMessages(messages =>
98-
[{text, sending: true, key: 1}, ...messages]
109+
[
110+
{
111+
text,
112+
sending: true,
113+
key: 1,
114+
},
115+
...messages,
116+
]
99117
);
100118
Js.Promise.resolve();
101119
| _ => Js.Promise.resolve()
@@ -108,46 +126,38 @@ module App = {
108126
};
109127
};
110128

111-
describe("Form with useOptimistic", () => {
112-
let container = ref(None);
113-
114-
beforeEach(prepareContainer(container));
115-
afterEach(cleanupContainer(container));
116-
117-
test("should render the form", () => {
118-
let container = getContainer(container);
119-
let root = ReactDOM.Client.createRoot(container);
129+
let (let.await) = (p, f) => Js.Promise.then_(f, p);
130+
let (let.catch) = (p, f) => Js.Promise.then_(f, p);
120131

121-
act(() => ReactDOM.Client.render(root, <App />));
132+
let findByString = (text, container) =>
133+
ReactTestingLibrary.findByText(~matcher=`Str(text), container);
122134

123-
expect(
124-
container
125-
->DOM.findBySelectorAndTextContent("button", "0")
126-
->Option.isSome,
127-
)
128-
->toBe(true);
135+
let findByPlaceholderText = (text, container) =>
136+
ReactTestingLibrary.findByPlaceholderText(~matcher=`Str(text), container);
129137

130-
let button = container->DOM.findBySelector("button");
138+
describe("Form with useOptimistic", () => {
139+
testPromise("should render the form", finish => {
140+
let container = ReactTestingLibrary.render(<App />);
141+
142+
ReactTestingLibrary.actAsync(() => {
143+
let.await _ = findByString("Hola!", container);
144+
145+
let.await button = findByString("Enviar", container);
146+
let.await input = findByPlaceholderText("message", container);
147+
148+
FireEvent.change(
149+
input,
150+
~eventInit={
151+
"target": {
152+
"value": "Let's go!",
153+
},
154+
},
155+
);
131156

132-
act(() => {
133-
switch (button) {
134-
| Some(button) => Simulate.click(button)
135-
| None => ()
136-
}
157+
FireEvent.click(button);
158+
let.await _newMessage = findByString("Let's go!", container);
159+
/* If the promise resolve, means the node is found in the DOM */
160+
finish();
137161
});
138-
139-
expect(
140-
container
141-
->DOM.findBySelectorAndTextContent("button", "0")
142-
->Option.isSome,
143-
)
144-
->toBe(false);
145-
146-
expect(
147-
container
148-
->DOM.findBySelectorAndTextContent("button", "1")
149-
->Option.isSome,
150-
)
151-
->toBe(true);
152-
});
162+
})
153163
});

test/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
reason-react.node
99
jest
1010
melange.belt
11-
melange-testing-library.dom
12-
melange-testing-library.react)
11+
melange_testing_library_dom
12+
melange_testing_library_react)
1313
(preprocess
1414
(pps melange.ppx reason-react-ppx)))

test/jest/Jest.re

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ let testAsync: (~timeout: int=?, string, (unit => unit) => unit) => unit =
2222
(~timeout=?, name, f) =>
2323
testAsyncU(name, callback => f(callback), timeout);
2424

25+
external testPromiseU:
26+
(
27+
string,
28+
(unit => Js.Promise.t(unit)) => Js.Promise.t(unit),
29+
option(int)
30+
) =>
31+
unit =
32+
"test";
33+
34+
let testPromise:
35+
(
36+
~timeout: int=?,
37+
string,
38+
(unit => Js.Promise.t(unit)) => Js.Promise.t(unit)
39+
) =>
40+
unit =
41+
(~timeout=?, name, f) =>
42+
testPromiseU(name, callback => f(callback), timeout);
43+
2544
module Only = {
2645
[@mel.scope "describe"]
2746
external describeU: (string, (. unit) => unit) => unit = "only";

0 commit comments

Comments
 (0)