Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { renderHook } from '@testing-library/react-hooks';
import { useShouldRenderGasSponsoredBanner } from './index';
import { useIsNetworkGasSponsored } from '../useIsNetworkGasSponsored';
import { useIsHardwareWalletForBridge } from '../useIsHardwareWalletForBridge';
import { useSelector } from 'react-redux';
import {
selectSourceToken,
selectDestToken,
} from '../../../../../core/redux/slices/bridge';

jest.mock('../useIsNetworkGasSponsored');
jest.mock('../useIsHardwareWalletForBridge');
jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));
Expand All @@ -16,6 +18,10 @@ const mockUseIsNetworkGasSponsored =
useIsNetworkGasSponsored as jest.MockedFunction<
typeof useIsNetworkGasSponsored
>;
const mockUseIsHardwareWalletForBridge =
useIsHardwareWalletForBridge as jest.MockedFunction<
typeof useIsHardwareWalletForBridge
>;
const mockUseSelector = useSelector as jest.MockedFunction<typeof useSelector>;

const SOURCE_CHAIN_ID = '0x1';
Expand Down Expand Up @@ -51,6 +57,7 @@ describe('useShouldRenderGasSponsoredBanner', () => {
jest.clearAllMocks();
mockTokens({});
mockUseIsNetworkGasSponsored.mockReturnValue(false);
mockUseIsHardwareWalletForBridge.mockReturnValue(false);
});

describe('returns true when quoteGasSponsored is true', () => {
Expand Down Expand Up @@ -146,6 +153,43 @@ describe('useShouldRenderGasSponsoredBanner', () => {
});

describe('returns false', () => {
it('returns false when quote is sponsored but source account is a hardware wallet', () => {
// Arrange
mockUseIsHardwareWalletForBridge.mockReturnValue(true);

// Act
const { result } = renderHook(() =>
useShouldRenderGasSponsoredBanner({
quoteGasSponsored: true,
hasInsufficientBalance: false,
}),
);

// Assert
expect(result.current).toBe(false);
});

it('returns false when insufficient balance and network is sponsored but source account is a hardware wallet', () => {
// Arrange
mockTokens({
sourceChainId: SOURCE_CHAIN_ID,
destChainId: SAME_CHAIN_DEST_CHAIN_ID,
});
mockUseIsNetworkGasSponsored.mockReturnValue(true);
mockUseIsHardwareWalletForBridge.mockReturnValue(true);

// Act
const { result } = renderHook(() =>
useShouldRenderGasSponsoredBanner({
quoteGasSponsored: false,
hasInsufficientBalance: true,
}),
);

// Assert
expect(result.current).toBe(false);
});

it('returns false when quoteGasSponsored is false and balance is sufficient', () => {
// Arrange
mockUseIsNetworkGasSponsored.mockReturnValue(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
selectSourceToken,
selectDestToken,
} from '../../../../../core/redux/slices/bridge';
import { useIsHardwareWalletForBridge } from '../useIsHardwareWalletForBridge';

interface Props {
quoteGasSponsored?: boolean;
Expand All @@ -16,6 +17,7 @@ export const useShouldRenderGasSponsoredBanner = ({
}: Props) => {
const sourceToken = useSelector(selectSourceToken);
const destToken = useSelector(selectDestToken);
const isHardwareWallet = useIsHardwareWalletForBridge();
const isNetworkGasSponsored = useIsNetworkGasSponsored(sourceToken?.chainId);

// Sponsorship only applies to same-chain (swap) flows; cross-chain bridges
Expand All @@ -27,8 +29,9 @@ export const useShouldRenderGasSponsoredBanner = ({
);

const shouldShowGasSponsored =
quoteGasSponsored ||
(hasInsufficientBalance && isNetworkGasSponsored && isSameChain);
!isHardwareWallet &&
(quoteGasSponsored ||
(hasInsufficientBalance && isNetworkGasSponsored && isSameChain));

return shouldShowGasSponsored;
};
Loading