mattermost-mobile/app/hooks/android_adjust_soft_keyboard/index.test.ts
Mattermost Build 2499e487f9
Fix android keyboard area when switching apps (#8554) (#8559)
(cherry picked from commit 762bbabfa0)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2025-02-05 08:40:05 +02:00

98 lines
3.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import RNUtils from '@mattermost/rnutils';
import {renderHook} from '@testing-library/react-hooks';
import {Keyboard} from 'react-native';
import {Navigation} from 'react-native-navigation';
import {useAndroidAdjustSoftKeyboard} from '.'; // Update with the correct path
jest.mock('react-native', () => ({
Keyboard: {
isVisible: jest.fn(),
dismiss: jest.fn(),
},
Platform: {
OS: 'android',
select: jest.fn((options) => options.android ?? options.default),
},
}));
jest.mock('react-native-navigation', () => {
const registerComponentListenerMock = jest.fn();
return {
Navigation: {
events: jest.fn(() => ({
registerComponentListener: registerComponentListenerMock,
})),
},
};
});
jest.mock('@mattermost/rnutils', () => ({
setSoftKeyboardToAdjustNothing: jest.fn(),
setSoftKeyboardToAdjustResize: jest.fn(),
}));
describe('useAndroidAdjustSoftKeyboard', () => {
let registerComponentListenerMock: jest.SpyInstance;
const unsubscribeMock = {remove: jest.fn()};
beforeEach(() => {
jest.useFakeTimers();
const events = Navigation.events();
registerComponentListenerMock = jest.spyOn(events, 'registerComponentListener');
registerComponentListenerMock.mockReturnValue(unsubscribeMock);
});
afterEach(() => {
jest.clearAllMocks();
jest.useRealTimers();
});
it('should register component listener and handle lifecycle events', () => {
renderHook(() => useAndroidAdjustSoftKeyboard('Channel'));
expect(registerComponentListenerMock).toHaveBeenCalledWith(
expect.objectContaining({
componentDidAppear: expect.any(Function),
componentDidDisappear: expect.any(Function),
}),
'Channel',
);
const listener = registerComponentListenerMock.mock.calls[0][0];
jest.mocked(Keyboard.isVisible).mockReturnValue(true);
listener.componentDidAppear();
expect(Keyboard.dismiss).toHaveBeenCalled();
jest.runAllTimers();
expect(RNUtils.setSoftKeyboardToAdjustNothing).toHaveBeenCalled();
listener.componentDidDisappear();
expect(RNUtils.setSoftKeyboardToAdjustResize).toHaveBeenCalled();
});
it('should register component listener and handle lifecycle events when the screen is undefined', () => {
renderHook(() => useAndroidAdjustSoftKeyboard(undefined));
expect(registerComponentListenerMock).not.toHaveBeenCalledWith(
expect.objectContaining({
componentDidAppear: expect.any(Function),
componentDidDisappear: expect.any(Function),
}),
undefined,
);
});
it('should clean up on unmount', () => {
const {unmount} = renderHook(() => useAndroidAdjustSoftKeyboard('Channel'));
unmount();
expect(unsubscribeMock.remove).toHaveBeenCalled();
});
});