Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,30 @@ This library is a replacement for [Enzyme](http://airbnb.io/enzyme/).
```jsx
import { render, fireEvent } from 'react-native-testing-library';
import { QuestionsBoard } from '../QuestionsBoard';
import { Question } from '../Question';

function setAnswer(question, answer) {
fireEvent.changeText(question, answer);
}
test('form submits two answers', () => {
const allQuestions = ['q1', 'q2'];
const mockFn = jest.fn();

test('should verify two questions', () => {
jest.spyOn(props, 'verifyQuestions');
const { getAllByA11yRole, getByText } = render(<QuestionsBoard {...props} />);
const allQuestions = getAllByA11yRole('header');
const { getAllByA11yLabel, getByText } = render(
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
);

setAnswer(allQuestions[0], 'a1');
setAnswer(allQuestions[1], 'a2');
const answerInputs = getAllByA11yLabel('answer input');

fireEvent.press(getByText('submit'));
fireEvent.changeText(answerInputs[0], 'a1');
fireEvent.changeText(answerInputs[1], 'a2');
fireEvent.press(getByText('Submit'));

expect(props.verifyQuestions).toBeCalledWith({
expect(mockFn).toBeCalledWith({
'1': { q: 'q1', a: 'a1' },
'2': { q: 'q2', a: 'a2' },
});
});
```

You can find the source of `QuestionsBoard` component and this example [here](../src/__tests__/questionsBoard.test.js).

## Installation

Open a Terminal in your project's folder and run:
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/questionsBoard.test.js
Original file line number Diff line number Diff line change
@@ -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' },
});
});