Skip to content

Commit

Permalink
Update API with new queries from bs-dom-testing-library
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Sep 18, 2018
1 parent bb50823 commit 78128d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
18 changes: 17 additions & 1 deletion src/ReactTestingLibrary.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include DomTestingLibrary;
open DomTestingLibrary;

module Simulate = {
[@bs.module "react-testing-library"] [@bs.scope "Simulate"]
Expand Down Expand Up @@ -30,6 +30,22 @@ external _debug : Js.undefined(Dom.element) => unit = "debug";
[@bs.send.pipe: renderResult]
external rerender : ReasonReact.reactElement => unit = "";

let getByAltText = (string, result) =>
getByAltText(string, result |> container);

let getByPlaceholderText = (string, result) =>
getByPlaceholderText(string, result |> container);

let getByTestId = (string, result) =>
getByTestId(string, result |> container);

let getByText = (~matcher, ~options=?, result) =>
getByText(~matcher, ~options=?options, result |> container);

let getByTitle = (string, result) => getByTitle(string, result |> container);

let getByValue = (string, result) => getByValue(string, result |> container);

let render = (~baseElement=?, ~container=?, element) => {
let baseElement_ =
switch (container) {
Expand Down
53 changes: 22 additions & 31 deletions src/ReactTestingLibrary.rei
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
module Query = DomTestingLibrary.Query;
module Wait = DomTestingLibrary.Wait;
module WaitForElement = DomTestingLibrary.WaitForElement;

let wait:
(~callback: unit => unit=?, ~options: Wait.options=?, unit) =>
Js.Promise.t('a);

let waitForElement:
(~callback: unit => 'a=?, ~options: WaitForElement.options=?, unit) =>
Js.Promise.t('a);

let prettyDOM: (~maxLength: int=?, Dom.element) => string;

[@bs.module "dom-testing-library"]
external getNodeText : Dom.element => string = "";

let getByTestId: (string, Dom.element) => Dom.element;

let getByText:
(
~matcher: [
| `Func((string, Dom.element) => bool)
| `RegExp(Js.Re.t)
| `Str(string)
],
~options: Query.options=?,
Dom.element
) =>
Dom.element;

module Simulate: {
[@bs.module "react-testing-library"] [@bs.scope "Simulate"]
external click : Dom.element => unit = "";
Expand All @@ -49,6 +18,28 @@ type renderOptions = {

[@bs.send.pipe: renderResult] external unmount : unit => bool = "";

let getByAltText: (string, renderResult) => Dom.element;

let getByPlaceholderText: (string, renderResult) => Dom.element;

let getByTestId: (string, renderResult) => Dom.element;

let getByText:
(
~matcher: [
| `Func((string, Dom.element) => bool)
| `RegExp(Js.Re.t)
| `Str(string)
],
~options: DomTestingLibrary.Query.options=?,
renderResult
) =>
Dom.element;

let getByTitle: (string, renderResult) => Dom.element;

let getByValue: (string, renderResult) => Dom.element;

[@bs.send.pipe: renderResult]
external rerender : ReasonReact.reactElement => unit = "";

Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/ReactTestingLibrary_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ describe("ReactTestingLibrary", () => {

let element = (
<div style=ReactDOMRe.Style.make(~color="rebeccapurple", ())>
<h1> {ReasonReact.string("Heading")} </h1>
(
ReasonReact.cloneElement(
<h1 />,
~props={"data-testid": "h1-heading"},
[|ReasonReact.string("Heading")|]
)
)
</div>
);

Expand All @@ -40,6 +46,14 @@ describe("ReactTestingLibrary", () => {
|> toMatchSnapshot;
});

test("getByTestId works", () => {
element
|> render
|> getByTestId("h1-heading")
|> expect
|> toMatchSnapshot;
});

describe("debug", () => {
open JestJs;

Expand Down
16 changes: 14 additions & 2 deletions src/__tests__/__snapshots__/ReactTestingLibrary_test.bs.js.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ReactTestingLibrary getByTestId works 1`] = `
<h1
data-testid="h1-heading"
>
Heading
</h1>
`;

exports[`ReactTestingLibrary render works 1`] = `
Object {
"baseElement": <body>
<div>
<div>
<h1>
<h1
data-testid="h1-heading"
>
Heading
</h1>
</div>
</div>
</body>,
"container": <div>
<div>
<h1>
<h1
data-testid="h1-heading"
>
Heading
</h1>
</div>
Expand Down

0 comments on commit 78128d3

Please sign in to comment.