Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Commit 5bd4e08

Browse files
committed
add asJSON to the docs
1 parent 0e7e29c commit 5bd4e08

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

docs/api-main.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ const { unmount } = render(<Login />);
149149
unmount();
150150
```
151151

152+
### asJSON
153+
154+
Returns a JSON representation of the current state of your rendered component. This can be useful if
155+
you need to avoid live bindings and see how your component reacts to events.
156+
157+
```javascript
158+
import { render, fireEvent } from 'native-testing-library';
159+
160+
function TestComponent() {
161+
const [count, setCount] = React.useState(0);
162+
163+
return (
164+
<Button onPress={() => setCount(state => state + 1)} title={`Click to increase: ${count}`} />
165+
);
166+
}
167+
168+
const { getByText, asJSON } = render(<TestComponent />);
169+
const firstRender = asJSON();
170+
171+
fireEvent.press(getByText(/Click to increase/));
172+
173+
// This will snapshot the difference before and after the press event.
174+
// See https://github.com/jest-community/snapshot-diff
175+
expect(firstRender).toMatchDiffSnapshot(asJSON());
176+
```
177+
152178
---
153179

154180
## `cleanup`

website/versioned_docs/version-3.0.0/api-main.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,32 @@ const { unmount } = render(<Login />);
150150
unmount();
151151
```
152152

153+
### asJSON
154+
155+
Returns a JSON representation of the current state of your rendered component. This can be useful if
156+
you need to avoid live bindings and see how your component reacts to events.
157+
158+
```javascript
159+
import { render, fireEvent } from 'native-testing-library';
160+
161+
function TestComponent() {
162+
const [count, setCount] = React.useState(0);
163+
164+
return (
165+
<Button onPress={() => setCount(state => state + 1)} title={`Click to increase: ${count}`} />
166+
);
167+
}
168+
169+
const { getByText, asJSON } = render(<TestComponent />);
170+
const firstRender = asJSON();
171+
172+
fireEvent.press(getByText(/Click to increase/));
173+
174+
// This will snapshot the difference before and after the press event.
175+
// See https://github.com/jest-community/snapshot-diff
176+
expect(firstRender).toMatchDiffSnapshot(asJSON());
177+
```
178+
153179
---
154180

155181
## `cleanup`

0 commit comments

Comments
 (0)