Skip to content

feat: export container from render #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"peerDependencies": {
"react": ">=16.0.0",
"react-native": ">=0.59",
"react-test-renderer": ">=16.0.0"
},
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,25 @@ test('renders options.wrapper around updated node', () => {
</RCTSafeAreaView>
`);
});

test('returns container', () => {
const { container } = render(<View testID="inner" />);

expect(container).toBeDefined();
expect(container.type).toBe(View);
expect(container.props.testID).toBe('inner');
});

test('returns wrapped component as container', () => {
const WrapperComponent = ({ children }) => (
<SafeAreaView testID="wrapper">{children}</SafeAreaView>
);

const { container } = render(<View testID="inner" />, {
wrapper: WrapperComponent,
});

expect(container).toBeDefined();
expect(container.type).toBe(WrapperComponent);
expect(container.props.testID).not.toBeDefined();
});
1 change: 0 additions & 1 deletion src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const isHostElement = (element?: ReactTestInstance) => {
};

const isTextInput = (element?: ReactTestInstance) => {
// eslint-disable-next-line import/no-extraneous-dependencies
const { TextInput } = require('react-native');
return element?.type === TextInput;
};
Expand Down
3 changes: 0 additions & 3 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const filterNodeByType = (node, type) => node.type === type;

const getNodeByText = (node, text) => {
try {
// eslint-disable-next-line
const { Text } = require('react-native');
const isTextComponent = filterNodeByType(node, Text);
if (isTextComponent) {
Expand Down Expand Up @@ -61,7 +60,6 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {

const getTextInputNodeByPlaceholderText = (node, placeholder) => {
try {
// eslint-disable-next-line
const { TextInput } = require('react-native');
return (
filterNodeByType(node, TextInput) &&
Expand All @@ -76,7 +74,6 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => {

const getTextInputNodeByDisplayValue = (node, value) => {
try {
// eslint-disable-next-line
const { TextInput } = require('react-native');
return (
filterNodeByType(node, TextInput) &&
Expand Down
1 change: 1 addition & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function render<T>(
...findByAPI(instance),
...a11yAPI(instance),
update,
container: instance,
rerender: update, // alias for `update`
unmount: renderer.unmount,
toJSON: renderer.toJSON,
Expand Down
8 changes: 8 additions & 0 deletions website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ toJSON(): ReactTestRendererJSON | null

Get the rendered component JSON representation, e.g. for snapshot testing.

### `container`

```ts
container: ReactTestInstance;
```

A reference to the rendered root element.

## `cleanup`

```ts
Expand Down