-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathfireEvent.test.js
132 lines (102 loc) · 3.22 KB
/
fireEvent.test.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @flow
import React from 'react';
import {
View,
TouchableOpacity,
Text,
ScrollView,
TextInput,
} from '../__mocks__/reactNativeMock';
import { render, fireEvent } from '..';
const OnPressComponent = ({ onPress }) => (
<View>
<TouchableOpacity onPress={onPress} testID="button">
<Text testID="text-button">Press me</Text>
</TouchableOpacity>
</View>
);
const WithoutEventComponent = () => (
<View>
<Text testID="text">Content</Text>
</View>
);
const CustomEventComponent = ({ onCustomEvent }) => (
<TouchableOpacity onPress={onCustomEvent}>
<Text>Click me</Text>
</TouchableOpacity>
);
describe('fireEvent', () => {
test('should invoke specified event', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />);
fireEvent(getByTestId('button'), 'press');
expect(onPressMock).toHaveBeenCalled();
});
test('should invoke specified event on parent element', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />);
fireEvent(getByTestId('text-button'), 'press');
expect(onPressMock).toHaveBeenCalled();
});
test('should throw an Error when event handler was not found', () => {
const { getByTestId } = render(<WithoutEventComponent />);
expect(() => fireEvent(getByTestId('text'), 'press')).toThrowError(
'No handler function found for event: press'
);
});
test('should invoke event with custom name', () => {
const handlerMock = jest.fn();
const EVENT_DATA = 'event data';
const { getByTestId } = render(
<View>
<CustomEventComponent testID="custom" onCustomEvent={handlerMock} />
</View>
);
fireEvent(getByTestId('custom'), 'customEvent', EVENT_DATA);
expect(handlerMock).toHaveBeenCalledWith(EVENT_DATA);
});
});
test('fireEvent.press', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(<OnPressComponent onPress={onPressMock} />);
fireEvent.press(getByTestId('text-button'));
expect(onPressMock).toHaveBeenCalled();
});
test('fireEvent.scroll', () => {
const onScrollMock = jest.fn();
const eventData = {
nativeEvent: {
contentOffset: {
y: 200,
},
},
};
const { getByTestId } = render(
<ScrollView testID="scroll-view" onScroll={onScrollMock}>
<Text>XD</Text>
</ScrollView>
);
fireEvent.scroll(getByTestId('scroll-view'), eventData);
expect(onScrollMock).toHaveBeenCalledWith(eventData);
});
test('fireEvent.doublePress', () => {
const onDoublePressMock = jest.fn();
const { getByTestId } = render(
<TouchableOpacity onDoublePress={onDoublePressMock}>
<Text testID="button-text">Click me</Text>
</TouchableOpacity>
);
fireEvent.doublePress(getByTestId('button-text'));
expect(onDoublePressMock).toHaveBeenCalled();
});
test('fireEvent.changeText', () => {
const onChangeTextMock = jest.fn();
const CHANGE_TEXT = 'content';
const { getByTestId } = render(
<View>
<TextInput testID="text-input" onChangeText={onChangeTextMock} />
</View>
);
fireEvent.changeText(getByTestId('text-input'), CHANGE_TEXT);
expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
});