Skip to content

Commit

Permalink
Vendor bs-react-test-renderer bindings and adapt to our Jest bindings (
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx authored Jul 10, 2023
1 parent 9e70d75 commit f6187dc
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 4 deletions.
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"devDependencies": {
"jest": "^26.0.1",
"react": "^16.8.1",
"react-dom": "^16.8.1"
"react-dom": "^16.8.1",
"react-test-renderer": "^16.13.1"
},
"peerDependencies": {
"react": "^16.8.1",
Expand Down
25 changes: 25 additions & 0 deletions src/ReactTestRenderer.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type t;

[@bs.module "react-test-renderer"] [@bs.val] external create :
React.element => t = "create";

[@bs.send] external toJSON :
t => Js.Json.t = "toJSON";

module Shallow = {
type t;

[@bs.module "react-test-renderer/shallow"] [@bs.val] external createRenderer :
unit => t = "createRenderer";

[@bs.send] external render :
t => React.element => option(React.element) = "render";

[@bs.send] external getRenderOutput :
t => option(React.element) = "getRenderOutput";

[@bs.send] external unmount :
t => unit = "unmount";

let renderWithRenderer = render(createRenderer());
}
111 changes: 111 additions & 0 deletions test/ReactTestRenderer__test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
open Jest;

external toObject: ReactTestRenderer.t => Js.t({.}) = "%identity";

module Tester = {
[@react.component]
let make = () => <div> {React.string("Tester")} </div>;
};

describe("reactTestRenderer", () => {
open Expect;

test("create returns ReactTestInstance", () => {
let component = ReactTestRenderer.create(<Tester />);
let keys = Js.Obj.keys(component);

expect(keys)
->toEqual(
arrayContaining([|
"_Scheduler",
"root",
"toJSON",
"toTree",
"update",
"unmount",
"unstable_flushSync",
"getInstance",
|]),
);
});

test("toJSON returns test rendered JSON", () => {
let component = ReactTestRenderer.create(<Tester />);
let json = ReactTestRenderer.toJSON(component);
let expected =
Js.Json.parseExn(
{|
{
"type": "div",
"props": {},
"children": [ "Tester" ]
}
|},
);
expect(json == expected)->toBe(true);
});
});

let element = <div> {React.string("Tester")} </div>;

describe("reactShallowRenderer", () => {
open Expect;

test("createRenderer", () => {
let renderer = ReactTestRenderer.Shallow.createRenderer();
let isDefined =
renderer
|> Js.Undefined.return
|> Js.Undefined.toOption
|> Option.is_some;
expect(isDefined)->toBe(true);
});

test("render accepts renderer", () => {
let renderer = ReactTestRenderer.Shallow.createRenderer();
let render = ReactTestRenderer.Shallow.render(renderer);
expect(Js.typeof(render))->toEqual("function");
});

test("render will render a component", () => {
let renderer = ReactTestRenderer.Shallow.createRenderer();
let component =
ReactTestRenderer.Shallow.render(renderer, <Tester />)
->Option.get;
expect(component == element)->toBe(true);
});

test("renderWithRenderer will render a component", () => {
let component =
ReactTestRenderer.Shallow.renderWithRenderer(<Tester />)
->Option.get;

expect(component == element)->toBe(true);
});

test("getRenderOutput returns element", () => {
let renderer = ReactTestRenderer.Shallow.createRenderer();

ReactTestRenderer.Shallow.render(renderer, <Tester />) |> ignore;

let component =
ReactTestRenderer.Shallow.getRenderOutput(renderer)
->Option.get;

expect(component == element)->toBe(true);
});

test("unmount removes the node", () => {
let renderer = ReactTestRenderer.Shallow.createRenderer();

ReactTestRenderer.Shallow.render(renderer, <Tester />) |> ignore;
ReactTestRenderer.Shallow.unmount(renderer);

let component =
ReactTestRenderer.Shallow.getRenderOutput(renderer)
->Option.get
->Js.Null.return;

expect(component)->toEqual(Js.null);
});
});

0 comments on commit f6187dc

Please sign in to comment.