chore: update GettingStarted example with something you can run (#284)

This commit is contained in:
Michał Pierzchała
2020-04-23 16:27:01 +02:00
committed by GitHub
parent b0e5e1baea
commit 61a83e9bb7
2 changed files with 71 additions and 12 deletions
+58
View File
@@ -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' },
});
});