diff --git a/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx b/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx index 41d2a01b6d3d..c8b3e33ce829 100644 --- a/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx +++ b/src/components/Search/DeferredSearchAutocompleteList/index.native.tsx @@ -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'; @@ -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 ( (); } const alreadyAutocompletedKeys = new Set( diff --git a/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx b/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx new file mode 100644 index 000000000000..bf159d55d5ec --- /dev/null +++ b/tests/unit/Search/DeferredSearchAutocompleteListTest.tsx @@ -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}) => ( + + ), +})); + +jest.mock('@components/Search/SearchAutocompleteList', () => ({ + __esModule: true, + default: () => , +})); + +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(); + + expect(screen.queryByTestId('search-autocomplete-list')).not.toBeOnTheScreen(); + + layOutSkeleton(); + + expect(screen.getByTestId('search-autocomplete-list')).toBeOnTheScreen(); + expect(mockedRunAfterTransitions).not.toHaveBeenCalled(); + + mockIsFocusedUntilTransitionEnd.mockReturnValue(false); + rerender(); + + expect(screen.getByTestId('options-list-skeleton')).toBeOnTheScreen(); + }); +}); diff --git a/tests/unit/hooks/useAutocompleteSuggestions.test.ts b/tests/unit/hooks/useAutocompleteSuggestions.test.ts index 61027e145191..b5b66b54b581 100644 --- a/tests/unit/hooks/useAutocompleteSuggestions.test.ts +++ b/tests/unit/hooks/useAutocompleteSuggestions.test.ts @@ -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'},