-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfire-event-textInput.test.tsx
156 lines (136 loc) · 4.79 KB
/
fire-event-textInput.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
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
153
154
155
156
import React from 'react';
import type { TextInputProps } from 'react-native';
import { Text, TextInput } from 'react-native';
import { fireEvent, render, screen } from '..';
function WrappedTextInput(props: TextInputProps) {
return <TextInput {...props} />;
}
function DoubleWrappedTextInput(props: TextInputProps) {
return <WrappedTextInput {...props} />;
}
const layoutEvent = { nativeEvent: { layout: { width: 100, height: 100 } } };
test('should fire only non-touch-related events on non-editable TextInput', () => {
const onFocus = jest.fn();
const onChangeText = jest.fn();
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
render(
<TextInput
editable={false}
testID="subject"
onFocus={onFocus}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
onLayout={onLayout}
/>,
);
const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'layout', layoutEvent);
expect(onFocus).not.toHaveBeenCalled();
expect(onChangeText).not.toHaveBeenCalled();
expect(onSubmitEditing).not.toHaveBeenCalled();
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
});
test('should fire only non-touch-related events on non-editable TextInput with nested Text', () => {
const onFocus = jest.fn();
const onChangeText = jest.fn();
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
render(
<TextInput
editable={false}
testID="subject"
onFocus={onFocus}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
onLayout={onLayout}
>
<Text>Nested Text</Text>
</TextInput>,
);
const subject = screen.getByText('Nested Text');
fireEvent(subject, 'focus');
fireEvent(subject, 'onFocus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'onSubmitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'layout', layoutEvent);
fireEvent(subject, 'onLayout', layoutEvent);
expect(onFocus).not.toHaveBeenCalled();
expect(onChangeText).not.toHaveBeenCalled();
expect(onSubmitEditing).not.toHaveBeenCalled();
expect(onLayout).toHaveBeenCalledTimes(2);
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
});
/**
* Historically there were problems with custom TextInput wrappers, as they
* could creat a hierarchy of three or more composite text input views with
* very similar event props.
*
* Typical hierarchy would be:
* - User composite TextInput
* - UI library composite TextInput
* - RN composite TextInput
* - RN host TextInput
*
* Previous implementation of fireEvent only checked `editable` prop for
* RN TextInputs, both host & composite but did not check on the UI library or
* user composite TextInput level, hence invoking the event handlers that
* should be blocked by `editable={false}` prop.
*/
test('should fire only non-touch-related events on non-editable wrapped TextInput', () => {
const onFocus = jest.fn();
const onChangeText = jest.fn();
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
render(
<WrappedTextInput
editable={false}
testID="subject"
onFocus={onFocus}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
onLayout={onLayout}
/>,
);
const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'layout', layoutEvent);
expect(onFocus).not.toHaveBeenCalled();
expect(onChangeText).not.toHaveBeenCalled();
expect(onSubmitEditing).not.toHaveBeenCalled();
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
});
/**
* Ditto testing for even deeper hierarchy of TextInput wrappers.
*/
test('should fire only non-touch-related events on non-editable double wrapped TextInput', () => {
const onFocus = jest.fn();
const onChangeText = jest.fn();
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
render(
<DoubleWrappedTextInput
editable={false}
testID="subject"
onFocus={onFocus}
onChangeText={onChangeText}
onSubmitEditing={onSubmitEditing}
onLayout={onLayout}
/>,
);
const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
fireEvent(subject, 'layout', layoutEvent);
expect(onFocus).not.toHaveBeenCalled();
expect(onChangeText).not.toHaveBeenCalled();
expect(onSubmitEditing).not.toHaveBeenCalled();
expect(onLayout).toHaveBeenCalledWith(layoutEvent);
});