Skip to content

Commit 82ec9e4

Browse files
committed
fix lint and add tests
1 parent 501d434 commit 82ec9e4

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { Dimensions } from 'react-native';
1010
// This height was tested thoroughly on several iPhone Models (from iPhone 8 to 14 Pro)
1111
const IOS_MODAL_HEIGHT = 262;
1212

13-
const preserveSpaces = (label) => {
13+
const preserveSpaces = (label) => {
1414
return label.replace(/ /g, '\u00a0');
15-
}
15+
};
1616

1717
export default class RNPickerSelect extends PureComponent {
1818
static propTypes = {
@@ -282,7 +282,7 @@ export default class RNPickerSelect extends PureComponent {
282282
}
283283
}
284284
);
285-
}
285+
};
286286

287287
togglePicker(animate = false, postToggleCallback) {
288288
const { disabled } = this.props;
@@ -295,8 +295,8 @@ export default class RNPickerSelect extends PureComponent {
295295

296296
if (Keyboard.isVisible()) {
297297
const keyboardListener = Keyboard.addListener('keyboardDidHide', () => {
298-
this.updatePickerState(animate, postToggleCallback);
299-
keyboardListener.remove();
298+
this.updatePickerState(animate, postToggleCallback);
299+
keyboardListener.remove();
300300
});
301301
Keyboard.dismiss();
302302
} else {

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ describe('RNPickerSelect', () => {
4242
beforeEach(() => {
4343
jest.resetAllMocks();
4444
jest.spyOn(Keyboard, 'dismiss');
45+
jest.spyOn(Keyboard, 'addListener');
46+
Keyboard.isVisible = jest.fn().mockReturnValue(false);
4547
});
4648

4749
describe('when provided an itemKey prop', () => {
@@ -144,6 +146,25 @@ describe('RNPickerSelect', () => {
144146
expect(wrapper.state().showPicker).toEqual(true);
145147
});
146148

149+
it('should call Keyboard.addListener when keyboard is visible', () => {
150+
Keyboard.isVisible.mockReturnValue(true);
151+
const wrapper = shallow(<RNPickerSelect items={selectItems} onValueChange={noop} />);
152+
153+
const touchable = wrapper.find('TouchableOpacity').at(1);
154+
touchable.simulate('press');
155+
expect(Keyboard.addListener).toHaveBeenCalledTimes(1);
156+
expect(Keyboard.addListener).toHaveBeenCalledWith('keyboardDidHide', expect.any(Function));
157+
});
158+
159+
it('should not call Keyboard.addListener when keyboard is not visible', () => {
160+
Keyboard.isVisible.mockReturnValue(false);
161+
const wrapper = shallow(<RNPickerSelect items={selectItems} onValueChange={noop} />);
162+
163+
const touchable = wrapper.find('TouchableOpacity').at(1);
164+
touchable.simulate('press');
165+
expect(Keyboard.addListener).not.toHaveBeenCalled();
166+
});
167+
147168
it('should not show the picker when pressed if disabled', () => {
148169
const wrapper = shallow(
149170
<RNPickerSelect
@@ -245,6 +266,7 @@ describe('RNPickerSelect', () => {
245266
});
246267

247268
it('should call Keyboard.dismiss when opened', () => {
269+
Keyboard.isVisible.mockReturnValue(true);
248270
const wrapper = shallow(<RNPickerSelect items={selectItems} onValueChange={noop} />);
249271

250272
const touchable = wrapper.find('[testID="ios_touchable_wrapper"]');

0 commit comments

Comments
 (0)