mattermost-mobile-upstream/app/hooks/throttled.test.ts
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

320 lines
10 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {renderHook} from '@testing-library/react-native';
import useThrottled from '@hooks/throttled';
import {advanceTimers, enableFakeTimers} from '@test/timer_helpers';
describe('useThrottled', () => {
beforeEach(() => {
enableFakeTimers();
jest.clearAllMocks();
jest.clearAllTimers();
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
it('should return a throttled function', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 1000));
expect(typeof result.current).toBe('function');
});
it('should call callback immediately on first invocation', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 1000));
result.current('arg1', 'arg2');
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith('arg1', 'arg2');
});
it('should throttle subsequent calls within the time window', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 1000));
// First call - should execute immediately
result.current('call1');
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith('call1');
// Second call - should be throttled
result.current('call2');
expect(callback).toHaveBeenCalledTimes(1);
// Third call - still throttled
result.current('call3');
expect(callback).toHaveBeenCalledTimes(1);
});
it('should execute throttled call after time window expires', async () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 1000));
// First call
result.current('call1');
expect(callback).toHaveBeenCalledTimes(1);
// Second call within window
result.current('call2');
expect(callback).toHaveBeenCalledTimes(1);
// Advance time past the throttle window
await advanceTimers(1000);
// Third call after window - should execute with most recent args
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenCalledWith('call2');
});
it('should handle multiple arguments', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 500));
result.current('arg1', 'arg2', 'arg3', {key: 'value'});
expect(callback).toHaveBeenCalledWith('arg1', 'arg2', 'arg3', {key: 'value'});
});
it('should handle no arguments', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 500));
result.current();
expect(callback).toHaveBeenCalledWith();
});
it('should use the latest callback reference', async () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
const {result, rerender} = renderHook(
({cb, time}) => useThrottled(cb, time),
{initialProps: {cb: callback1, time: 1000}},
);
// First call with callback1
result.current('test1');
expect(callback1).toHaveBeenCalledWith('test1');
expect(callback2).not.toHaveBeenCalled();
// Update to callback2
rerender({cb: callback2, time: 1000});
// Advance time to allow next call
await advanceTimers(1000);
// Call with updated callback
result.current('test2');
expect(callback2).toHaveBeenCalledWith('test2');
});
it('should return the same throttled function when time does not change', () => {
const callback1 = jest.fn();
const callback2 = jest.fn();
const {result, rerender} = renderHook(
({cb, time}) => useThrottled(cb, time),
{initialProps: {cb: callback1, time: 1000}},
);
const throttledFn1 = result.current;
// Change callback but keep time the same
rerender({cb: callback2, time: 1000});
const throttledFn2 = result.current;
// Should be the same function reference (memoized)
expect(throttledFn1).toBe(throttledFn2);
});
it('should return a new throttled function when time changes', () => {
const callback = jest.fn();
const {result, rerender} = renderHook(
({cb, time}) => useThrottled(cb, time),
{initialProps: {cb: callback, time: 1000}},
);
const throttledFn1 = result.current;
// Change the time value
rerender({cb: callback, time: 2000});
const throttledFn2 = result.current;
// Should be a different function reference
expect(throttledFn1).not.toBe(throttledFn2);
});
it('should respect the new time value after update', async () => {
const callback = jest.fn();
const {result, rerender} = renderHook(
({time}) => useThrottled(callback, time),
{initialProps: {time: 1000}},
);
// First call
result.current('call1');
expect(callback).toHaveBeenCalledTimes(1);
// Second call - should be throttled
result.current('call2');
expect(callback).toHaveBeenCalledTimes(1);
// Change throttle time to 2000ms - this creates a new throttled function
rerender({time: 2000});
// Advance by 1000ms to clear the old throttled function's timer
await advanceTimers(1000);
// The trailing call from the old throttled function executes
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenLastCalledWith('call2');
// Now use the new throttled function with 2000ms throttle
result.current('call3');
expect(callback).toHaveBeenCalledTimes(3);
// Call again - should be throttled with new 2000ms time
result.current('call4');
expect(callback).toHaveBeenCalledTimes(3);
// Advance by 2000ms
await advanceTimers(2000);
// Should execute the trailing call
expect(callback).toHaveBeenCalledTimes(4);
expect(callback).toHaveBeenLastCalledWith('call4');
});
it('should handle rapid successive calls', async () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 1000));
// Make 10 rapid calls
for (let i = 0; i < 10; i++) {
result.current(`call${i}`);
}
// Only the first call should execute
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith('call0');
// Advance time
await advanceTimers(1000);
// The last call should execute (trailing edge)
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenCalledWith('call9');
});
it('should handle zero throttle time', () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 0));
result.current('call1');
result.current('call2');
result.current('call3');
// With 0ms throttle, all calls should execute
expect(callback).toHaveBeenCalledTimes(3);
});
it('should handle very short throttle time', async () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 10));
result.current('call1');
expect(callback).toHaveBeenCalledTimes(1);
result.current('call2');
expect(callback).toHaveBeenCalledTimes(1);
await advanceTimers(10);
expect(callback).toHaveBeenCalledTimes(2);
});
it('should handle very long throttle time', async () => {
const callback = jest.fn();
const {result} = renderHook(() => useThrottled(callback, 60000)); // 1 minute
result.current('call1');
expect(callback).toHaveBeenCalledTimes(1);
result.current('call2');
expect(callback).toHaveBeenCalledTimes(1);
// Advance by 59 seconds - should still be throttled
await advanceTimers(59000);
expect(callback).toHaveBeenCalledTimes(1);
// Advance by 1 more second
await advanceTimers(1000);
expect(callback).toHaveBeenCalledTimes(2);
});
it('should handle callback that returns a value', () => {
const callback = jest.fn((x: number, y: number) => x + y);
const {result} = renderHook(() => useThrottled(callback, 1000));
const returnValue = result.current(5, 3);
expect(returnValue).toBe(8);
expect(callback).toHaveBeenCalledWith(5, 3);
});
it('should handle callback that throws an error', () => {
const error = new Error('Test error');
const callback = jest.fn(() => {
throw error;
});
const {result} = renderHook(() => useThrottled(callback, 1000));
expect(() => result.current()).toThrow(error);
});
it('should handle callback being called through the ref', async () => {
let callCount = 0;
const callback = jest.fn(() => {
callCount++;
return callCount;
});
const {result, rerender} = renderHook(
({cb}) => useThrottled(cb, 1000),
{initialProps: {cb: callback}},
);
// First call
const result1 = result.current();
expect(result1).toBe(1);
expect(callback).toHaveBeenCalledTimes(1);
// Update callback
const callback2 = jest.fn(() => {
callCount++;
return callCount;
});
rerender({cb: callback2});
// Advance time to allow next call
await advanceTimers(1000);
// Call with updated callback - should use callback2 through the ref
result.current();
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1); // Original callback not called again
});
});