Skip to content

Commit 78128d3

Browse files
committed
Update API with new queries from bs-dom-testing-library
1 parent bb50823 commit 78128d3

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

src/ReactTestingLibrary.re

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include DomTestingLibrary;
1+
open DomTestingLibrary;
22

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

33+
let getByAltText = (string, result) =>
34+
getByAltText(string, result |> container);
35+
36+
let getByPlaceholderText = (string, result) =>
37+
getByPlaceholderText(string, result |> container);
38+
39+
let getByTestId = (string, result) =>
40+
getByTestId(string, result |> container);
41+
42+
let getByText = (~matcher, ~options=?, result) =>
43+
getByText(~matcher, ~options=?options, result |> container);
44+
45+
let getByTitle = (string, result) => getByTitle(string, result |> container);
46+
47+
let getByValue = (string, result) => getByValue(string, result |> container);
48+
3349
let render = (~baseElement=?, ~container=?, element) => {
3450
let baseElement_ =
3551
switch (container) {

src/ReactTestingLibrary.rei

+22-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
1-
module Query = DomTestingLibrary.Query;
2-
module Wait = DomTestingLibrary.Wait;
3-
module WaitForElement = DomTestingLibrary.WaitForElement;
4-
5-
let wait:
6-
(~callback: unit => unit=?, ~options: Wait.options=?, unit) =>
7-
Js.Promise.t('a);
8-
9-
let waitForElement:
10-
(~callback: unit => 'a=?, ~options: WaitForElement.options=?, unit) =>
11-
Js.Promise.t('a);
12-
13-
let prettyDOM: (~maxLength: int=?, Dom.element) => string;
14-
15-
[@bs.module "dom-testing-library"]
16-
external getNodeText : Dom.element => string = "";
17-
18-
let getByTestId: (string, Dom.element) => Dom.element;
19-
20-
let getByText:
21-
(
22-
~matcher: [
23-
| `Func((string, Dom.element) => bool)
24-
| `RegExp(Js.Re.t)
25-
| `Str(string)
26-
],
27-
~options: Query.options=?,
28-
Dom.element
29-
) =>
30-
Dom.element;
31-
321
module Simulate: {
332
[@bs.module "react-testing-library"] [@bs.scope "Simulate"]
343
external click : Dom.element => unit = "";
@@ -49,6 +18,28 @@ type renderOptions = {
4918

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

21+
let getByAltText: (string, renderResult) => Dom.element;
22+
23+
let getByPlaceholderText: (string, renderResult) => Dom.element;
24+
25+
let getByTestId: (string, renderResult) => Dom.element;
26+
27+
let getByText:
28+
(
29+
~matcher: [
30+
| `Func((string, Dom.element) => bool)
31+
| `RegExp(Js.Re.t)
32+
| `Str(string)
33+
],
34+
~options: DomTestingLibrary.Query.options=?,
35+
renderResult
36+
) =>
37+
Dom.element;
38+
39+
let getByTitle: (string, renderResult) => Dom.element;
40+
41+
let getByValue: (string, renderResult) => Dom.element;
42+
5243
[@bs.send.pipe: renderResult]
5344
external rerender : ReasonReact.reactElement => unit = "";
5445

src/__tests__/ReactTestingLibrary_test.re

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ describe("ReactTestingLibrary", () => {
2929

3030
let element = (
3131
<div style=ReactDOMRe.Style.make(~color="rebeccapurple", ())>
32-
<h1> {ReasonReact.string("Heading")} </h1>
32+
(
33+
ReasonReact.cloneElement(
34+
<h1 />,
35+
~props={"data-testid": "h1-heading"},
36+
[|ReasonReact.string("Heading")|]
37+
)
38+
)
3339
</div>
3440
);
3541

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

49+
test("getByTestId works", () => {
50+
element
51+
|> render
52+
|> getByTestId("h1-heading")
53+
|> expect
54+
|> toMatchSnapshot;
55+
});
56+
4357
describe("debug", () => {
4458
open JestJs;
4559

src/__tests__/__snapshots__/ReactTestingLibrary_test.bs.js.snap

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`ReactTestingLibrary getByTestId works 1`] = `
4+
<h1
5+
data-testid="h1-heading"
6+
>
7+
Heading
8+
</h1>
9+
`;
10+
311
exports[`ReactTestingLibrary render works 1`] = `
412
Object {
513
"baseElement": <body>
614
<div>
715
<div>
8-
<h1>
16+
<h1
17+
data-testid="h1-heading"
18+
>
919
Heading
1020
</h1>
1121
</div>
1222
</div>
1323
</body>,
1424
"container": <div>
1525
<div>
16-
<h1>
26+
<h1
27+
data-testid="h1-heading"
28+
>
1729
Heading
1830
</h1>
1931
</div>

0 commit comments

Comments
 (0)