11open Jest ;
2- open Jest . Expect ;
3- open ReactDOMTestUtils ;
4- open Belt ;
52
63module 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});
0 commit comments