Skip to content

Commit 9b0e6e9

Browse files
mdjastrzebskithymikee
authored andcommitted
test rerender
1 parent 8ab31c1 commit 9b0e6e9

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/__tests__/screen.test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { Text } from 'react-native';
2+
import { View, Text } from 'react-native';
33
import { render, screen } from '..';
44

55
test('screen has the same queries as render result', () => {
@@ -23,6 +23,34 @@ test('screen holds last render result', () => {
2323
expect(screen.queryByText('Mt. Blanc')).toBeFalsy();
2424
});
2525

26+
test('screen works with updating rerender', () => {
27+
const result = render(<Text>Mt. Everest</Text>);
28+
expect(screen).toBe(result);
29+
30+
screen.rerender(<Text>Śnieżka</Text>);
31+
expect(screen).toBe(result);
32+
expect(screen.getByText('Śnieżka')).toBeTruthy();
33+
});
34+
35+
test('screen works with nested re-mounting rerender', () => {
36+
const result = render(
37+
<View>
38+
<Text>Mt. Everest</Text>
39+
</View>
40+
);
41+
expect(screen).toBe(result);
42+
43+
screen.rerender(
44+
<View>
45+
<View>
46+
<Text>Śnieżka</Text>
47+
</View>
48+
</View>
49+
);
50+
expect(screen).toBe(result);
51+
expect(screen.getByText('Śnieżka')).toBeTruthy();
52+
});
53+
2654
test('screen throws without render', () => {
2755
expect(() => screen.container).toThrowError(
2856
'`render` method has not been called'

website/docs/Queries.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ Returns a `ReactTestInstance` with matching text – may be a string or regular
9696
This method will join `<Text>` siblings to find matches, similarly to [how React Native handles these components](https://reactnative.dev/docs/text#containers). This will allow for querying for strings that will be visually rendered together, but may be semantically separate React components.
9797

9898
```jsx
99-
import { render } from '@testing-library/react-native';
99+
import { render, screen } from '@testing-library/react-native';
100100

101-
const { getByText } = render(<MyComponent />);
102-
const element = getByText('banana');
101+
render(<MyComponent />);
102+
const element = screen.getByText('banana');
103103
```
104104

105105
### `ByPlaceholderText`
@@ -109,10 +109,10 @@ const element = getByText('banana');
109109
Returns a `ReactTestInstance` for a `TextInput` with a matching placeholder – may be a string or regular expression.
110110

111111
```jsx
112-
import { render } from '@testing-library/react-native';
112+
import { render, screen } from '@testing-library/react-native';
113113

114-
const { getByPlaceholderText } = render(<MyComponent />);
115-
const element = getByPlaceholderText('username');
114+
render(<MyComponent />);
115+
const element = screen.getByPlaceholderText('username');
116116
```
117117

118118
### `ByDisplayValue`
@@ -122,10 +122,10 @@ const element = getByPlaceholderText('username');
122122
Returns a `ReactTestInstance` for a `TextInput` with a matching display value – may be a string or regular expression.
123123

124124
```jsx
125-
import { render } from '@testing-library/react-native';
125+
import { render, screen } from '@testing-library/react-native';
126126

127-
const { getByDisplayValue } = render(<MyComponent />);
128-
const element = getByDisplayValue('username');
127+
render(<MyComponent />);
128+
const element = screen.getByDisplayValue('username');
129129
```
130130

131131
### `ByTestId`
@@ -135,10 +135,10 @@ const element = getByDisplayValue('username');
135135
Returns a `ReactTestInstance` with matching `testID` prop. `testID` – may be a string or a regular expression.
136136

137137
```jsx
138-
import { render } from '@testing-library/react-native';
138+
import { render, screen } from '@testing-library/react-native';
139139

140-
const { getByTestId } = render(<MyComponent />);
141-
const element = getByTestId('unique-id');
140+
render(<MyComponent />);
141+
const element = screen.getByTestId('unique-id');
142142
```
143143

144144
:::info

0 commit comments

Comments
 (0)