Skip to content

Commit 95b513e

Browse files
committed
Add reason testing library documentation (#29)
* Add reason testing library documentation * Split reason docs into separate sections
1 parent 57dd262 commit 95b513e

File tree

11 files changed

+320
-5
lines changed

11 files changed

+320
-5
lines changed

docs/api-queries.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ const usernameInputElement = getByTestId(container, 'username-input')
283283
> case. Using data-testid attributes do not resemble how your software is used
284284
> and should be avoided if possible. That said, they are _way_ better than
285285
> querying based on DOM structure or styling css class names. Learn more about
286-
> `data-testid`s from the blog post ["Making your UI tests resilient to
287-
> change"](https://blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269)
286+
> `data-testid`s from the blog post
287+
> ["Making your UI tests resilient to change"](https://blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269)
288288
289289
#### Overriding `data-testid`
290290

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
id: example-intro
3+
title: Example
4+
---
5+
6+
The below examples use
7+
[`bs-webapi`](https://github.com/reasonml-community/bs-webapi-incubator) to help
8+
with typings and creating events.
9+
10+
## getByText
11+
12+
```reason
13+
/* __tests__/example_test.re */
14+
open Jest;
15+
open DomTestingLibrary;
16+
open Expect;
17+
18+
type parser;
19+
20+
[@bs.new]
21+
external domParser : unit => parser = "DOMParser";
22+
23+
[@bs.send.pipe : parser]
24+
external parseFromString : ( string, [@bs.as "text/html"] _) => Dom.element = "";
25+
26+
[@bs.get]
27+
external body : Dom.element => Dom.element = "";
28+
29+
[@bs.get]
30+
external firstChild : Dom.element => Dom.element = "";
31+
32+
let div = domParser()
33+
|> parseFromString({j|
34+
<div>
35+
<b title="greeting">Hello,</b>
36+
<p data-testid="world"> World!</p>
37+
<input type="text" placeholder="Enter something" />
38+
<input type="text" value="Some value" />
39+
<img src="" alt="Alt text" />
40+
</div>
41+
|j})
42+
|> body
43+
|> firstChild;
44+
45+
describe("getByText", () => {
46+
test("works with string matcher", () => {
47+
let actual = div |> getByText(~matcher=`Str("Hello,"));
48+
49+
expect(actual) |> toMatchSnapshot;
50+
});
51+
52+
test("works with regex matcher", () => {
53+
let actual = div |> getByText(~matcher=`RegExp([%bs.re "/\\w!/"]));
54+
55+
expect(actual) |> toMatchSnapshot;
56+
});
57+
58+
test("works with function matcher", () => {
59+
let matcher = ( _text, node ) => (node |> tagName) === "P";
60+
let actual = div |> getByText(~matcher=`Func(matcher));
61+
62+
expect(actual) |> toMatchSnapshot;
63+
});
64+
});
65+
```
66+
67+
## FireEvent
68+
69+
```reason
70+
open Jest;
71+
open DomTestingLibrary;
72+
open Expect;
73+
74+
describe("FireEvent", () => {
75+
test("click works", () => {
76+
open Webapi.Dom;
77+
78+
let node = document |> Document.createElement("button");
79+
let spy = JestJs.inferred_fn();
80+
let fn = spy |> MockJs.fn;
81+
let clickHandler = _ => [@bs] fn("clicked!") |> ignore;
82+
83+
node |> Element.addEventListener("click", clickHandler);
84+
85+
FireEvent.click(node);
86+
87+
expect(spy |> MockJs.calls) |> toEqual([|"clicked!"|]);
88+
});
89+
90+
test("change works", () => {
91+
open Webapi.Dom;
92+
93+
let node = document |> Document.createElement("input");
94+
let spy = JestJs.inferred_fn();
95+
let fn = spy |> MockJs.fn;
96+
let changeHandler = _ => [@bs] fn("changed!") |> ignore;
97+
let event = Event.makeWithOptions("change", { "target": { "value": "1" } });
98+
99+
node |> Element.addEventListener("change", changeHandler);
100+
101+
FireEvent.change(node, event);
102+
103+
expect(spy |> MockJs.calls) |> toEqual([|"changed!"|]);
104+
});
105+
});
106+
```
107+
108+
## More
109+
110+
You can find more bs-dom-testing-library examples at
111+
[wyze/bs-dom-testing-library/src/\_\_tests\_\_](https://github.com/wyze/bs-dom-testing-library/tree/master/src/__tests__).

docs/bs-dom-testing-library/intro.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: intro
3+
title: Introduction
4+
---
5+
6+
[`bs-dom-testing-library`][gh] contains [BuckleScript][bs] bindings for
7+
`dom-testing-library`.
8+
9+
```
10+
npm install --save-dev bs-dom-testing-library
11+
```
12+
13+
- [bs-dom-testing-library on GitHub][gh]
14+
15+
[gh]: https://github.com/wyze/bs-dom-testing-library
16+
17+
## Setup
18+
19+
After installation, you will need to add it to your `bsconfig.json` file like
20+
so:
21+
22+
```json
23+
{
24+
"bs-dev-dependencies": ["bs-dom-testing-library"]
25+
}
26+
```
27+
28+
## Other Dependencies
29+
30+
### bs-platform
31+
32+
This is what [BuckleScript][bs] uses to compile the [Reason][re] code to JS. If
33+
it is not in your project you can install it like so:
34+
35+
```
36+
npm install --save-dev bs-platform
37+
```
38+
39+
### bs-jest
40+
41+
This is the recommended test runner and is a wrapper around Jest. All of the
42+
examples here will be using it.
43+
44+
- [bs-jest on GitHub](https://github.com/glennsl/bs-jest)
45+
46+
```
47+
npm install --save-dev @glennsl/bs-jest
48+
```
49+
50+
Then update `bsconfig.json`:
51+
52+
```json
53+
{
54+
"bs-dev-dependencies": ["@glennsl/bs-jest"]
55+
}
56+
```
57+
58+
[bs]: https://bucklescript.github.io/
59+
[re]: https://reasonml.github.io/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
id: example-intro
3+
title: Example
4+
---
5+
6+
## Basic
7+
8+
```reason
9+
/* Component_test.re */
10+
11+
open Jest;
12+
open Expect;
13+
open ReactTestingLibrary;
14+
15+
test("Component renders", () =>
16+
<div style=ReactDOMRe.Style.make(~color="rebeccapurple", ())>
17+
<h1> {ReasonReact.string("Heading")} </h1>
18+
</div>
19+
|> render
20+
|> expect
21+
|> toMatchSnapshot
22+
);
23+
```
24+
25+
## More
26+
27+
You can find more bs-react-testing-library examples at
28+
[wyze/bs-react-testing-library/src/\_\_tests\_\_](https://github.com/wyze/bs-react-testing-library/tree/master/src/__tests__).
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
id: intro
3+
title: Introduction
4+
---
5+
6+
[`bs-react-testing-library`][gh] contains
7+
[BuckleScript](https://bucklescript.github.io/) bindings for
8+
`react-testing-library`.
9+
10+
```
11+
npm install --save-dev bs-react-testing-library
12+
```
13+
14+
- [bs-react-testing-library on GitHub][gh]
15+
16+
[gh]: https://github.com/wyze/bs-react-testing-library
17+
18+
## Setup
19+
20+
After installation, you will need to add it to your `bsconfig.json` file like
21+
so:
22+
23+
```json
24+
{
25+
"bs-dev-dependencies": ["bs-react-testing-library"]
26+
}
27+
```

docs/ecosystem-bs-jest-dom.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: ecosystem-bs-jest-dom
3+
title: bs-jest-dom
4+
---
5+
6+
[`bs-jest-dom`][gh] is a companion library for `bs-react-testing-library` that
7+
provides custom DOM element matchers for Jest
8+
9+
```
10+
npm install --save-dev bs-jest-dom
11+
```
12+
13+
- [bs-jest-dom on GitHub][gh]
14+
15+
Check out [jest-dom's documentation](https://github.com/gnapse/jest-dom) for a
16+
full list of available matchers.
17+
18+
[gh]: https://github.com/wyze/bs-jest-dom
19+
20+
## Setup
21+
22+
```json
23+
{
24+
"bs-dev-dependencies": ["bs-jest-dom"]
25+
}
26+
```
27+
28+
## Example
29+
30+
```reason
31+
/* A_test.re */
32+
33+
open Jest;
34+
open JestDom;
35+
open ReactTestingLibrary;
36+
37+
module Heading = {
38+
let component = ReasonReact.statelessComponent("Heading");
39+
40+
let make = (~text, _children) => {
41+
...component,
42+
render: _self =>
43+
<h1> {ReasonReact.string(text)} </h1>,
44+
};
45+
};
46+
47+
test("renders with text", () =>
48+
<Heading text="Hello, World!" />
49+
|> render
50+
|> getByText(~matcher=`Str("Hello, World!"))
51+
|> expect
52+
|> toBeInTheDocument
53+
);
54+
```
55+
56+
## More Examples
57+
58+
You can find more bs-jest-dom examples at
59+
[wyze/bs-jest-dom/src/\_\_tests\_\_](https://github.com/wyze/bs-jest-dom/tree/master/src/__tests__).

docs/example-input-event.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ sidebar_label: Input Event
66

77
> **Note**
88
>
9-
> If you want to simulate a more natural typing behaviour while testing your component, consider the companion library [`user-event`](ecosystem-user-event.md)
9+
> If you want to simulate a more natural typing behaviour while testing your
10+
> component, consider the companion library
11+
> [`user-event`](ecosystem-user-event.md)
1012
1113
```jsx
1214
import React from 'react'

docs/learning.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ title: Learning Material
44
sidebar_label: Learning Material
55
---
66

7-
- [TestingJavaScript.com](https://testingjavascript.com) A HUGE course all about testing JavaScript applications (See also the [course material with many examples using react-testing-library](https://github.com/kentcdodds/react-testing-library-course))
7+
- [TestingJavaScript.com](https://testingjavascript.com) A HUGE course all about
8+
testing JavaScript applications (See also the
9+
[course material with many examples using react-testing-library](https://github.com/kentcdodds/react-testing-library-course))
810
by [Kent C. Dodds](https://github.com/kentcdodds)
911
- [Migrating from Enzyme shallow rendering to explicit component mocks](https://www.youtube.com/watch?v=LHUdxkThTM0&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u)
1012
- [Confident React](https://www.youtube.com/watch?v=qXRPHRgcXJ0&list=PLV5CVI1eNcJgNqzNwcs4UKrlJdhfDjshf)

website/pages/en/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ class Index extends React.Component {
208208
imageAlign: 'top',
209209
title: '[Angular Testing Library](./angular)',
210210
},
211+
{
212+
content: 'For testing ReasonReact Components',
213+
image: `${baseUrl}img/reason-200x200.png`,
214+
imageAlign: 'top',
215+
title:
216+
'[ReasonReact Testing Library](./docs/bs-react-testing-library/intro)',
217+
},
211218
{
212219
content: 'Explore the ecosystem',
213220
image: `${baseUrl}img/construction-128x128.png`,

website/sidebars.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,29 @@
4545
"type": "subcategory",
4646
"label": "Angular Testing Library",
4747
"ids": ["angular-testing-library/intro"]
48+
},
49+
{
50+
"type": "subcategory",
51+
"label": "Reason Testing Library",
52+
"ids": [
53+
"bs-dom-testing-library/intro",
54+
"bs-dom-testing-library/example-intro"
55+
]
56+
},
57+
{
58+
"type": "subcategory",
59+
"label": "ReasonReact Testing Library",
60+
"ids": [
61+
"bs-react-testing-library/intro",
62+
"bs-react-testing-library/example-intro"
63+
]
4864
}
4965
],
50-
"Ecosystem": ["ecosystem-user-event", "ecosystem-jest-dom"],
66+
"Ecosystem": [
67+
"ecosystem-user-event",
68+
"ecosystem-jest-dom",
69+
"ecosystem-bs-jest-dom"
70+
],
5171
"Help": ["faq", "learning"]
5272
}
5373
}

website/static/img/reason-200x200.png

2.46 KB
Loading

0 commit comments

Comments
 (0)