mattermost-mobile/app/hooks/keyboardAnimation.test.ts
Rajat Dabade e39b27be7e
New keyboard library Setup (#9278)
* Initial setup for new keyboard

* fix the offset calculation onMove by adding isKeyboardFullyOpened

* Done with the keyboard handling implementation

* Handled keyboard focus and blured state using context

* Added default height for input container

* Android support

* Tablet state handling

* Fix for refreshing offset in list

* Created a default context for mention post list

* Fix linter errors

* Fix tests

* Minor

* Fix the height issue for tablet view

* Review comments

* Dependency fix

* Reveiw comment

* keyboard animation only enabled with screen on top navigation

* added tests

* Added extra keyboard component for emoji picker (#9328)

* Added extra keyboard component

* handled swipe geature for extra keyboard

* scroll to bottom visible on emoji picker

* Check for stale event for keyboard geature area

* fix keyboard stale event for mid-gesture change swipe direction

* Closing emoji picker when message priority is opened

* changing to thread view closes emoji picker

* Close emoij picker and keyboard when attachment icons are clicked

* handle android emoji picker behaviour

* Remove emoji picker code

* fix tests

* Address reviev comments

* Test fixes

* usePreventDoubleTab for race condition and corrected comment on isInputAccessoryViewMode flag

* File attachments option in bottom sheet quick action (#9331)

* Added extra keyboard component

* handled swipe geature for extra keyboard

* File attachments option in bottom sheet quick action

* Updated tests

* i18n

* fix test

* Added tests

* Review comment

* fix tests

* Integrated Emoji picker to Extra keyboard component.  (#9339)

* Added extra keyboard component

* handled swipe geature for extra keyboard

* scroll to bottom visible on emoji picker

* Fix the post input container height change issue

* Added emoji picker with search functionality

* keyboard height not recorded fallback to default height, set search to false closing emoji picker

* fix search funcitonality in android

* scroll to end bottom dismissed with pressed

* Fixed the scroll issue for android

* fix the flickering post input issue

* Wired up the emoji picker

* Added keyboard and spaceback in emoji picker

* intl extract

* initial cursor position and simple calculation review comment

* separated grapheme to utils file

* Review comments

* nitpick

* Fix the bottom safe area and navigation stack restore fix

* Fix test

* disabled emoji picker in edit post screen

* change the name of the variable to name it clarier

* fix the tab gap issue in andriod

* disabled the emoji skin tone change on andriod

* fix the input container jump issue

* fix the failing test

* UX review

* added white space after the attachment icon

* When @ or / is press open keyboard on andriod also fix the scroll issue when keyboard is open

* back bottom closes emoji picker and opening keyboard jump fix

* reverted code

* fixed the flickering issue of input and scroll position fix

* Fix the gap issue in thread

* fix the warning reaminated issue when opening a channel

* Fix the extra space issue between input and emoij picker

* Minor fix for extra space between input and emoji picker

* Fix thread view bottom safe area and gap between keyboard and searched emojis

* Fix the keyboard issue in tablet

* Fix the warning issue react-native-keyboard-controller

* Fix tests

* Fix the tablet search hight issue

* Replaced the ExtraKeyboardProvider with KeyboardProvider from rnkc for permalink

* Bottom sheet jump issue resolved

* Search do not close after selecting emoji in search list

* Added the bottom inset height in search emoji for android

* fix buid issue

* Fix the cancel state to emoji picker

* use portals for autocomplete

* fix permalink extra space in post list

* Portal only for android

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2026-01-13 22:38:35 +05:30

490 lines
17 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {renderHook, act} from '@testing-library/react-hooks';
import {useKeyboardHandler} from 'react-native-keyboard-controller';
import {BOTTOM_TAB_HEIGHT} from '@constants/view';
import {useKeyboardAnimation} from './keyboardAnimation';
jest.mock('react-native-keyboard-controller', () => ({
useKeyboardHandler: jest.fn(),
}));
jest.mock('react-native-reanimated', () => {
const sharedValues = new Map();
return {
...jest.requireActual('react-native-reanimated/mock'),
useSharedValue: jest.fn((initial) => {
const key = Math.random();
const sv = {
value: initial,
_key: key,
};
sharedValues.set(key, sv);
return sv;
}),
useDerivedValue: jest.fn((fn) => ({
value: fn(),
})),
useAnimatedScrollHandler: jest.fn((handlers) => (event: {contentOffset: {y: number}}) => {
if (handlers.onScroll) {
handlers.onScroll(event);
}
}),
};
});
describe('useKeyboardAnimation', () => {
const mockUseKeyboardHandler = useKeyboardHandler as jest.Mock;
let keyboardHandlerCallbacks: {
onStart?: (e: {height: number; progress: number}) => void;
onInteractive?: (e: {height: number; progress: number}) => void;
onMove?: (e: {height: number; progress: number}) => void;
onEnd?: (e: {height: number; progress: number}) => void;
} = {};
beforeEach(() => {
jest.clearAllMocks();
keyboardHandlerCallbacks = {};
mockUseKeyboardHandler.mockImplementation((callbacks) => {
keyboardHandlerCallbacks = callbacks;
});
});
describe('initialization', () => {
it('should initialize with default values', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
expect(result.current.keyboardTranslateY.value).toBe(0);
expect(result.current.bottomInset.value).toBe(0);
expect(result.current.scrollOffset.value).toBe(0);
expect(result.current.scrollPosition.value).toBe(0);
expect(result.current.keyboardHeight.value).toBe(0);
expect(result.current.isKeyboardFullyOpen.value).toBe(false);
expect(result.current.isKeyboardFullyClosed.value).toBe(true);
expect(result.current.isKeyboardInTransition.value).toBe(false);
expect(result.current.onScroll).toBeDefined();
});
it('should call useKeyboardHandler with callbacks', () => {
renderHook(() => useKeyboardAnimation(100, true, false, 0, false, true));
expect(mockUseKeyboardHandler).toHaveBeenCalled();
expect(keyboardHandlerCallbacks.onStart).toBeDefined();
expect(keyboardHandlerCallbacks.onInteractive).toBeDefined();
expect(keyboardHandlerCallbacks.onMove).toBeDefined();
expect(keyboardHandlerCallbacks.onEnd).toBeDefined();
});
});
describe('enabled prop', () => {
it('should skip processing when enabled is false', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, false),
);
const initialHeight = result.current.keyboardTranslateY.value;
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(initialHeight);
});
it('should process events when enabled is true', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(300);
});
});
describe('enableAnimation prop', () => {
it('should skip processing when enableAnimation is false', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, false, false, 0, false, true),
);
const initialHeight = result.current.keyboardTranslateY.value;
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(initialHeight);
});
});
describe('onStart callback', () => {
it('should update keyboard height and state on start', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardHeight.value).toBe(300);
expect(result.current.keyboardTranslateY.value).toBe(300);
expect(result.current.bottomInset.value).toBe(300);
// isKeyboardFullyOpen is set to false in onStart to prevent jerky behavior
// It will be set to true in onEnd when animation completes
expect(result.current.isKeyboardFullyOpen.value).toBe(false);
expect(result.current.isKeyboardFullyClosed.value).toBe(false);
expect(result.current.isKeyboardInTransition.value).toBe(false);
// Call onEnd to finalize the state
act(() => {
keyboardHandlerCallbacks.onEnd?.({
height: 300,
progress: 1,
});
});
// After onEnd, keyboard should be marked as fully open
expect(result.current.isKeyboardFullyOpen.value).toBe(true);
expect(result.current.isKeyboardFullyClosed.value).toBe(false);
expect(result.current.isKeyboardInTransition.value).toBe(false);
});
it('should handle partial progress on start', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 150,
progress: 0.5,
});
});
expect(result.current.keyboardTranslateY.value).toBe(150);
expect(result.current.isKeyboardFullyOpen.value).toBe(false);
expect(result.current.isKeyboardInTransition.value).toBe(true);
});
it('should ignore adjustment events from KeyboardGestureArea', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
// Set initial keyboard height
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
const initialHeight = result.current.keyboardTranslateY.value;
// Try to trigger adjustment event (height = keyboardHeight - postInputContainerHeight)
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 200, // 300 - 100
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(initialHeight);
});
});
describe('onInteractive callback', () => {
it('should update values during interactive drag', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
// First call onStart to set initial height (required for onInteractive to work)
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
// Then call onInteractive to simulate interactive drag
act(() => {
keyboardHandlerCallbacks.onInteractive?.({
height: 250,
progress: 0.8,
});
});
expect(result.current.keyboardTranslateY.value).toBe(250);
expect(result.current.scrollOffset.value).toBe(250);
expect(result.current.bottomInset.value).toBe(250);
});
it('should track keyboard closing state', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
// Set initial height
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
// Simulate closing (height decreasing)
act(() => {
keyboardHandlerCallbacks.onInteractive?.({
height: 200,
progress: 0.7,
});
});
// isKeyboardClosing is internal state, not exposed in return value
// We verify closing behavior by checking that height decreases
expect(result.current.keyboardTranslateY.value).toBeLessThan(300);
});
it('should ignore events matching postInputContainerHeight', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
const initialHeight = result.current.keyboardTranslateY.value;
act(() => {
keyboardHandlerCallbacks.onInteractive?.({
height: 100, // matches postInputContainerHeight
progress: 0.5,
});
});
expect(result.current.keyboardTranslateY.value).toBe(initialHeight);
});
});
describe('onMove callback', () => {
it('should update values during keyboard movement', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 250,
progress: 0.7,
});
});
act(() => {
keyboardHandlerCallbacks.onMove?.({
height: 280,
progress: 0.9,
});
});
expect(result.current.keyboardTranslateY.value).toBe(280);
expect(result.current.scrollOffset.value).toBe(280);
expect(result.current.bottomInset.value).toBe(280);
});
it('should ignore onMove events when keyboard is closing', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
act(() => {
keyboardHandlerCallbacks.onInteractive?.({
height: 200,
progress: 0.7,
});
});
act(() => {
keyboardHandlerCallbacks.onMove?.({
height: 150,
progress: 0.5,
});
});
expect(result.current.keyboardTranslateY.value).toBe(200);
expect(result.current.scrollOffset.value).toBe(200);
expect(result.current.bottomInset.value).toBe(200);
});
it('should handle negative heights from programmatic dismiss', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onMove?.({
height: -50, // negative height from KeyboardController.dismiss()
progress: 0,
});
});
expect(result.current.keyboardTranslateY.value).toBe(50); // Math.abs applied
});
});
describe('onEnd callback', () => {
it('should finalize state when keyboard fully opens', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
act(() => {
keyboardHandlerCallbacks.onEnd?.({
height: 300,
progress: 1,
});
});
expect(result.current.isKeyboardFullyOpen.value).toBe(true);
expect(result.current.isKeyboardFullyClosed.value).toBe(false);
expect(result.current.isKeyboardInTransition.value).toBe(false);
});
it('should finalize state when keyboard fully closes', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 0,
progress: 0,
});
});
act(() => {
keyboardHandlerCallbacks.onEnd?.({
height: 0,
progress: 0,
});
});
expect(result.current.isKeyboardFullyOpen.value).toBe(false);
expect(result.current.isKeyboardFullyClosed.value).toBe(true);
expect(result.current.isKeyboardInTransition.value).toBe(false);
expect(result.current.keyboardTranslateY.value).toBe(0);
});
});
describe('tablet and tab bar adjustment', () => {
it('should apply tab bar adjustment for tablets in non-thread view', () => {
const safeAreaBottom = 20;
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, true, safeAreaBottom, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
const expectedAdjustment = BOTTOM_TAB_HEIGHT + safeAreaBottom;
expect(result.current.keyboardTranslateY.value).toBe(300 - expectedAdjustment);
});
it('should apply safeAreaBottom adjustment for thread view', () => {
const safeAreaBottom = 20;
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, true, safeAreaBottom, true, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(300);
});
it('should apply safeAreaBottom adjustment for mobile', () => {
const safeAreaBottom = 20;
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, safeAreaBottom, false, true),
);
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 300,
progress: 1,
});
});
expect(result.current.keyboardTranslateY.value).toBe(300 - safeAreaBottom);
});
});
describe('scroll handler', () => {
it('should track scroll position with bottomInset', () => {
const {result} = renderHook(() =>
useKeyboardAnimation(100, true, false, 0, false, true),
);
// Set bottomInset first
act(() => {
keyboardHandlerCallbacks.onStart?.({
height: 200,
progress: 1,
});
});
const mockScrollEvent = {
contentOffset: {y: 100},
} as unknown as Parameters<typeof result.current.onScroll>[0];
act(() => {
result.current.onScroll(mockScrollEvent);
});
expect(result.current.scrollPosition.value).toBe(100 + result.current.bottomInset.value);
});
});
});