-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfireEvent.test.js
152 lines (116 loc) · 3.79 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// @flow
import React from 'react';
import {
View,
TouchableOpacity,
Text,
ScrollView,
TextInput,
} from 'react-native';
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>
);
const MyCustomButton = ({ handlePress }) => (
<OnPressComponent onPress={handlePress} />
);
const CustomEventComponentWithCustomName = ({ handlePress }) => (
<MyCustomButton testID="my-custom-button" handlePress={handlePress} />
);
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 onPress={() => 'this is not passed to children'} />
);
expect(() => fireEvent(getByTestId('text'), 'press')).toThrow(
'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.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);
});
test('custom component with custom event name', () => {
const handlePress = jest.fn();
const { getByTestId } = render(
<CustomEventComponentWithCustomName handlePress={handlePress} />
);
fireEvent(getByTestId('my-custom-button'), 'handlePress');
expect(handlePress).toHaveBeenCalled();
});
test('event with multiple handler parameters', () => {
const handlePress = jest.fn();
const { getByTestId } = render(
<CustomEventComponentWithCustomName handlePress={handlePress} />
);
fireEvent(getByTestId('my-custom-button'), 'handlePress', 'param1', 'param2');
expect(handlePress).toHaveBeenCalledWith('param1', 'param2');
});