-
Notifications
You must be signed in to change notification settings - Fork 273
/
questionsBoard.test.tsx
57 lines (49 loc) · 1.54 KB
/
questionsBoard.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as React from 'react';
import { Pressable, ScrollView, Text, TextInput, View } from 'react-native';
import { render, screen, userEvent } from '..';
type QuestionsBoardProps = {
questions: string[];
onSubmit: (obj: {}) => void;
};
jest.useFakeTimers();
function QuestionsBoard({ questions, onSubmit }: QuestionsBoardProps) {
const [data, setData] = React.useState({});
return (
<ScrollView>
{questions.map((q, index) => {
return (
<View key={q}>
<Text>{q}</Text>
<TextInput
accessibilityLabel="answer input"
accessibilityHint="input"
onChangeText={(text) => {
setData((state) => ({
...state,
[index + 1]: { q, a: text },
}));
}}
/>
</View>
);
})}
<Pressable accessibilityRole={'button'} onPress={() => onSubmit(data)}>
<Text>Submit</Text>
</Pressable>
</ScrollView>
);
}
test('form submits two answers', async () => {
const questions = ['q1', 'q2'];
const onSubmit = jest.fn();
const user = userEvent.setup();
render(<QuestionsBoard questions={questions} onSubmit={onSubmit} />);
const answerInputs = screen.getAllByLabelText('answer input');
await user.type(answerInputs[0], 'a1');
await user.type(answerInputs[1], 'a2');
await user.press(screen.getByRole('button', { name: 'Submit' }));
expect(onSubmit).toHaveBeenCalledWith({
'1': { q: 'q1', a: 'a1' },
'2': { q: 'q2', a: 'a2' },
});
});