Skip to content

Commit 78fb456

Browse files
klekowskimthymikee
andauthored
feat: export container from render (#567)
* feat: export root test instance from render * test: add tests for root test instance returned from render function * docs: add description about root test instance returned from render function * feat: change root export to container and wrap component in View if no wrapper defined * docs: update description about container test instance returned from render function * test: update tests for root test instance returned from render function * test: clean up comments in tests * feat: add react-native as peerDependency, clean up lint ignore for lines that was importing react-native * revert adding default wrapper * rework tests * reword docs * add testID checks * add comments Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 696b28b commit 78fb456

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
},
5757
"peerDependencies": {
5858
"react": ">=16.0.0",
59+
"react-native": ">=0.59",
5960
"react-test-renderer": ">=16.0.0"
6061
},
6162
"scripts": {

src/__tests__/render.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,29 @@ test('renders options.wrapper around updated node', () => {
382382
</RCTSafeAreaView>
383383
`);
384384
});
385+
386+
test('returns container', () => {
387+
const { container } = render(<View testID="inner" />);
388+
389+
expect(container).toBeDefined();
390+
// `View` composite component is returned. This behavior will break if we
391+
// start returning only host components.
392+
expect(container.type).toBe(View);
393+
expect(container.props.testID).toBe('inner');
394+
});
395+
396+
test('returns wrapped component as container', () => {
397+
const WrapperComponent = ({ children }) => (
398+
<SafeAreaView testID="wrapper">{children}</SafeAreaView>
399+
);
400+
401+
const { container } = render(<View testID="inner" />, {
402+
wrapper: WrapperComponent,
403+
});
404+
405+
expect(container).toBeDefined();
406+
// `WrapperComponent` composite component is returned with no testID passed to
407+
// it. This behavior will break if we start returning only host components.
408+
expect(container.type).toBe(WrapperComponent);
409+
expect(container.props.testID).not.toBeDefined();
410+
});

src/fireEvent.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const isHostElement = (element?: ReactTestInstance) => {
77
};
88

99
const isTextInput = (element?: ReactTestInstance) => {
10-
// eslint-disable-next-line import/no-extraneous-dependencies
1110
const { TextInput } = require('react-native');
1211
return element?.type === TextInput;
1312
};

src/helpers/getByAPI.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const filterNodeByType = (node, type) => node.type === type;
1313

1414
const getNodeByText = (node, text) => {
1515
try {
16-
// eslint-disable-next-line
1716
const { Text } = require('react-native');
1817
const isTextComponent = filterNodeByType(node, Text);
1918
if (isTextComponent) {
@@ -61,7 +60,6 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {
6160

6261
const getTextInputNodeByPlaceholderText = (node, placeholder) => {
6362
try {
64-
// eslint-disable-next-line
6563
const { TextInput } = require('react-native');
6664
return (
6765
filterNodeByType(node, TextInput) &&
@@ -76,7 +74,6 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => {
7674

7775
const getTextInputNodeByDisplayValue = (node, value) => {
7876
try {
79-
// eslint-disable-next-line
8077
const { TextInput } = require('react-native');
8178
return (
8279
filterNodeByType(node, TextInput) &&

src/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default function render<T>(
4444
...findByAPI(instance),
4545
...a11yAPI(instance),
4646
update,
47+
container: instance,
4748
rerender: update, // alias for `update`
4849
unmount: renderer.unmount,
4950
toJSON: renderer.toJSON,

website/docs/API.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ toJSON(): ReactTestRendererJSON | null
118118

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

121+
### `container`
122+
123+
```ts
124+
container: ReactTestInstance;
125+
```
126+
127+
A reference to the rendered root element.
128+
121129
## `cleanup`
122130

123131
```ts

0 commit comments

Comments
 (0)