Skip to content

Commit 7ae018b

Browse files
update payment state machine tests
1 parent 6499e49 commit 7ae018b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

packages/thirdweb/src/react/core/machines/paymentMachine.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,112 @@ describe("PaymentMachine", () => {
580580
[state] = result.current;
581581
expect(state.context.preparedQuote).toBeUndefined(); // Should be cleared
582582
});
583+
584+
it("should handle post-buy-transaction state flow", () => {
585+
const { result } = renderHook(() =>
586+
usePaymentMachine(adapters, "fund_wallet"),
587+
);
588+
589+
// Go through the complete happy path to reach success state
590+
act(() => {
591+
const [, send] = result.current;
592+
send({
593+
type: "DESTINATION_CONFIRMED",
594+
destinationToken: testTokenForPayment,
595+
destinationAmount: "100",
596+
receiverAddress: "0xa3841994009B4fEabb01ebcC62062F9E56F701CD",
597+
});
598+
});
599+
600+
act(() => {
601+
const [, send] = result.current;
602+
send({
603+
type: "PAYMENT_METHOD_SELECTED",
604+
paymentMethod: {
605+
type: "wallet",
606+
payerWallet: TEST_IN_APP_WALLET_A,
607+
originToken: testUSDCToken,
608+
balance: 1000000000000000000n,
609+
},
610+
});
611+
});
612+
613+
act(() => {
614+
const [, send] = result.current;
615+
send({
616+
type: "QUOTE_RECEIVED",
617+
preparedQuote: mockBuyQuote,
618+
});
619+
});
620+
621+
act(() => {
622+
const [, send] = result.current;
623+
send({
624+
type: "ROUTE_CONFIRMED",
625+
});
626+
});
627+
628+
act(() => {
629+
const [, send] = result.current;
630+
send({
631+
type: "EXECUTION_COMPLETE",
632+
completedStatuses: [
633+
{
634+
type: "buy",
635+
status: "COMPLETED",
636+
paymentId: "test-payment-id",
637+
originAmount: 1000000000000000000n,
638+
destinationAmount: 100000000n,
639+
originChainId: 1,
640+
destinationChainId: 137,
641+
originTokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
642+
destinationTokenAddress:
643+
"0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
644+
originToken: testETHToken,
645+
destinationToken: testUSDCToken,
646+
sender: "0xa3841994009B4fEabb01ebcC62062F9E56F701CD",
647+
receiver: "0xa3841994009B4fEabb01ebcC62062F9E56F701CD",
648+
transactions: [
649+
{
650+
chainId: 1,
651+
transactionHash: "0xtest123",
652+
},
653+
],
654+
},
655+
],
656+
});
657+
});
658+
659+
let [state] = result.current;
660+
expect(state.value).toBe("success");
661+
662+
// Continue to post-buy transaction
663+
act(() => {
664+
const [, send] = result.current;
665+
send({
666+
type: "CONTINUE_TO_TRANSACTION",
667+
});
668+
});
669+
670+
[state] = result.current;
671+
expect(state.value).toBe("post-buy-transaction");
672+
673+
// Reset from post-buy-transaction should go back to init
674+
act(() => {
675+
const [, send] = result.current;
676+
send({
677+
type: "RESET",
678+
});
679+
});
680+
681+
[state] = result.current;
682+
expect(state.value).toBe("init");
683+
// Context should be reset to initial state with only adapters and mode
684+
expect(state.context.adapters).toBe(adapters);
685+
expect(state.context.mode).toBe("fund_wallet");
686+
expect(state.context.destinationToken).toBeUndefined();
687+
expect(state.context.selectedPaymentMethod).toBeUndefined();
688+
expect(state.context.preparedQuote).toBeUndefined();
689+
expect(state.context.completedStatuses).toBeUndefined();
690+
});
583691
});

packages/thirdweb/src/react/core/machines/paymentMachine.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ export function usePaymentMachine(
200200
};
201201
}
202202
break;
203+
204+
case "post-buy-transaction":
205+
if (event.type === "RESET") {
206+
return {
207+
mode: ctx.mode,
208+
adapters: ctx.adapters,
209+
};
210+
}
211+
break;
203212
}
204213
return ctx;
205214
});

0 commit comments

Comments
 (0)