Skip to content

Commit 9ec7c12

Browse files
authored
test: memo and renders (#825)
1 parent 470a284 commit 9ec7c12

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/React__test.re

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,54 @@ describe("React", () => {
359359
/* We catch the exception here to not populate the error to the toplevel */
360360
()
361361
};
362+
363+
test(
364+
"Memo and normal components rendering with equal and different props", () => {
365+
let container = getContainer(container);
366+
let root = ReactDOM.Client.createRoot(container);
367+
368+
module Normal = {
369+
let renders = ref(0);
370+
371+
[@react.component]
372+
let make = (~a) => {
373+
renders := renders^ + 1;
374+
<div> {Printf.sprintf("`a` is %s", a) |> React.string} </div>;
375+
};
376+
};
377+
378+
module Memo = {
379+
let renders = ref(0);
380+
381+
[@react.component]
382+
let make =
383+
React.memo((~a) => {
384+
renders := renders^ + 1;
385+
<div> {Printf.sprintf("`a` is %s", a) |> React.string} </div>;
386+
});
387+
};
388+
389+
act(() => {
390+
ReactDOM.Client.render(
391+
root,
392+
<div> <Normal a="a" /> <Memo a="a" /> </div>,
393+
)
394+
});
395+
act(() => {
396+
ReactDOM.Client.render(
397+
root,
398+
<div> <Normal a="a" /> <Memo a="a" /> </div>,
399+
)
400+
});
401+
act(() => {
402+
ReactDOM.Client.render(
403+
root,
404+
<div> <Normal a="b" /> <Memo a="b" /> </div>,
405+
)
406+
});
407+
408+
expect(Normal.renders^)->toBe(3);
409+
410+
expect(Memo.renders^)->toBe(2);
411+
});
362412
});

0 commit comments

Comments
 (0)