11import { findFocusedRoute } from '@react-navigation/native' ;
22import { renderHook } from '@testing-library/react-native' ;
33import { useEffect } from 'react' ;
4- import useDeepCompareRef from '@hooks/useDeepCompareRef' ;
54import { clearActiveTransactionIDs , setActiveTransactionIDs } from '@libs/actions/TransactionThreadNavigation' ;
65import { navigationRef } from '@libs/Navigation/Navigation' ;
76import SCREENS from '@src/SCREENS' ;
@@ -30,20 +29,19 @@ jest.mock('@react-navigation/native', () => ({
3029 * to allow isolated testing of the useEffect behavior.
3130 */
3231function useActiveTransactionIDsEffect ( visualOrderTransactionIDs : string [ ] ) {
33- const visualOrderTransactionIDsDeepCompare = useDeepCompareRef ( visualOrderTransactionIDs ) ;
34-
3532 useEffect ( ( ) => {
3633 const focusedRoute = findFocusedRoute ( navigationRef . getRootState ( ) ) ;
3734 if ( focusedRoute ?. name !== SCREENS . RIGHT_MODAL . SEARCH_REPORT ) {
3835 return ;
3936 }
40- setActiveTransactionIDs ( visualOrderTransactionIDsDeepCompare ?? [ ] ) ;
37+ setActiveTransactionIDs ( visualOrderTransactionIDs ) ;
38+ } , [ visualOrderTransactionIDs ] ) ;
39+
40+ useEffect ( ( ) => {
4141 return ( ) => {
4242 clearActiveTransactionIDs ( ) ;
4343 } ;
44- } , [ visualOrderTransactionIDsDeepCompare ] ) ;
45-
46- return { visualOrderTransactionIDsDeepCompare} ;
44+ } , [ ] ) ;
4745}
4846
4947describe ( 'MoneyRequestReportTransactionList - Active Transaction IDs Effect' , ( ) => {
@@ -112,7 +110,7 @@ describe('MoneyRequestReportTransactionList - Active Transaction IDs Effect', ()
112110 expect ( mockClearActiveTransactionIDs ) . toHaveBeenCalledTimes ( 1 ) ;
113111 } ) ;
114112
115- it ( 'should NOT call clearActiveTransactionIDs on unmount when route was NOT SEARCH_REPORT' , ( ) => {
113+ it ( 'should call clearActiveTransactionIDs on unmount even when route was NOT SEARCH_REPORT' , ( ) => {
116114 // Given the focused route is NOT SEARCH_REPORT
117115 mockFindFocusedRoute . mockReturnValue ( { name : 'SomeOtherRoute' , key : 'test-key' } ) ;
118116
@@ -123,11 +121,11 @@ describe('MoneyRequestReportTransactionList - Active Transaction IDs Effect', ()
123121
124122 unmount ( ) ;
125123
126- // Then clearActiveTransactionIDs should NOT be called (since the effect returned early )
127- expect ( mockClearActiveTransactionIDs ) . not . toHaveBeenCalled ( ) ;
124+ // Then clearActiveTransactionIDs should still be called (cleanup runs on unmount regardless of route )
125+ expect ( mockClearActiveTransactionIDs ) . toHaveBeenCalledTimes ( 1 ) ;
128126 } ) ;
129127
130- it ( 'should update active transaction IDs when the list changes (deep comparison) ' , ( ) => {
128+ it ( 'should update active transaction IDs when the list changes' , ( ) => {
131129 // Given the focused route is SEARCH_REPORT
132130 mockFindFocusedRoute . mockReturnValue ( { name : SCREENS . RIGHT_MODAL . SEARCH_REPORT , key : 'test-key' } ) ;
133131
@@ -150,7 +148,7 @@ describe('MoneyRequestReportTransactionList - Active Transaction IDs Effect', ()
150148 expect ( mockSetActiveTransactionIDs ) . toHaveBeenLastCalledWith ( newTransactionIDs ) ;
151149 } ) ;
152150
153- it ( 'should NOT update when transaction IDs array has same content (deep comparison )' , ( ) => {
151+ it ( 'should call setActiveTransactionIDs on reference change without clearing first (idempotent guard is in the action layer )' , ( ) => {
154152 // Given the focused route is SEARCH_REPORT
155153 mockFindFocusedRoute . mockReturnValue ( { name : SCREENS . RIGHT_MODAL . SEARCH_REPORT , key : 'test-key' } ) ;
156154
@@ -167,8 +165,12 @@ describe('MoneyRequestReportTransactionList - Active Transaction IDs Effect', ()
167165 const sameContentNewArray = [ 'trans1' , 'trans2' ] ;
168166 rerender ( { ids : sameContentNewArray } ) ;
169167
170- // Then setActiveTransactionIDs should NOT be called again (deep comparison prevents it)
171- expect ( mockSetActiveTransactionIDs ) . toHaveBeenCalledTimes ( 1 ) ;
168+ // Then the effect fires (new reference), but setActiveTransactionIDs internally skips the Onyx write.
169+ // Crucially, clearActiveTransactionIDs is NOT called on re-render (only on unmount),
170+ // preventing the null→same-IDs flash.
171+ expect ( mockSetActiveTransactionIDs ) . toHaveBeenCalledTimes ( 2 ) ;
172+ expect ( mockSetActiveTransactionIDs ) . toHaveBeenLastCalledWith ( sameContentNewArray ) ;
173+ expect ( mockClearActiveTransactionIDs ) . not . toHaveBeenCalled ( ) ;
172174 } ) ;
173175
174176 it ( 'should handle empty transaction IDs array' , ( ) => {
0 commit comments