Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {SearchAutocompleteListProps} from '@components/Search/SearchAutocom
import SearchAutocompleteList from '@components/Search/SearchAutocompleteList';

import useIsFocusedUntilTransitionEnd from '@hooks/useIsFocusedUntilTransitionEnd';
import useRunAfterTransitions from '@hooks/useRunAfterTransitions';

import {endSpan} from '@libs/telemetry/activeSpans';
import type {SkeletonSpanReasonAttributes} from '@libs/telemetry/useSkeletonSpan';
Expand Down Expand Up @@ -32,12 +31,13 @@ function DeferredAutocompleteList(props: SearchAutocompleteListProps) {
setHasLayout(true);
};

// Wait for the slide-in animation to finish before rendering the list. With startTransition, a competing update
// interrupted and discarded the (expensive) first mount render before it could commit, forcing React to redo
// that same render a second time. useRunAfterTransitions fires a plain, synchronous update instead, so the first render always completes in a single pass.
const shouldRender = useRunAfterTransitions(hasLayout);

if (!shouldRender || !isFocusedUntilTransitionEnd) {
// Mount the real list as soon as the skeleton has laid out, overlapping its render with the
// native-stack push animation instead of waiting for the transition to end. The push runs on the
// UI thread, so rendering the list on the JS thread during it does not stutter the slide, and the
// list's onLayout (which ends the ManualOpenSearchRouter span) now fires while the animation is
// still finishing rather than ~150ms after it. Affordable because the option-list build is cheap
// after the caching/deferral in #95378 and #95683.
if (!hasLayout || !isFocusedUntilTransitionEnd) {
return (
<OptionsListSkeletonView
fixedNumItems={4}
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useAutocompleteSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {Beta, CardFeeds, CardList, PersonalDetailsList, Policy} from '@src/types/onyx';
import type {VisibleReportActionsDerivedValue} from '@src/types/onyx/DerivedValues';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
import getEmptyArray from '@src/types/utils/getEmptyArray';

import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';

Expand Down Expand Up @@ -148,7 +149,8 @@ function useAutocompleteSuggestions({
}

if (!autocompleteKey) {
return [];
// Returns the same array reference on every render, so the consumer's `sections` memo stays valid and the list doesn't re-render.
return getEmptyArray<AutocompleteItemData>();
}

const alreadyAutocompletedKeys = new Set(
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/Search/DeferredSearchAutocompleteListTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {fireEvent, render, screen} from '@testing-library/react-native';

import DeferredSearchAutocompleteList from '@components/Search/DeferredSearchAutocompleteList';
import type {SearchAutocompleteListProps} from '@components/Search/SearchAutocompleteList';

import TransitionTracker from '@libs/Navigation/TransitionTracker';

import type {LayoutChangeEvent} from 'react-native';

import React from 'react';
import {View} from 'react-native';

// jest.mock factories can't reference imported bindings, but `mock`-prefixed locals are allowed.
const MockView = View;

const mockIsFocusedUntilTransitionEnd = jest.fn(() => true);

jest.mock('@hooks/useIsFocusedUntilTransitionEnd', () => ({
__esModule: true,
default: () => mockIsFocusedUntilTransitionEnd(),
}));

// Stand in for the two children so the test only exercises which one the wrapper picks.
jest.mock('@components/OptionsListSkeletonView', () => ({
__esModule: true,
default: ({onLayout}: {onLayout?: (event: LayoutChangeEvent) => void}) => (
<MockView
testID="options-list-skeleton"
onLayout={onLayout}
/>
),
}));

jest.mock('@components/Search/SearchAutocompleteList', () => ({
__esModule: true,
default: () => <MockView testID="search-autocomplete-list" />,
}));

jest.mock('@libs/Navigation/TransitionTracker', () => ({
runAfterTransitions: jest.fn(),
}));

jest.mock('@libs/telemetry/activeSpans', () => ({
endSpan: jest.fn(),
}));

const mockedRunAfterTransitions = jest.mocked(TransitionTracker.runAfterTransitions);

const listProps: SearchAutocompleteListProps = {
autocompleteQueryValue: '',
handleSearch: jest.fn(),
onListItemPress: jest.fn(),
};

function layOutSkeleton() {
fireEvent(screen.getByTestId('options-list-skeleton'), 'layout', {nativeEvent: {layout: {width: 300, height: 400, x: 0, y: 0}}});
}

beforeEach(() => {
jest.clearAllMocks();
mockIsFocusedUntilTransitionEnd.mockReturnValue(true);
// A transition that never completes: anything gating on it would stay on the skeleton forever.
mockedRunAfterTransitions.mockReturnValue({cancel: jest.fn()});
});

describe('DeferredSearchAutocompleteList', () => {
it('swaps the skeleton for the list on layout, without waiting for the navigation transition, and back on blur', () => {
const {rerender} = render(<DeferredSearchAutocompleteList {...listProps} />);

expect(screen.queryByTestId('search-autocomplete-list')).not.toBeOnTheScreen();

layOutSkeleton();

expect(screen.getByTestId('search-autocomplete-list')).toBeOnTheScreen();
expect(mockedRunAfterTransitions).not.toHaveBeenCalled();

mockIsFocusedUntilTransitionEnd.mockReturnValue(false);
rerender(<DeferredSearchAutocompleteList {...listProps} />);

expect(screen.getByTestId('options-list-skeleton')).toBeOnTheScreen();
});
});
14 changes: 14 additions & 0 deletions tests/unit/hooks/useAutocompleteSuggestions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ describe('useAutocompleteSuggestions', () => {
expect(result.current).toEqual([]);
});

it('keeps the same empty array reference across renders and hook instances', () => {
parseForAutocomplete.mockReturnValue({autocomplete: null, ranges: []});

// Each render builds a fresh params object, mirroring a parent re-render.
const {result: first, rerender} = renderHook(() => useAutocompleteSuggestions({...defaultParams, autocompleteQueryValue: ''}));
const firstResult = first.current;
rerender({});

const {result: second} = renderHook(() => useAutocompleteSuggestions({...defaultParams, autocompleteQueryValue: ''}));

expect(first.current).toBe(firstResult);
expect(second.current).toBe(firstResult);
});

it('returns tag suggestions when autocomplete key is tag', () => {
parseForAutocomplete.mockReturnValue({
autocomplete: {key: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG, value: 'eng'},
Expand Down
Loading