|
| 1 | +import React from 'react'; |
| 2 | +import { Text, TextInput, TextInputProps } from 'react-native'; |
| 3 | +import { render, fireEvent } from '..'; |
| 4 | + |
| 5 | +function WrappedTextInput(props: TextInputProps) { |
| 6 | + return <TextInput {...props} />; |
| 7 | +} |
| 8 | + |
| 9 | +function DoubleWrappedTextInput(props: TextInputProps) { |
| 10 | + return <WrappedTextInput {...props} />; |
| 11 | +} |
| 12 | + |
| 13 | +const layoutEvent = { nativeEvent: { layout: { width: 100, height: 100 } } }; |
| 14 | + |
| 15 | +test('should fire only non-touch-related events on non-editable TextInput', () => { |
| 16 | + const onFocus = jest.fn(); |
| 17 | + const onChangeText = jest.fn(); |
| 18 | + const onSubmitEditing = jest.fn(); |
| 19 | + const onLayout = jest.fn(); |
| 20 | + |
| 21 | + const view = render( |
| 22 | + <TextInput |
| 23 | + editable={false} |
| 24 | + testID="subject" |
| 25 | + onFocus={onFocus} |
| 26 | + onChangeText={onChangeText} |
| 27 | + onSubmitEditing={onSubmitEditing} |
| 28 | + onLayout={onLayout} |
| 29 | + /> |
| 30 | + ); |
| 31 | + |
| 32 | + const subject = view.getByTestId('subject'); |
| 33 | + fireEvent(subject, 'focus'); |
| 34 | + fireEvent.changeText(subject, 'Text'); |
| 35 | + fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } }); |
| 36 | + fireEvent(subject, 'layout', layoutEvent); |
| 37 | + |
| 38 | + expect(onFocus).not.toHaveBeenCalled(); |
| 39 | + expect(onChangeText).not.toHaveBeenCalled(); |
| 40 | + expect(onSubmitEditing).not.toHaveBeenCalled(); |
| 41 | + expect(onLayout).toHaveBeenCalledWith(layoutEvent); |
| 42 | +}); |
| 43 | + |
| 44 | +test('should fire only non-touch-related events on non-editable TextInput with nested Text', () => { |
| 45 | + const onFocus = jest.fn(); |
| 46 | + const onChangeText = jest.fn(); |
| 47 | + const onSubmitEditing = jest.fn(); |
| 48 | + const onLayout = jest.fn(); |
| 49 | + |
| 50 | + const view = render( |
| 51 | + <TextInput |
| 52 | + editable={false} |
| 53 | + testID="subject" |
| 54 | + onFocus={onFocus} |
| 55 | + onChangeText={onChangeText} |
| 56 | + onSubmitEditing={onSubmitEditing} |
| 57 | + onLayout={onLayout} |
| 58 | + > |
| 59 | + <Text>Nested Text</Text> |
| 60 | + </TextInput> |
| 61 | + ); |
| 62 | + |
| 63 | + const subject = view.getByText('Nested Text'); |
| 64 | + fireEvent(subject, 'focus'); |
| 65 | + fireEvent.changeText(subject, 'Text'); |
| 66 | + fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } }); |
| 67 | + fireEvent(subject, 'layout', layoutEvent); |
| 68 | + |
| 69 | + expect(onFocus).not.toHaveBeenCalled(); |
| 70 | + expect(onChangeText).not.toHaveBeenCalled(); |
| 71 | + expect(onSubmitEditing).not.toHaveBeenCalled(); |
| 72 | + expect(onLayout).toHaveBeenCalledWith(layoutEvent); |
| 73 | +}); |
| 74 | + |
| 75 | +test('should fire only non-touch-related events on non-editable wrapped TextInput', () => { |
| 76 | + const onFocus = jest.fn(); |
| 77 | + const onChangeText = jest.fn(); |
| 78 | + const onSubmitEditing = jest.fn(); |
| 79 | + const onLayout = jest.fn(); |
| 80 | + |
| 81 | + const view = render( |
| 82 | + <WrappedTextInput |
| 83 | + editable={false} |
| 84 | + testID="subject" |
| 85 | + onFocus={onFocus} |
| 86 | + onChangeText={onChangeText} |
| 87 | + onSubmitEditing={onSubmitEditing} |
| 88 | + onLayout={onLayout} |
| 89 | + /> |
| 90 | + ); |
| 91 | + |
| 92 | + const subject = view.getByTestId('subject'); |
| 93 | + fireEvent(subject, 'focus'); |
| 94 | + fireEvent.changeText(subject, 'Text'); |
| 95 | + fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } }); |
| 96 | + fireEvent(subject, 'layout', layoutEvent); |
| 97 | + |
| 98 | + expect(onFocus).not.toHaveBeenCalled(); |
| 99 | + expect(onChangeText).not.toHaveBeenCalled(); |
| 100 | + expect(onSubmitEditing).not.toHaveBeenCalled(); |
| 101 | + expect(onLayout).toHaveBeenCalledWith(layoutEvent); |
| 102 | +}); |
| 103 | + |
| 104 | +test('should fire only non-touch-related events on non-editable double wrapped TextInput', () => { |
| 105 | + const onFocus = jest.fn(); |
| 106 | + const onChangeText = jest.fn(); |
| 107 | + const onSubmitEditing = jest.fn(); |
| 108 | + const onLayout = jest.fn(); |
| 109 | + |
| 110 | + const view = render( |
| 111 | + <DoubleWrappedTextInput |
| 112 | + editable={false} |
| 113 | + testID="subject" |
| 114 | + onFocus={onFocus} |
| 115 | + onChangeText={onChangeText} |
| 116 | + onSubmitEditing={onSubmitEditing} |
| 117 | + onLayout={onLayout} |
| 118 | + /> |
| 119 | + ); |
| 120 | + |
| 121 | + const subject = view.getByTestId('subject'); |
| 122 | + fireEvent(subject, 'focus'); |
| 123 | + fireEvent.changeText(subject, 'Text'); |
| 124 | + fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } }); |
| 125 | + fireEvent(subject, 'layout', layoutEvent); |
| 126 | + |
| 127 | + expect(onFocus).not.toHaveBeenCalled(); |
| 128 | + expect(onChangeText).not.toHaveBeenCalled(); |
| 129 | + expect(onSubmitEditing).not.toHaveBeenCalled(); |
| 130 | + expect(onLayout).toHaveBeenCalledWith(layoutEvent); |
| 131 | +}); |
0 commit comments