Skip to content

Commit

Permalink
Add Ref test
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Jul 17, 2024
1 parent 57eae9a commit 1dc2976
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/Ref__test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
open Jest;
open Jest.Expect;
open ReactDOMTestUtils;
open Belt;

/* https://react.dev/blog/2022/03/08/react-18-upgrade-guide#configuring-your-testing-environment */
[%%mel.raw "globalThis.IS_REACT_ACT_ENVIRONMENT = true"];

module Button_with_ref = {
[@react.component]
let make = (~children, ~ref) => {
<button ref className="FancyButton"> children </button>;
};
};

describe("React - Ref", () => {
let container = ref(None);

beforeEach(prepareContainer(container));
afterEach(cleanupContainer(container));

test("can render DOM elements", () => {
let container = getContainer(container);
let root = ReactDOM.Client.createRoot(container);
let domRef = React.createRef();

act(() => {
ReactDOM.Client.render(
root,
<Button_with_ref ref={ReactDOM.Ref.domRef(domRef)}>
"Click me"->React.string
</Button_with_ref>,
)
});

expect(
container
->DOM.findBySelectorAndTextContent("button", "Click me")
->Option.isSome,
)
->toBe(true);
});
});

0 comments on commit 1dc2976

Please sign in to comment.