mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
chore: update GettingStarted example with something you can run (#284)
This commit is contained in:
committed by
GitHub
parent
b0e5e1baea
commit
61a83e9bb7
@@ -0,0 +1,58 @@
|
||||
// @flow
|
||||
import React from 'react';
|
||||
import {
|
||||
View,
|
||||
TouchableOpacity,
|
||||
Text,
|
||||
ScrollView,
|
||||
TextInput,
|
||||
} from 'react-native';
|
||||
import { render, fireEvent } from '..';
|
||||
|
||||
function QuestionsBoard({ questions, onSubmit }) {
|
||||
const [data, setData] = React.useState({});
|
||||
|
||||
return (
|
||||
<ScrollView>
|
||||
{questions.map((q, index) => {
|
||||
return (
|
||||
<View key={q}>
|
||||
<Text>{q}</Text>
|
||||
<TextInput
|
||||
accessibilityLabel="answer input"
|
||||
onChangeText={text => {
|
||||
setData(state => ({
|
||||
...state,
|
||||
[index + 1]: { q, a: text },
|
||||
}));
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
})}
|
||||
<TouchableOpacity onPress={() => onSubmit(data)}>
|
||||
<Text>Submit</Text>
|
||||
</TouchableOpacity>
|
||||
</ScrollView>
|
||||
);
|
||||
}
|
||||
|
||||
test('form submits two answers', () => {
|
||||
const allQuestions = ['q1', 'q2'];
|
||||
const mockFn = jest.fn();
|
||||
|
||||
const { getAllByA11yLabel, getByText } = render(
|
||||
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
|
||||
);
|
||||
|
||||
const answerInputs = getAllByA11yLabel('answer input');
|
||||
|
||||
fireEvent.changeText(answerInputs[0], 'a1');
|
||||
fireEvent.changeText(answerInputs[1], 'a2');
|
||||
fireEvent.press(getByText('Submit'));
|
||||
|
||||
expect(mockFn).toBeCalledWith({
|
||||
'1': { q: 'q1', a: 'a1' },
|
||||
'2': { q: 'q2', a: 'a2' },
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user