Skip to content

Commit 07d3be1

Browse files
authored
feat: add toJSON to render; cleanup shallow (#35)
* feat: add toJSON to render; cleanup shallow * Remove empty dependencies entry * revert shallow import * add docs * add TS typings
1 parent 61f6895 commit 07d3be1

File tree

7 files changed

+19
-13
lines changed

7 files changed

+19
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ Unmount the in-memory tree, triggering the appropriate lifecycle events
136136

137137
When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases it's convenient to create your custom `render` method. [Follow this great guide on how to set this up](https://github.com/kentcdodds/react-testing-library#custom-render).
138138

139+
### `toJSON: () => ?ReactTestRendererJSON`
140+
141+
Get the rendered component JSON representation, e.g. for snapshot testing.
142+
139143
## `shallow`
140144

141145
Shallowly render given React component. Since it doesn't return helpers to query the output, it's mostly advised to used for snapshot testing (short snapshots are best for code reviewers).

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
"react": ">=16.0.0",
3131
"react-test-renderer": ">= 16.0.0"
3232
},
33-
"dependencies": {
34-
"react-is": "^16.5.2"
35-
},
3633
"scripts": {
3734
"test": "jest",
3835
"flow-check": "flow check",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`toJSON 1`] = `"press me"`;

src/__tests__/render.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,9 @@ test('unmount', () => {
161161
unmount();
162162
expect(fn).toHaveBeenCalled();
163163
});
164+
165+
test('toJSON', () => {
166+
const { toJSON } = render(<Button>press me</Button>);
167+
168+
expect(toJSON()).toMatchSnapshot();
169+
});

src/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export default function render(
2020
...queryByAPI(instance),
2121
update: renderer.update,
2222
unmount: renderer.unmount,
23+
toJSON: renderer.toJSON,
2324
};
2425
}

src/shallow.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22
import * as React from 'react';
3-
import { isValidElementType } from 'react-is';
43
import ShallowRenderer from 'react-test-renderer/shallow'; // eslint-disable-line import/no-extraneous-dependencies
54

65
/**
@@ -10,15 +9,10 @@ export default function shallow(
109
instance: ReactTestInstance | React.Element<*>
1110
) {
1211
const renderer = new ShallowRenderer();
13-
if (isValidElementType(instance)) {
14-
// $FlowFixMe - instance is React.Element<*> in this branch
15-
renderer.render(instance);
16-
} else {
17-
renderer.render(React.createElement(instance.type, instance.props));
18-
}
19-
const output = renderer.getRenderOutput();
12+
13+
renderer.render(React.createElement(instance.type, instance.props));
2014

2115
return {
22-
output,
16+
output: renderer.getRenderOutput(),
2317
};
2418
}

typings/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { ReactTestInstance } from 'react-test-renderer';
2+
import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer';
33

44
export interface GetByAPI {
55
getByName: (name: React.ReactType) => ReactTestInstance;
@@ -30,6 +30,7 @@ export interface RenderOptions {
3030
export interface RenderAPI extends GetByAPI, QueryByAPI {
3131
update(nextElement: React.ReactElement<any>): void;
3232
unmount(nextElement?: React.ReactElement<any>): void;
33+
toJSON(): ReactTestRendererJSON | null;
3334
}
3435

3536
export type FireEventFunction = (

0 commit comments

Comments
 (0)