11import { renderHook } from "@testing-library/react-hooks"
22
3+ import { flushEffects } from "../../helpers/flush-effects"
4+
35import { usePaymentRequest } from "@app/screens/receive-bitcoin-screen/hooks/use-payment-request"
46import { WalletCurrency } from "@app/graphql/generated"
57import {
@@ -28,12 +30,20 @@ jest.mock("@app/screens/receive-bitcoin-screen/hooks/use-wallet-resolution", ()
2830 useWalletResolution : ( ) => mockUseWalletResolution ( ) ,
2931} ) )
3032
33+ // The mutation functions must keep a stable identity across renders: they
34+ // feed the `mutations` useMemo in usePaymentRequest, and a fresh jest.fn()
35+ // per render would re-trigger useInvoiceLifecycle's layout effect on every
36+ // render, looping until React aborts with "Maximum update depth exceeded".
37+ const mockLnInvoiceCreate = jest . fn ( )
38+ const mockLnNoAmountInvoiceCreate = jest . fn ( )
39+ const mockLnUsdInvoiceCreate = jest . fn ( )
40+ const mockOnChainAddressCurrent = jest . fn ( )
3141jest . mock ( "@app/graphql/generated" , ( ) => ( {
3242 WalletCurrency : { Btc : "BTC" , Usd : "USD" } ,
33- useLnInvoiceCreateMutation : ( ) => [ jest . fn ( ) ] ,
34- useLnNoAmountInvoiceCreateMutation : ( ) => [ jest . fn ( ) ] ,
35- useLnUsdInvoiceCreateMutation : ( ) => [ jest . fn ( ) ] ,
36- useOnChainAddressCurrentMutation : ( ) => [ jest . fn ( ) ] ,
43+ useLnInvoiceCreateMutation : ( ) => [ mockLnInvoiceCreate ] ,
44+ useLnNoAmountInvoiceCreateMutation : ( ) => [ mockLnNoAmountInvoiceCreate ] ,
45+ useLnUsdInvoiceCreateMutation : ( ) => [ mockLnUsdInvoiceCreate ] ,
46+ useOnChainAddressCurrentMutation : ( ) => [ mockOnChainAddressCurrent ] ,
3747} ) )
3848
3949const mockUseLnUpdateHashPaid = jest . fn ( )
@@ -153,17 +163,20 @@ describe("usePaymentRequest", () => {
153163 expect ( result . current ) . toBeNull ( )
154164 } )
155165
156- it ( "creates PRCD with PayCode for BTC default wallet with username" , ( ) => {
166+ it ( "creates PRCD with PayCode for BTC default wallet with username" , async ( ) => {
157167 setupMocksWithPR ( )
158168
159169 renderHook ( ( ) => usePaymentRequest ( ) )
160170
171+ // Settle generateRequest's async setPR inside act()
172+ await flushEffects ( )
173+
161174 expect ( mockCreatePaymentRequestCreationData ) . toHaveBeenCalledWith (
162175 expect . objectContaining ( { type : Invoice . PayCode } ) ,
163176 )
164177 } )
165178
166- it ( "creates PRCD with Lightning for wallet without username" , ( ) => {
179+ it ( "creates PRCD with Lightning for wallet without username" , async ( ) => {
167180 const walletsNoUsername = { ...mockWallets , username : null }
168181 const mockPRCD = {
169182 ...createFullMockPRCD ( ) ,
@@ -189,12 +202,15 @@ describe("usePaymentRequest", () => {
189202
190203 renderHook ( ( ) => usePaymentRequest ( ) )
191204
205+ // Settle generateRequest's async setPR inside act()
206+ await flushEffects ( )
207+
192208 expect ( mockCreatePaymentRequestCreationData ) . toHaveBeenCalledWith (
193209 expect . objectContaining ( { type : Invoice . Lightning } ) ,
194210 )
195211 } )
196212
197- it ( "creates PRCD with Lightning for USD default wallet even with username" , ( ) => {
213+ it ( "creates PRCD with Lightning for USD default wallet even with username" , async ( ) => {
198214 const walletsUsdDefault = {
199215 ...mockWallets ,
200216 defaultWallet : { id : "usd-id" , balance : 100 , walletCurrency : WalletCurrency . Usd } ,
@@ -219,16 +235,22 @@ describe("usePaymentRequest", () => {
219235
220236 renderHook ( ( ) => usePaymentRequest ( ) )
221237
238+ // Settle generateRequest's async setPR inside act()
239+ await flushEffects ( )
240+
222241 expect ( mockCreatePaymentRequestCreationData ) . toHaveBeenCalledWith (
223242 expect . objectContaining ( { type : Invoice . Lightning } ) ,
224243 )
225244 } )
226245
227- it ( "uses default expiration time for BTC wallet" , ( ) => {
246+ it ( "uses default expiration time for BTC wallet" , async ( ) => {
228247 setupMocksWithPR ( )
229248
230249 renderHook ( ( ) => usePaymentRequest ( ) )
231250
251+ // Settle generateRequest's async setPR inside act()
252+ await flushEffects ( )
253+
232254 expect ( mockCreatePaymentRequestCreationData ) . toHaveBeenCalledWith (
233255 expect . objectContaining ( { expirationTime : 1440 } ) ,
234256 )
0 commit comments