11import { renderHook } from '@testing-library/react-native' ;
22import Onyx from 'react-native-onyx' ;
3+ import { useCardList , useWorkspaceCardList } from '@components/OnyxListItemProvider' ;
34import { getPolicy , getWorkspaceAccountID , isPolicyAdmin } from '@libs/PolicyUtils' ;
45import { getOriginalMessage , isCardIssuedAction } from '@libs/ReportActionsUtils' ;
56import CONST from '@src/CONST' ;
@@ -12,6 +13,10 @@ import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'
1213// Mock the dependencies
1314jest . mock ( '@libs/PolicyUtils' ) ;
1415jest . mock ( '@libs/ReportActionsUtils' ) ;
16+ jest . mock ( '@components/OnyxListItemProvider' , ( ) => ( {
17+ useCardList : jest . fn ( ) ,
18+ useWorkspaceCardList : jest . fn ( ) ,
19+ } ) ) ;
1520
1621// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
1722// eslint-disable-next-line deprecation/deprecation
@@ -20,6 +25,8 @@ const mockGetWorkspaceAccountID = getWorkspaceAccountID as jest.MockedFunction<t
2025const mockIsPolicyAdmin = isPolicyAdmin as jest . MockedFunction < typeof isPolicyAdmin > ;
2126const mockGetOriginalMessage = getOriginalMessage as jest . MockedFunction < typeof getOriginalMessage > ;
2227const mockIsCardIssuedAction = isCardIssuedAction as jest . MockedFunction < typeof isCardIssuedAction > ;
28+ const mockUseCardList = useCardList as jest . MockedFunction < typeof useCardList > ;
29+ const mockUseWorkspaceCardList = useWorkspaceCardList as jest . MockedFunction < typeof useWorkspaceCardList > ;
2330
2431describe ( 'useGetExpensifyCardFromReportAction' , ( ) => {
2532 const mockCard : Card = {
@@ -76,6 +83,8 @@ describe('useGetExpensifyCardFromReportAction', () => {
7683 mockIsPolicyAdmin . mockReturnValue ( false ) ;
7784 mockGetOriginalMessage . mockReturnValue ( { cardID : 123 , assigneeAccountID : 1 } ) ;
7885 mockIsCardIssuedAction . mockReturnValue ( true ) ;
86+ mockUseCardList . mockReturnValue ( { } ) ;
87+ mockUseWorkspaceCardList . mockReturnValue ( { } ) ;
7988 } ) ;
8089
8190 describe ( 'when reportAction is not a card issued action' , ( ) => {
@@ -102,8 +111,7 @@ describe('useGetExpensifyCardFromReportAction', () => {
102111
103112 it ( 'returns card from allUserCards when card exists' , async ( ) => {
104113 // eslint-disable-next-line @typescript-eslint/naming-convention
105- Onyx . set ( ONYXKEYS . CARD_LIST , { '123' : mockCard } ) ;
106- await waitForBatchedUpdatesWithAct ( ) ;
114+ mockUseCardList . mockReturnValue ( { '123' : mockCard } ) ;
107115
108116 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
109117 await waitForBatchedUpdatesWithAct ( ) ;
@@ -112,8 +120,7 @@ describe('useGetExpensifyCardFromReportAction', () => {
112120 } ) ;
113121
114122 it ( 'returns undefined when card does not exist in allUserCards' , async ( ) => {
115- Onyx . set ( ONYXKEYS . CARD_LIST , { } ) ;
116- await waitForBatchedUpdatesWithAct ( ) ;
123+ mockUseCardList . mockReturnValue ( { } ) ;
117124
118125 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
119126 await waitForBatchedUpdatesWithAct ( ) ;
@@ -143,8 +150,7 @@ describe('useGetExpensifyCardFromReportAction', () => {
143150 it ( 'returns card from allExpensifyCards when card exists' , async ( ) => {
144151 const workspaceCardsKey = `${ ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST } 123_${ CONST . EXPENSIFY_CARD . BANK } ` as OnyxKey ;
145152 // eslint-disable-next-line @typescript-eslint/naming-convention
146- Onyx . set ( workspaceCardsKey , { 123 : mockCard } ) ;
147- await waitForBatchedUpdatesWithAct ( ) ;
153+ mockUseWorkspaceCardList . mockReturnValue ( { [ workspaceCardsKey ] : { 123 : mockCard } } ) ;
148154
149155 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
150156 await waitForBatchedUpdatesWithAct ( ) ;
@@ -155,8 +161,7 @@ describe('useGetExpensifyCardFromReportAction', () => {
155161 it ( 'returns undefined when card does not exist in allExpensifyCards' , async ( ) => {
156162 const workspaceCardsKey = `${ ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST } 123_${ CONST . EXPENSIFY_CARD . BANK } ` as OnyxKey ;
157163 // eslint-disable-next-line @typescript-eslint/naming-convention
158- Onyx . set ( ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST , { [ workspaceCardsKey ] : { } } ) ;
159- await waitForBatchedUpdatesWithAct ( ) ;
164+ mockUseWorkspaceCardList . mockReturnValue ( { [ workspaceCardsKey ] : { } } ) ;
160165
161166 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
162167 await waitForBatchedUpdatesWithAct ( ) ;
@@ -168,16 +173,22 @@ describe('useGetExpensifyCardFromReportAction', () => {
168173
169174 describe ( 'reactivity to Onyx changes' , ( ) => {
170175 it ( 'updates when allUserCards changes' , async ( ) => {
176+ mockUseCardList . mockReturnValue ( { } ) ;
177+ mockUseWorkspaceCardList . mockReturnValue ( { } ) ;
178+
171179 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
172180 await waitForBatchedUpdatesWithAct ( ) ;
173181
174182 expect ( result . current ) . toBeUndefined ( ) ;
175183
176184 // eslint-disable-next-line @typescript-eslint/naming-convention
177- Onyx . set ( ONYXKEYS . CARD_LIST , { '123' : mockCard } ) ;
185+ mockUseCardList . mockReturnValue ( { '123' : mockCard } ) ;
186+
187+ // Re-render the hook to get the updated result
188+ const { result : updatedResult } = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
178189 await waitForBatchedUpdatesWithAct ( ) ;
179190
180- expect ( result . current ) . toEqual ( mockCard ) ;
191+ expect ( updatedResult . current ) . toEqual ( mockCard ) ;
181192 } ) ;
182193
183194 it ( 'updates when allExpensifyCards changes for policy admin' , async ( ) => {
@@ -193,26 +204,30 @@ describe('useGetExpensifyCardFromReportAction', () => {
193204 isPolicyExpenseChatEnabled : false ,
194205 workspaceAccountID : 123 ,
195206 } ) ;
207+
208+ // Set initial state
209+ mockUseCardList . mockReturnValue ( { } ) ;
210+ mockUseWorkspaceCardList . mockReturnValue ( { } ) ;
211+
196212 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
197213 await waitForBatchedUpdatesWithAct ( ) ;
198214
199215 expect ( result . current ) . toBeUndefined ( ) ;
200216
201- // eslint-disable-next-line @typescript-eslint/naming-convention
202217 const workspaceCardsKey = `${ ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST } 123_${ CONST . EXPENSIFY_CARD . BANK } ` as OnyxKey ;
203218 // eslint-disable-next-line @typescript-eslint/naming-convention
204- Onyx . set ( workspaceCardsKey , { 123 : mockCard } ) ;
219+ mockUseWorkspaceCardList . mockReturnValue ( { [ workspaceCardsKey ] : { 123 : mockCard } } ) ;
220+ const { result : updatedResult } = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
205221 await waitForBatchedUpdatesWithAct ( ) ;
206222
207- expect ( result . current ) . toEqual ( mockCard ) ;
223+ expect ( updatedResult . current ) . toEqual ( mockCard ) ;
208224 } ) ;
209225 } ) ;
210226
211227 describe ( 'workspace account ID generation' , ( ) => {
212228 it ( 'calls getWorkspaceAccountID with correct policyID' , async ( ) => {
213229 // eslint-disable-next-line @typescript-eslint/naming-convention
214- Onyx . set ( ONYXKEYS . CARD_LIST , { '123' : mockCard } ) ;
215- await waitForBatchedUpdatesWithAct ( ) ;
230+ mockUseCardList . mockReturnValue ( { '123' : mockCard } ) ;
216231
217232 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'test-policy-123' } ) ) ;
218233 await waitForBatchedUpdatesWithAct ( ) ;
@@ -238,8 +253,7 @@ describe('useGetExpensifyCardFromReportAction', () => {
238253 mockGetPolicy . mockReturnValue ( testPolicy ) ;
239254
240255 // eslint-disable-next-line @typescript-eslint/naming-convention
241- Onyx . set ( ONYXKEYS . CARD_LIST , { '123' : mockCard } ) ;
242- await waitForBatchedUpdatesWithAct ( ) ;
256+ mockUseCardList . mockReturnValue ( { '123' : mockCard } ) ;
243257
244258 const { result} = renderHook ( ( ) => useGetExpensifyCardFromReportAction ( { reportAction : createMockReportAction ( ) , policyID : 'policy123' } ) ) ;
245259 await waitForBatchedUpdatesWithAct ( ) ;
0 commit comments