File tree 2 files changed +38
-3
lines changed
packages/base/src/SearchBar
2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 5
5
ActivityIndicator ,
6
6
Keyboard ,
7
7
TextInput ,
8
+ EmitterSubscription ,
8
9
} from 'react-native' ;
9
10
import { defaultTheme , renderNode } from '../helpers' ;
10
11
import { Input , InputProps } from '../Input' ;
@@ -49,7 +50,7 @@ export class SearchBarAndroid extends Component<
49
50
SearchBarState
50
51
> {
51
52
input ! : TextInput ;
52
- _keyboardDidHideListener ;
53
+
53
54
static defaultProps = {
54
55
onClear : ( ) => null ,
55
56
onCancel : ( ) => null ,
@@ -58,6 +59,8 @@ export class SearchBarAndroid extends Component<
58
59
onChangeText : ( ) => null ,
59
60
} ;
60
61
62
+ keyboardListener : EmitterSubscription ;
63
+
61
64
focus = ( ) => {
62
65
this . input . focus ( ) ;
63
66
} ;
@@ -102,7 +105,7 @@ export class SearchBarAndroid extends Component<
102
105
hasFocus : false ,
103
106
isEmpty : value ? value === '' : true ,
104
107
} ;
105
- this . _keyboardDidHideListener = Keyboard . addListener (
108
+ this . keyboardListener = Keyboard . addListener (
106
109
'keyboardDidHide' ,
107
110
this . _keyboardDidHide
108
111
) ;
@@ -112,7 +115,9 @@ export class SearchBarAndroid extends Component<
112
115
} ;
113
116
114
117
componentWillUnmount ( ) {
115
- this . _keyboardDidHideListener . remove ( ) ;
118
+ if ( this . keyboardListener ) {
119
+ this . keyboardListener . remove ( ) ;
120
+ }
116
121
}
117
122
118
123
render ( ) {
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import SearchBar from '../index' ;
3
3
import { renderWithWrapper } from '../../../.ci/testHelper' ;
4
+ import { Keyboard } from 'react-native' ;
4
5
5
6
describe ( 'SearchBar wrapper component' , ( ) => {
6
7
it ( 'should match snapshot' , ( ) => {
@@ -37,4 +38,33 @@ describe('SearchBar wrapper component', () => {
37
38
) ;
38
39
expect ( component . toJSON ( ) ) . toMatchSnapshot ( ) ;
39
40
} ) ;
41
+
42
+ describe ( 'keyboard eventListener' , ( ) => {
43
+ const mockListener = {
44
+ remove : jest . fn ( ) ,
45
+ } ;
46
+ const originalAddListener = Keyboard . addListener ;
47
+ const mockAddListener = jest . fn ( ) . mockReturnValue ( mockListener ) ;
48
+
49
+ beforeAll ( ( ) => {
50
+ Keyboard . addListener = mockAddListener ;
51
+ } ) ;
52
+ beforeEach ( ( ) => {
53
+ mockAddListener . mockClear ( ) ;
54
+ mockListener . remove . mockClear ( ) ;
55
+ } ) ;
56
+ afterAll ( ( ) => {
57
+ Keyboard . addListener = originalAddListener ;
58
+ } ) ;
59
+ it ( 'should subscribe to KeyboardDidClose event' , ( ) => {
60
+ renderWithWrapper ( < SearchBar platform = "android" /> ) ;
61
+ expect ( Keyboard . addListener ) . toHaveBeenCalled ( ) ;
62
+ } ) ;
63
+
64
+ it ( 'should call listener.remove on unmount' , ( ) => {
65
+ const component = renderWithWrapper ( < SearchBar platform = "android" /> ) ;
66
+ component . unmount ( ) ;
67
+ expect ( mockListener . remove ) . toHaveBeenCalled ( ) ;
68
+ } ) ;
69
+ } ) ;
40
70
} ) ;
You can’t perform that action at this time.
0 commit comments