Skip to content

Commit 4e148bd

Browse files
chore: tweak docs and examples to match recommended practices 2024 (use screen object for queries) (#1561)
* chore: tweak docs and examples to match modern usage of lib. Use screen object for queries (pt.1) Use screen object for queries (pt.2) update render docs to modern screen API and fix lint * refactor: code review tweaks --------- Co-authored-by: Maciej Jastrzębski <mdjastrzebski@gmail.com>
1 parent 4c6ec4f commit 4e148bd

36 files changed

+1178
-1155
lines changed

README.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,29 @@ flow-typed install react-test-renderer
8888
import { render, screen, fireEvent } from '@testing-library/react-native';
8989
import { QuestionsBoard } from '../QuestionsBoard';
9090

91-
test('form submits two answers', () => {
92-
const allQuestions = ['q1', 'q2'];
93-
const mockFn = jest.fn();
91+
// It is recommended to use userEvent with fake timers
92+
// Some events involve duration so your tests may take a long time to run.
93+
jest.useFakeTimers();
9494

95-
render(<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />);
95+
test('form submits two answers', async () => {
96+
const questions = ['q1', 'q2'];
97+
const onSubmit = jest.fn();
98+
99+
const user = userEvent.setup();
100+
render(<QuestionsBoard questions={questions} onSubmit={onSubmit} />);
96101

97102
const answerInputs = screen.getAllByLabelText('answer input');
98103

99-
fireEvent.changeText(answerInputs[0], 'a1');
100-
fireEvent.changeText(answerInputs[1], 'a2');
101-
fireEvent.press(screen.getByText('Submit'));
104+
// simulates the user focusing on TextInput and typing text one char at a time
105+
await user.type(answerInputs[0], 'a1');
106+
await user.type(answerInputs[1], 'a2');
107+
108+
// simulates the user pressing on any pressable element
109+
await user.press(screen.getByRole('button', { name: 'Submit' }));
102110

103-
expect(mockFn).toBeCalledWith({
104-
1: { q: 'q1', a: 'a1' },
105-
2: { q: 'q2', a: 'a2' },
111+
expect(onSubmit).toHaveBeenCalledWith({
112+
'1': { q: 'q1', a: 'a1' },
113+
'2': { q: 'q2', a: 'a2' },
106114
});
107115
});
108116
```

src/__tests__/__snapshots__/render-debug.test.tsx.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ exports[`debug 1`] = `
5858
onResponderTerminate={[Function onResponderTerminate]}
5959
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
6060
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
61+
role="button"
6162
>
6263
<Text>
6364
Change freshness!
@@ -137,6 +138,7 @@ exports[`debug changing component: bananaFresh button message should now be "fre
137138
onResponderTerminate={[Function onResponderTerminate]}
138139
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
139140
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
141+
role="button"
140142
>
141143
<Text>
142144
Change freshness!
@@ -343,6 +345,7 @@ exports[`debug: another custom message 1`] = `
343345
onResponderTerminate={[Function onResponderTerminate]}
344346
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
345347
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
348+
role="button"
346349
>
347350
<Text>
348351
Change freshness!
@@ -394,7 +397,6 @@ exports[`debug: shallow 1`] = `
394397
/>
395398
<MyButton
396399
onPress={[Function changeFresh]}
397-
type="primary"
398400
>
399401
Change freshness!
400402
</MyButton>
@@ -446,7 +448,6 @@ exports[`debug: shallow with message 1`] = `
446448
/>
447449
<MyButton
448450
onPress={[Function changeFresh]}
449-
type="primary"
450451
>
451452
Change freshness!
452453
</MyButton>
@@ -526,6 +527,7 @@ exports[`debug: with message 1`] = `
526527
onResponderTerminate={[Function onResponderTerminate]}
527528
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
528529
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
530+
role="button"
529531
>
530532
<Text>
531533
Change freshness!

src/__tests__/act.test.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as React from 'react';
22
import { Text } from 'react-native';
3-
import act from '../act';
4-
import render from '../render';
5-
import fireEvent from '../fire-event';
3+
import { act, fireEvent, render, screen } from '../';
64

75
type UseEffectProps = { callback(): void };
86
const UseEffect = ({ callback }: UseEffectProps) => {
@@ -26,15 +24,15 @@ test('render should trigger useEffect', () => {
2624

2725
test('update should trigger useEffect', () => {
2826
const effectCallback = jest.fn();
29-
const { update } = render(<UseEffect callback={effectCallback} />);
30-
update(<UseEffect callback={effectCallback} />);
27+
render(<UseEffect callback={effectCallback} />);
28+
screen.update(<UseEffect callback={effectCallback} />);
3129

3230
expect(effectCallback).toHaveBeenCalledTimes(2);
3331
});
3432

3533
test('fireEvent should trigger useState', () => {
36-
const { getByText } = render(<Counter />);
37-
const counter = getByText(/Total count/i);
34+
render(<Counter />);
35+
const counter = screen.getByText(/Total count/i);
3836

3937
expect(counter.props.children).toEqual('Total count: 0');
4038
fireEvent.press(counter);

src/__tests__/fire-event-textInput.test.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Text, TextInput, TextInputProps } from 'react-native';
3-
import { render, fireEvent } from '..';
3+
import { render, fireEvent, screen } from '..';
44

55
function WrappedTextInput(props: TextInputProps) {
66
return <TextInput {...props} />;
@@ -18,7 +18,7 @@ test('should fire only non-touch-related events on non-editable TextInput', () =
1818
const onSubmitEditing = jest.fn();
1919
const onLayout = jest.fn();
2020

21-
const view = render(
21+
render(
2222
<TextInput
2323
editable={false}
2424
testID="subject"
@@ -29,7 +29,7 @@ test('should fire only non-touch-related events on non-editable TextInput', () =
2929
/>
3030
);
3131

32-
const subject = view.getByTestId('subject');
32+
const subject = screen.getByTestId('subject');
3333
fireEvent(subject, 'focus');
3434
fireEvent.changeText(subject, 'Text');
3535
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
@@ -47,7 +47,7 @@ test('should fire only non-touch-related events on non-editable TextInput with n
4747
const onSubmitEditing = jest.fn();
4848
const onLayout = jest.fn();
4949

50-
const view = render(
50+
render(
5151
<TextInput
5252
editable={false}
5353
testID="subject"
@@ -60,7 +60,7 @@ test('should fire only non-touch-related events on non-editable TextInput with n
6060
</TextInput>
6161
);
6262

63-
const subject = view.getByText('Nested Text');
63+
const subject = screen.getByText('Nested Text');
6464
fireEvent(subject, 'focus');
6565
fireEvent(subject, 'onFocus');
6666
fireEvent.changeText(subject, 'Text');
@@ -98,7 +98,7 @@ test('should fire only non-touch-related events on non-editable wrapped TextInpu
9898
const onSubmitEditing = jest.fn();
9999
const onLayout = jest.fn();
100100

101-
const view = render(
101+
render(
102102
<WrappedTextInput
103103
editable={false}
104104
testID="subject"
@@ -109,7 +109,7 @@ test('should fire only non-touch-related events on non-editable wrapped TextInpu
109109
/>
110110
);
111111

112-
const subject = view.getByTestId('subject');
112+
const subject = screen.getByTestId('subject');
113113
fireEvent(subject, 'focus');
114114
fireEvent.changeText(subject, 'Text');
115115
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
@@ -130,7 +130,7 @@ test('should fire only non-touch-related events on non-editable double wrapped T
130130
const onSubmitEditing = jest.fn();
131131
const onLayout = jest.fn();
132132

133-
const view = render(
133+
render(
134134
<DoubleWrappedTextInput
135135
editable={false}
136136
testID="subject"
@@ -141,7 +141,7 @@ test('should fire only non-touch-related events on non-editable double wrapped T
141141
/>
142142
);
143143

144-
const subject = view.getByTestId('subject');
144+
const subject = screen.getByTestId('subject');
145145
fireEvent(subject, 'focus');
146146
fireEvent.changeText(subject, 'Text');
147147
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });

0 commit comments

Comments
 (0)