Skip to content

Commit

Permalink
(base/SearchBar): Remove deprecated Keyboard.removeListener (#3215)
Browse files Browse the repository at this point in the history
Co-authored-by: Gustav Haglund <gustav.haglund@nordnet.se>
  • Loading branch information
roffelund and Gustav Haglund committed Jan 14, 2022
1 parent 8c285ca commit 29224f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/base/src/SearchBar/SearchBar-android.tsx
Expand Up @@ -5,6 +5,7 @@ import {
ActivityIndicator,
Keyboard,
TextInput,
EmitterSubscription,
} from 'react-native';
import { defaultTheme, renderNode } from '../helpers';
import { Input, InputProps } from '../Input';
Expand Down Expand Up @@ -49,7 +50,7 @@ export class SearchBarAndroid extends Component<
SearchBarState
> {
input!: TextInput;
_keyboardDidHideListener;

static defaultProps = {
onClear: () => null,
onCancel: () => null,
Expand All @@ -58,6 +59,8 @@ export class SearchBarAndroid extends Component<
onChangeText: () => null,
};

keyboardListener: EmitterSubscription;

focus = () => {
this.input.focus();
};
Expand Down Expand Up @@ -102,7 +105,7 @@ export class SearchBarAndroid extends Component<
hasFocus: false,
isEmpty: value ? value === '' : true,
};
this._keyboardDidHideListener = Keyboard.addListener(
this.keyboardListener = Keyboard.addListener(
'keyboardDidHide',
this._keyboardDidHide
);
Expand All @@ -112,7 +115,9 @@ export class SearchBarAndroid extends Component<
};

componentWillUnmount() {
this._keyboardDidHideListener.remove();
if (this.keyboardListener) {
this.keyboardListener.remove();
}
}

render() {
Expand Down
30 changes: 30 additions & 0 deletions packages/base/src/SearchBar/__tests__/SearchBar.test.tsx
@@ -1,6 +1,7 @@
import React from 'react';
import SearchBar from '../index';
import { renderWithWrapper } from '../../../.ci/testHelper';
import { Keyboard } from 'react-native';

describe('SearchBar wrapper component', () => {
it('should match snapshot', () => {
Expand Down Expand Up @@ -37,4 +38,33 @@ describe('SearchBar wrapper component', () => {
);
expect(component.toJSON()).toMatchSnapshot();
});

describe('keyboard eventListener', () => {
const mockListener = {
remove: jest.fn(),
};
const originalAddListener = Keyboard.addListener;
const mockAddListener = jest.fn().mockReturnValue(mockListener);

beforeAll(() => {
Keyboard.addListener = mockAddListener;
});
beforeEach(() => {
mockAddListener.mockClear();
mockListener.remove.mockClear();
});
afterAll(() => {
Keyboard.addListener = originalAddListener;
});
it('should subscribe to KeyboardDidClose event', () => {
renderWithWrapper(<SearchBar platform="android" />);
expect(Keyboard.addListener).toHaveBeenCalled();
});

it('should call listener.remove on unmount', () => {
const component = renderWithWrapper(<SearchBar platform="android" />);
component.unmount();
expect(mockListener.remove).toHaveBeenCalled();
});
});
});

0 comments on commit 29224f8

Please sign in to comment.