Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 106 additions & 75 deletions app/components/UI/Ramp/Views/Checkout/Checkout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fireEvent, act, waitFor } from '@testing-library/react-native';
import Checkout from './Checkout';
import renderWithProvider from '../../../../../util/test/renderWithProvider';
import { MetaMetricsEvents } from '../../../../../core/Analytics';
import Routes from '../../../../../constants/navigation/Routes';
import { callbackBaseUrl } from '../../Aggregator/sdk';

jest.mock('@react-navigation/native', () => {
Expand Down Expand Up @@ -32,11 +33,6 @@ jest.mock('../../hooks/useRampsOrders', () => ({
useRampsOrders: jest.fn(),
}));

jest.mock('../../hooks/useRampsUnifiedV2Enabled', () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock('../../../../hooks/useAnalytics/useAnalytics', () => ({
useAnalytics: jest.fn(),
}));
Expand All @@ -51,10 +47,6 @@ jest.mock('../../../../../reducers/fiatOrders', () => ({
getRampRoutingDecision: () => null,
}));

jest.mock('../../utils/v2OrderToast', () => ({
showV2OrderToast: jest.fn(),
}));

jest.mock('../../headless/sessionRegistry', () => ({
getSession: jest.fn(),
closeSession: jest.fn(),
Expand Down Expand Up @@ -228,10 +220,6 @@ const mockUseParams = jest.requireMock(
const mockUseRampsOrders = jest.requireMock('../../hooks/useRampsOrders')
.useRampsOrders as jest.Mock;

const mockUseRampsUnifiedV2Enabled = jest.requireMock(
'../../hooks/useRampsUnifiedV2Enabled',
).default as jest.Mock;

const mockUseAnalytics = jest.requireMock(
'../../../../hooks/useAnalytics/useAnalytics',
).useAnalytics as jest.Mock;
Expand Down Expand Up @@ -265,7 +253,6 @@ describe('Checkout', () => {
getOrderFromCallback: mockGetOrderFromCallback,
addPrecreatedOrder: mockAddPrecreatedOrder,
});
mockUseRampsUnifiedV2Enabled.mockReturnValue(false);
mockUseAnalytics.mockReturnValue({
trackEvent: mockTrackEvent,
createEventBuilder: mockCreateEventBuilder,
Expand Down Expand Up @@ -303,8 +290,7 @@ describe('Checkout', () => {
});
});

expect(mockGetOrderFromCallback).not.toHaveBeenCalled();
expect(mockAddOrder).not.toHaveBeenCalled();
expect(mockNavigation.reset).not.toHaveBeenCalled();
});

it('does not invoke callback handler when hasCallbackFlow is false', async () => {
Expand All @@ -319,8 +305,7 @@ describe('Checkout', () => {
fireEvent.press(getByTestId('trigger-callback-navigation'));
});

expect(mockGetOrderFromCallback).not.toHaveBeenCalled();
expect(mockAddOrder).not.toHaveBeenCalled();
expect(mockNavigation.reset).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -510,26 +495,15 @@ describe('Checkout', () => {
});
});

describe('V2 enabled flow', () => {
it('calls showV2OrderToast when V2 is enabled and callback succeeds', async () => {
const { showV2OrderToast } = jest.requireMock(
'../../utils/v2OrderToast',
) as {
showV2OrderToast: jest.Mock;
};
const mockOrder = {
providerOrderId: 'order-v2-1',
cryptoCurrency: { symbol: 'ETH' },
cryptoAmount: '0.5',
status: 'COMPLETED',
};
mockGetOrderFromCallback.mockResolvedValue(mockOrder);
mockUseRampsUnifiedV2Enabled.mockReturnValue(true);
describe('callback success (unified buy stack)', () => {
it('resets navigation to order details with callback params without fetching the order in Checkout', async () => {
const callbackUrl = `${callbackBaseUrl}?orderId=123`;
mockUseParams.mockReturnValue({
url: 'https://provider.example.com/checkout',
providerName: 'Test',
providerCode: 'moonpay',
walletAddress: '0xabc',
cryptocurrency: 'ETH',
});

const { getByTestId } = renderWithProvider(<Checkout />, {}, true, false);
Expand All @@ -539,41 +513,57 @@ describe('Checkout', () => {
});

await waitFor(() => {
expect(showV2OrderToast).toHaveBeenCalledWith(
expect.objectContaining({
orderId: 'order-v2-1',
cryptocurrency: 'ETH',
}),
);
expect(mockNavigation.reset).toHaveBeenCalledWith({
index: 0,
routes: [
{
name: Routes.RAMP.RAMPS_ORDER_DETAILS,
params: {
callbackUrl,
providerCode: 'moonpay',
walletAddress: '0xabc',
showCloseButton: true,
cryptocurrency: 'ETH',
},
},
],
});
});

expect(mockGetOrderFromCallback).not.toHaveBeenCalled();
expect(mockAddOrder).not.toHaveBeenCalled();
});
});

describe('callback error handling', () => {
it('sets error when getOrderFromCallback returns null', async () => {
mockGetOrderFromCallback.mockResolvedValue(null);
it('omits cryptocurrency when not provided in params', async () => {
const callbackUrl = `${callbackBaseUrl}?orderId=123`;
mockUseParams.mockReturnValue({
url: 'https://provider.example.com/checkout',
providerName: 'Test',
providerCode: 'moonpay',
walletAddress: '0xabc',
});

const { getByTestId, getByText } = renderWithProvider(
<Checkout />,
{},
true,
false,
);
const { getByTestId } = renderWithProvider(<Checkout />, {}, true, false);

await act(async () => {
fireEvent.press(getByTestId('trigger-callback-navigation'));
});

await waitFor(() => {
expect(
getByText('Order could not be retrieved from callback'),
).toBeOnTheScreen();
expect(mockNavigation.reset).toHaveBeenCalledWith({
index: 0,
routes: [
{
name: Routes.RAMP.RAMPS_ORDER_DETAILS,
params: {
callbackUrl,
providerCode: 'moonpay',
walletAddress: '0xabc',
showCloseButton: true,
},
},
],
});
});
});
});
Expand Down Expand Up @@ -657,8 +647,6 @@ describe('Checkout', () => {
.closeSession as jest.Mock;
const mockFailSession = jest.requireMock('../../headless/sessionRegistry')
.failSession as jest.Mock;
const showV2OrderToastMock = jest.requireMock('../../utils/v2OrderToast')
.showV2OrderToast as jest.Mock;

const mockOrder = {
providerOrderId: 'headless-order-1',
Expand All @@ -684,7 +672,6 @@ describe('Checkout', () => {
mockParentPop = jest.fn();
mockNavigation.getParent.mockReturnValue({ pop: mockParentPop });
mockGetOrderFromCallback.mockResolvedValue(mockOrder);
mockUseRampsUnifiedV2Enabled.mockReturnValue(true);
});

it('fires onOrderCreated, closes the session, and pops the ramp stack when a live session is present', async () => {
Expand Down Expand Up @@ -714,7 +701,6 @@ describe('Checkout', () => {
});
expect(mockParentPop).toHaveBeenCalled();
expect(mockNavigation.reset).not.toHaveBeenCalled();
expect(showV2OrderToastMock).not.toHaveBeenCalled();
});

it('still adds the order to Redux and dispatches protect-wallet when headless', async () => {
Expand Down Expand Up @@ -780,7 +766,51 @@ describe('Checkout', () => {
expect(mockParentPop).toHaveBeenCalled();
});

it('fires RAMPS_CHECKOUT_CLOSED with close_source=callback_error when headless getOrderFromCallback returns null', async () => {
mockGetSession.mockReturnValue({
id: 'hs-1',
status: 'continued',
callbacks: {
onOrderCreated: jest.fn(),
onError: jest.fn(),
onClose: jest.fn(),
},
});
mockGetOrderFromCallback.mockResolvedValue(null);
mockFailSession.mockReturnValue(true);
mockUseParams.mockReturnValue(callbackFlowParams);

const { getByTestId, unmount } = renderWithProvider(
<Checkout />,
{},
true,
false,
);

await act(async () => {
fireEvent.press(getByTestId('trigger-callback-navigation'));
});
unmount();

const closedIdx = mockCreateEventBuilder.mock.calls.findIndex(
(c) => c[0] === MetaMetricsEvents.RAMPS_CHECKOUT_CLOSED,
);
expect(closedIdx).toBeGreaterThanOrEqual(0);
expect(mockAddProperties.mock.calls[closedIdx]?.[0]).toMatchObject({
close_source: 'callback_error',
});
});

it('surfaces callback processing failures through onError and skips the ErrorView', async () => {
mockGetSession.mockReturnValue({
id: 'hs-1',
status: 'continued',
callbacks: {
onOrderCreated: jest.fn(),
onError: jest.fn(),
onClose: jest.fn(),
},
});
mockUseParams.mockReturnValue(callbackFlowParams);
mockGetOrderFromCallback.mockRejectedValueOnce(
new Error('callback failed'),
Expand All @@ -805,7 +835,6 @@ describe('Checkout', () => {
expect(mockFailSession).toHaveBeenCalledWith('hs-1', expect.any(Error));
});
expect(mockParentPop).toHaveBeenCalled();
expect(showV2OrderToastMock).not.toHaveBeenCalled();
expect(queryByText('callback failed')).toBeNull();
});

Expand All @@ -826,7 +855,7 @@ describe('Checkout', () => {
expect(mockParentPop).toHaveBeenCalled();
});

it('falls back to the regular reset + toast when session id is present but session is missing from registry', async () => {
it('falls back to OrderDetails callback-resolution when session id is present but session is missing from registry', async () => {
mockGetSession.mockReturnValue(undefined);
mockUseParams.mockReturnValue(callbackFlowParams);

Expand All @@ -837,21 +866,23 @@ describe('Checkout', () => {
});

await waitFor(() => {
expect(showV2OrderToastMock).toHaveBeenCalledWith(
expect.objectContaining({ orderId: 'headless-order-1' }),
expect(mockNavigation.reset).toHaveBeenCalledWith(
expect.objectContaining({
routes: [
expect.objectContaining({
name: Routes.RAMP.RAMPS_ORDER_DETAILS,
params: expect.objectContaining({
callbackUrl: `${callbackBaseUrl}?orderId=123`,
providerCode: 'moonpay',
walletAddress: '0xdeadbeef',
}),
}),
],
}),
);
});
expect(mockNavigation.reset).toHaveBeenCalledWith(
expect.objectContaining({
routes: [
expect.objectContaining({
params: expect.objectContaining({
orderId: 'headless-order-1',
}),
}),
],
}),
);
expect(mockGetOrderFromCallback).not.toHaveBeenCalled();
expect(mockAddOrder).not.toHaveBeenCalled();
expect(mockCloseSession).not.toHaveBeenCalled();
expect(mockParentPop).not.toHaveBeenCalled();
});
Expand All @@ -873,7 +904,8 @@ describe('Checkout', () => {
await waitFor(() => {
expect(mockNavigation.reset).toHaveBeenCalled();
});
expect(showV2OrderToastMock).toHaveBeenCalled();
expect(mockGetOrderFromCallback).not.toHaveBeenCalled();
expect(mockAddOrder).not.toHaveBeenCalled();
expect(mockCloseSession).not.toHaveBeenCalled();
expect(mockParentPop).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -1279,8 +1311,7 @@ describe('Checkout', () => {
});
});

it('fires RAMPS_CHECKOUT_CLOSED with close_source=callback_error when getOrderFromCallback returns null', async () => {
mockGetOrderFromCallback.mockResolvedValue(null);
it('fires RAMPS_CHECKOUT_CLOSED with close_source=callback_success when callback URL is recognized (non-headless)', async () => {
mockUseParams.mockReturnValue({
url: 'https://provider.example.com/checkout',
providerName: 'Test',
Expand All @@ -1301,7 +1332,7 @@ describe('Checkout', () => {
unmount();

const closed = findEventProps(MetaMetricsEvents.RAMPS_CHECKOUT_CLOSED);
expect(closed).toMatchObject({ close_source: 'callback_error' });
expect(closed).toMatchObject({ close_source: 'callback_success' });
});

it('fires RAMPS_CHECKOUT_CLOSED with close_source=http_error after a terminal HTTP error', async () => {
Expand Down
Loading
Loading