mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
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
This commit is contained in:
committed by
GitHub
parent
61f6895b32
commit
07d3be1c51
@@ -136,6 +136,10 @@ Unmount the in-memory tree, triggering the appropriate lifecycle events
|
|||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
||||||
|
### `toJSON: () => ?ReactTestRendererJSON`
|
||||||
|
|
||||||
|
Get the rendered component JSON representation, e.g. for snapshot testing.
|
||||||
|
|
||||||
## `shallow`
|
## `shallow`
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
|||||||
@@ -30,9 +30,6 @@
|
|||||||
"react": ">=16.0.0",
|
"react": ">=16.0.0",
|
||||||
"react-test-renderer": ">= 16.0.0"
|
"react-test-renderer": ">= 16.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
|
||||||
"react-is": "^16.5.2"
|
|
||||||
},
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"flow-check": "flow check",
|
"flow-check": "flow check",
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`toJSON 1`] = `"press me"`;
|
||||||
@@ -161,3 +161,9 @@ test('unmount', () => {
|
|||||||
unmount();
|
unmount();
|
||||||
expect(fn).toHaveBeenCalled();
|
expect(fn).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('toJSON', () => {
|
||||||
|
const { toJSON } = render(<Button>press me</Button>);
|
||||||
|
|
||||||
|
expect(toJSON()).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|||||||
@@ -20,5 +20,6 @@ export default function render(
|
|||||||
...queryByAPI(instance),
|
...queryByAPI(instance),
|
||||||
update: renderer.update,
|
update: renderer.update,
|
||||||
unmount: renderer.unmount,
|
unmount: renderer.unmount,
|
||||||
|
toJSON: renderer.toJSON,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-9
@@ -1,6 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { isValidElementType } from 'react-is';
|
|
||||||
import ShallowRenderer from 'react-test-renderer/shallow'; // eslint-disable-line import/no-extraneous-dependencies
|
import ShallowRenderer from 'react-test-renderer/shallow'; // eslint-disable-line import/no-extraneous-dependencies
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,15 +9,10 @@ export default function shallow(
|
|||||||
instance: ReactTestInstance | React.Element<*>
|
instance: ReactTestInstance | React.Element<*>
|
||||||
) {
|
) {
|
||||||
const renderer = new ShallowRenderer();
|
const renderer = new ShallowRenderer();
|
||||||
if (isValidElementType(instance)) {
|
|
||||||
// $FlowFixMe - instance is React.Element<*> in this branch
|
renderer.render(React.createElement(instance.type, instance.props));
|
||||||
renderer.render(instance);
|
|
||||||
} else {
|
|
||||||
renderer.render(React.createElement(instance.type, instance.props));
|
|
||||||
}
|
|
||||||
const output = renderer.getRenderOutput();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
output,
|
output: renderer.getRenderOutput(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+2
-1
@@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { ReactTestInstance } from 'react-test-renderer';
|
import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer';
|
||||||
|
|
||||||
export interface GetByAPI {
|
export interface GetByAPI {
|
||||||
getByName: (name: React.ReactType) => ReactTestInstance;
|
getByName: (name: React.ReactType) => ReactTestInstance;
|
||||||
@@ -30,6 +30,7 @@ export interface RenderOptions {
|
|||||||
export interface RenderAPI extends GetByAPI, QueryByAPI {
|
export interface RenderAPI extends GetByAPI, QueryByAPI {
|
||||||
update(nextElement: React.ReactElement<any>): void;
|
update(nextElement: React.ReactElement<any>): void;
|
||||||
unmount(nextElement?: React.ReactElement<any>): void;
|
unmount(nextElement?: React.ReactElement<any>): void;
|
||||||
|
toJSON(): ReactTestRendererJSON | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FireEventFunction = (
|
export type FireEventFunction = (
|
||||||
|
|||||||
Reference in New Issue
Block a user