Skip to content

Commit b330aae

Browse files
committed
add tests
1 parent 45067f8 commit b330aae

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

src/__tests__/fire-event.test.tsx

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const verticalScrollEvent = { nativeEvent: { contentOffset: { y: 200 } } };
2121
const horizontalScrollEvent = { nativeEvent: { contentOffset: { x: 50 } } };
2222
const pressEventData = { nativeEvent: { pageX: 20, pageY: 30 } };
2323

24+
beforeEach(() => {
25+
jest.spyOn(Date, 'now').mockImplementation(() => 100100100100);
26+
});
27+
2428
test('fireEvent accepts event name with or without "on" prefix', async () => {
2529
const onPress = jest.fn();
2630
await render(<Pressable testID="btn" onPress={onPress} />);
@@ -65,6 +69,71 @@ test('fireEvent bubbles event to parent handler', async () => {
6569
});
6670

6771
describe('fireEvent.press', () => {
72+
test('passes default press event object to handler', async () => {
73+
const onPress = jest.fn();
74+
await render(<Pressable testID="btn" onPress={onPress} />);
75+
await fireEvent.press(screen.getByTestId('btn'));
76+
expect(onPress.mock.calls[0][0]).toMatchInlineSnapshot(`
77+
{
78+
"currentTarget": {
79+
"measure": [Function],
80+
},
81+
"isDefaultPrevented": [Function],
82+
"isPersistent": [Function],
83+
"isPropagationStopped": [Function],
84+
"nativeEvent": {
85+
"changedTouches": [],
86+
"identifier": 0,
87+
"locationX": 0,
88+
"locationY": 0,
89+
"pageX": 0,
90+
"pageY": 0,
91+
"target": 0,
92+
"timestamp": 100100100100,
93+
"touches": [],
94+
},
95+
"persist": [Function],
96+
"preventDefault": [Function],
97+
"stopPropagation": [Function],
98+
"target": {},
99+
"timeStamp": 0,
100+
}
101+
`);
102+
});
103+
104+
test('overrides default event properties with passed event props', async () => {
105+
const onPress = jest.fn();
106+
await render(<Pressable testID="btn" onPress={onPress} />);
107+
const customEventData = { nativeEvent: { pageX: 20, pageY: 30 } };
108+
await fireEvent.press(screen.getByTestId('btn'), customEventData);
109+
expect(onPress.mock.calls[0][0]).toMatchInlineSnapshot(`
110+
{
111+
"currentTarget": {
112+
"measure": [Function],
113+
},
114+
"isDefaultPrevented": [Function],
115+
"isPersistent": [Function],
116+
"isPropagationStopped": [Function],
117+
"nativeEvent": {
118+
"changedTouches": [],
119+
"identifier": 0,
120+
"locationX": 0,
121+
"locationY": 0,
122+
"pageX": 20,
123+
"pageY": 30,
124+
"target": 0,
125+
"timestamp": 100100100100,
126+
"touches": [],
127+
},
128+
"persist": [Function],
129+
"preventDefault": [Function],
130+
"stopPropagation": [Function],
131+
"target": {},
132+
"timeStamp": 0,
133+
}
134+
`);
135+
});
136+
68137
test.each([
69138
['Pressable', Pressable],
70139
['TouchableOpacity', TouchableOpacity],
@@ -106,6 +175,107 @@ describe('fireEvent.changeText', () => {
106175
});
107176

108177
describe('fireEvent.scroll', () => {
178+
test('passes default scroll event object to handler', async () => {
179+
const onScroll = jest.fn();
180+
await render(
181+
<ScrollView testID="scroll" onScroll={onScroll}>
182+
<Text>Content</Text>
183+
</ScrollView>,
184+
);
185+
const scrollView = screen.getByTestId('scroll');
186+
await fireEvent.scroll(scrollView);
187+
expect(onScroll.mock.calls[0][0]).toMatchInlineSnapshot(`
188+
{
189+
"currentTarget": {},
190+
"isDefaultPrevented": [Function],
191+
"isPersistent": [Function],
192+
"isPropagationStopped": [Function],
193+
"nativeEvent": {
194+
"contentInset": {
195+
"bottom": 0,
196+
"left": 0,
197+
"right": 0,
198+
"top": 0,
199+
},
200+
"contentOffset": {
201+
"x": 0,
202+
"y": 0,
203+
},
204+
"contentSize": {
205+
"height": 0,
206+
"width": 0,
207+
},
208+
"layoutMeasurement": {
209+
"height": 0,
210+
"width": 0,
211+
},
212+
"responderIgnoreScroll": true,
213+
"target": 0,
214+
"velocity": {
215+
"x": 0,
216+
"y": 0,
217+
},
218+
},
219+
"persist": [Function],
220+
"preventDefault": [Function],
221+
"stopPropagation": [Function],
222+
"target": {},
223+
"timeStamp": 0,
224+
}
225+
`);
226+
});
227+
228+
test('overrides default event properties with passed event props', async () => {
229+
const onScroll = jest.fn();
230+
await render(
231+
<ScrollView testID="scroll" onScroll={onScroll}>
232+
<Text>Content</Text>
233+
</ScrollView>,
234+
);
235+
const scrollView = screen.getByTestId('scroll');
236+
const customEventData = { nativeEvent: { contentOffset: { x: 50, y: 200 } } };
237+
await fireEvent.scroll(scrollView, customEventData);
238+
expect(onScroll.mock.calls[0][0]).toMatchInlineSnapshot(`
239+
{
240+
"currentTarget": {},
241+
"isDefaultPrevented": [Function],
242+
"isPersistent": [Function],
243+
"isPropagationStopped": [Function],
244+
"nativeEvent": {
245+
"contentInset": {
246+
"bottom": 0,
247+
"left": 0,
248+
"right": 0,
249+
"top": 0,
250+
},
251+
"contentOffset": {
252+
"x": 50,
253+
"y": 200,
254+
},
255+
"contentSize": {
256+
"height": 0,
257+
"width": 0,
258+
},
259+
"layoutMeasurement": {
260+
"height": 0,
261+
"width": 0,
262+
},
263+
"responderIgnoreScroll": true,
264+
"target": 0,
265+
"velocity": {
266+
"x": 0,
267+
"y": 0,
268+
},
269+
},
270+
"persist": [Function],
271+
"preventDefault": [Function],
272+
"stopPropagation": [Function],
273+
"target": {},
274+
"timeStamp": 0,
275+
}
276+
`);
277+
});
278+
109279
test('works on ScrollView', async () => {
110280
const onScroll = jest.fn();
111281
await render(

0 commit comments

Comments
 (0)