-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor bs-react-test-renderer bindings and adapt to our Jest bindings (…
- Loading branch information
Showing
4 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |