Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3213 Remove deprecated Keyboard.removeListener #3215

Merged
merged 2 commits into from Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});
});