diff --git a/src/pages/inbox/LinkedActionNotFoundGuard.tsx b/src/pages/inbox/LinkedActionNotFoundGuard.tsx index d77c311a4068..f2445dc94b4c 100644 --- a/src/pages/inbox/LinkedActionNotFoundGuard.tsx +++ b/src/pages/inbox/LinkedActionNotFoundGuard.tsx @@ -30,26 +30,26 @@ function LinkedActionNotFoundGuard({children}: LinkedActionNotFoundGuardProps) { const routeParams = route.params as {reportActionID?: string} | undefined; const reportActionIDFromRoute = routeParams?.reportActionID; - if (!reportActionIDFromRoute) { - return children; - } - + // Render the gate next to children, not around them, so changing the linked action doesn't remount the report + // and lose its state (e.g. composer text). It comes after children so its effects run after theirs. return ( - + <> {children} - + {!!reportActionIDFromRoute && ( + + )} + ); } type LinkedActionNotFoundGateProps = { reportActionIDFromRoute: string; - children: ReactNode; }; -function LinkedActionNotFoundGate({reportActionIDFromRoute, children}: LinkedActionNotFoundGateProps) { +function LinkedActionNotFoundGate({reportActionIDFromRoute}: LinkedActionNotFoundGateProps) { const route = useRoute(); const navigation = useNavigation(); const navigatorKey = navigation.getState()?.key; @@ -147,7 +147,7 @@ function LinkedActionNotFoundGate({reportActionIDFromRoute, children}: LinkedAct useAutoNavigateForDeletedLinkedAction(isLinkedActionUnavailable, navigateToEndOfReport); - return children; + return null; } LinkedActionNotFoundGuard.displayName = 'LinkedActionNotFoundGuard'; diff --git a/tests/navigation/LinkedActionNotFoundGuardTest.tsx b/tests/navigation/LinkedActionNotFoundGuardTest.tsx index b464add1f92b..653773865d14 100644 --- a/tests/navigation/LinkedActionNotFoundGuardTest.tsx +++ b/tests/navigation/LinkedActionNotFoundGuardTest.tsx @@ -5,7 +5,7 @@ import CONST from '@src/CONST'; import LinkedActionNotFoundGuard from '@src/pages/inbox/LinkedActionNotFoundGuard'; import type {ReportAction} from '@src/types/onyx'; -import React from 'react'; +import React, {useEffect, useLayoutEffect} from 'react'; import {View} from 'react-native'; const REPORT_ID = '12345'; @@ -68,6 +68,23 @@ jest.mock('@hooks/useResponsiveLayout', () => ({ let mockIsReportActionVisible = true; +// The gate only re-renders when its Onyx subscriptions change, so the useOnyx mock notifies subscribers like Onyx does. +// Each update also replaces DERIVED.VISIBLE_REPORT_ACTIONS, which visibility is derived from. +const mockOnyxListeners = new Set<() => void>(); +let mockOnyxVersion = 0; +let mockVisibleReportActions: Record = {}; + +function updateOnyx(update: () => void) { + act(() => { + update(); + mockOnyxVersion++; + mockVisibleReportActions = {}; + for (const listener of mockOnyxListeners) { + listener(); + } + }); +} + jest.mock('@libs/ReportActionsUtils', () => ({ __esModule: true, isReportActionVisible: () => mockIsReportActionVisible, @@ -84,9 +101,18 @@ type UseOnyxReturn = [unknown, {status: string}]; let mockLinkedAction: ReportAction | null | undefined; let mockIsLoadingInitialReportActions: boolean; -jest.mock('@hooks/useOnyx', () => ({ - __esModule: true, - default: (key: string): UseOnyxReturn => { +jest.mock('@hooks/useOnyx', () => { + const {useSyncExternalStore} = jest.requireActual<{useSyncExternalStore: (subscribe: (listener: () => void) => () => void, getSnapshot: () => number) => number}>('react'); + const subscribe = (listener: () => void) => { + mockOnyxListeners.add(listener); + return () => { + mockOnyxListeners.delete(listener); + }; + }; + const getVersion = () => mockOnyxVersion; + + function useOnyxMock(key: string): UseOnyxReturn { + useSyncExternalStore(subscribe, getVersion); if (key.startsWith('reportActions_')) { return [mockLinkedAction, {status: 'loaded'}]; } @@ -97,9 +123,14 @@ jest.mock('@hooks/useOnyx', () => ({ return [{reportID: '12345', type: 'chat'}, {status: 'loaded'}]; } // DERIVED.VISIBLE_REPORT_ACTIONS - return [undefined, {status: 'loaded'}]; - }, -})); + return [mockVisibleReportActions, {status: 'loaded'}]; + } + + return { + __esModule: true, + default: useOnyxMock, + }; +}); function createReportAction(overrides: Partial = {}): ReportAction { return { @@ -117,9 +148,26 @@ function TestChildren() { return ; } +const mockChildMount = jest.fn(); +const mockChildUnmount = jest.fn(); +const effectLog: string[] = []; + +function MountTrackingChildren() { + useLayoutEffect(() => { + effectLog.push('child-layout-effect'); + }, []); + + useEffect(() => { + mockChildMount(); + return () => mockChildUnmount(); + }, []); + + return ; +} + describe('LinkedActionNotFoundGuard', () => { beforeEach(() => { - mockSetParams.mockClear(); + mockSetParams.mockReset(); mockCleanStaleBackToParam.mockClear(); mockIsNavigationReady.mockClear(); mockRouteParams.reportID = REPORT_ID; @@ -155,7 +203,7 @@ describe('LinkedActionNotFoundGuard', () => { }); it('clears reportActionID when the linked action is deleted while being viewed', () => { - const {rerender} = render( + render( , @@ -163,16 +211,10 @@ describe('LinkedActionNotFoundGuard', () => { expect(mockSetParams).not.toHaveBeenCalled(); - act(() => { + updateOnyx(() => { mockIsReportActionVisible = false; }); - rerender( - - - , - ); - expect(screen.getByTestId('test-children')).toBeTruthy(); expect(mockSetParams).toHaveBeenCalledWith({reportActionID: undefined}, ROUTE_KEY, NAVIGATOR_KEY); expect(mockCleanStaleBackToParam).toHaveBeenCalledWith(REPORT_ID, REPORT_ACTION_ID); @@ -180,7 +222,7 @@ describe('LinkedActionNotFoundGuard', () => { it('clears reportActionID when linked action is completely removed from Onyx', () => { // Start with action present - const {rerender} = render( + render( , @@ -189,16 +231,10 @@ describe('LinkedActionNotFoundGuard', () => { expect(mockSetParams).not.toHaveBeenCalled(); // Now remove the action (simulating REPORT_PREVIEW being nulled when moving IOU to workspace) - act(() => { + updateOnyx(() => { mockLinkedAction = null; }); - rerender( - - - , - ); - // The cleanup effect should clear reportActionID with the route key expect(mockSetParams).toHaveBeenCalledWith({reportActionID: undefined}, ROUTE_KEY, NAVIGATOR_KEY); expect(mockCleanStaleBackToParam).toHaveBeenCalledWith(REPORT_ID, REPORT_ACTION_ID); @@ -220,23 +256,17 @@ describe('LinkedActionNotFoundGuard', () => { }); it('passes route.key to setParams for proper targeting inside split navigators', () => { - const {rerender} = render( + render( , ); // Remove the action - act(() => { + updateOnyx(() => { mockLinkedAction = null; }); - rerender( - - - , - ); - expect(mockSetParams).toHaveBeenCalledTimes(1); // Verify route.key is the second argument (needed for split navigator targeting) expect(mockSetParams).toHaveBeenCalledWith({reportActionID: undefined}, ROUTE_KEY, NAVIGATOR_KEY); @@ -256,25 +286,116 @@ describe('LinkedActionNotFoundGuard', () => { it('does not clear params while still loading report actions', () => { // Action is present initially - const {rerender} = render( + render( , ); // Action disappears but loading is still in progress - act(() => { + updateOnyx(() => { mockLinkedAction = null; mockIsLoadingInitialReportActions = true; }); - rerender( - - - , - ); - // Should not navigate away while loading — the action might come back expect(mockSetParams).not.toHaveBeenCalled(); }); + + describe('keeps the report subtree mounted', () => { + beforeEach(() => { + mockChildMount.mockClear(); + mockChildUnmount.mockClear(); + effectLog.length = 0; + }); + + it('does not remount children when the linked action param is cleared', () => { + // Given a report opened at a linked action, e.g. the unapproved expense the inbox scrolls to + const {rerender} = render( + + + , + ); + expect(mockChildMount).toHaveBeenCalledTimes(1); + + // When the live-tail jump after sending a message clears the param to an empty string + mockRouteParams.reportActionID = ''; + rerender( + + + , + ); + + // Then the header, list and composer under the guard keep their state, so the composer is not reseeded with a stale draft + expect(mockChildUnmount).not.toHaveBeenCalled(); + expect(mockChildMount).toHaveBeenCalledTimes(1); + }); + + it('does not remount children when a linked action param is added', () => { + // Given a report opened without a linked action + mockRouteParams.reportActionID = undefined; + const {rerender} = render( + + + , + ); + + // When the user follows a link to an action in the same report + mockRouteParams.reportActionID = REPORT_ACTION_ID; + rerender( + + + , + ); + + // Then the existing subtree stays mounted + expect(mockChildUnmount).not.toHaveBeenCalled(); + expect(mockChildMount).toHaveBeenCalledTimes(1); + }); + + it('resets the gate but not the children when the linked action changes', () => { + // Given a report showing a linked action that was visible, so the gate has marked it as seen + const {rerender} = render( + + + , + ); + expect(mockSetParams).not.toHaveBeenCalled(); + + // When the route moves to a different linked action that is already deleted + mockRouteParams.reportActionID = 'another-action-id'; + mockIsReportActionVisible = false; + rerender( + + + , + ); + + // Then the gate starts fresh for the new action and takes the never-visible recovery path (which skips the backTo + // cleanup), while children stay mounted + expect(mockSetParams).toHaveBeenCalledTimes(1); + expect(mockSetParams).toHaveBeenCalledWith({reportActionID: undefined}, ROUTE_KEY, NAVIGATOR_KEY); + expect(mockCleanStaleBackToParam).not.toHaveBeenCalled(); + expect(mockChildUnmount).not.toHaveBeenCalled(); + expect(mockChildMount).toHaveBeenCalledTimes(1); + }); + + it('runs the recovery for a deleted linked action after the children layout effects', () => { + // Given the linked action is already deleted when the report mounts + mockIsReportActionVisible = false; + mockSetParams.mockImplementation(() => { + effectLog.push('recovery-set-params'); + }); + + // When the guard and its children mount + render( + + + , + ); + + // Then the children's layout effects still run before the gate's recovery, as they did when the gate wrapped them + expect(effectLog).toEqual(['child-layout-effect', 'recovery-set-params']); + }); + }); });