diff --git a/src/internal/svg/fundWallet.tsx b/src/internal/svg/fundWallet.tsx index ccca7ee7ce..2e55a2e952 100644 --- a/src/internal/svg/fundWallet.tsx +++ b/src/internal/svg/fundWallet.tsx @@ -1,6 +1,6 @@ import { fill } from '../../styles/theme'; -export const FundWalletSvg = ( +export const fundWalletSvg = ( { - if (icon === undefined) { - return FundWalletSvg; - } - if (isValidElement(icon)) { - return icon; - } - }, [icon]); + const iconSvg = useIcon({ icon }); const currentURL = window.location.href; const tabName = document.title; diff --git a/src/wallet/components/WalletDropdownLink.tsx b/src/wallet/components/WalletDropdownLink.tsx index 3801126149..0f04539083 100644 --- a/src/wallet/components/WalletDropdownLink.tsx +++ b/src/wallet/components/WalletDropdownLink.tsx @@ -1,6 +1,5 @@ -import { isValidElement, useMemo } from 'react'; -import { walletSvg } from '../../internal/svg/walletSvg'; import { cn, pressable, text } from '../../styles/theme'; +import { useIcon } from '../hooks/useIcon'; import type { WalletDropdownLinkReact } from '../types'; export function WalletDropdownLink({ @@ -11,18 +10,7 @@ export function WalletDropdownLink({ rel, target, }: WalletDropdownLinkReact) { - const iconSvg = useMemo(() => { - if (icon === undefined) { - return null; - } - switch (icon) { - case 'wallet': - return walletSvg; - } - if (isValidElement(icon)) { - return icon; - } - }, [icon]); + const iconSvg = useIcon({ icon }); return ( { + it('should return null when icon is undefined', () => { + const { result } = renderHook(() => useIcon({ icon: undefined })); + expect(result.current).toBeNull(); + }); + + it('should return walletSvg when icon is "wallet"', () => { + const { result } = renderHook(() => useIcon({ icon: 'wallet' })); + expect(result.current).toBe(walletSvg); + }); + + it('should return fundWalletSvg when icon is "fundWallet"', () => { + const { result } = renderHook(() => useIcon({ icon: 'fundWallet' })); + expect(result.current).toBe(fundWalletSvg); + }); + + it('should memoize the result for undefined', () => { + const { result, rerender } = renderHook(() => useIcon({}), { + initialProps: {}, + }); + + const initialResult = result.current; + rerender({}); + expect(result.current).toBe(initialResult); + }); + + it('should memoize the result for wallet', () => { + const { result, rerender } = renderHook(({ icon }) => useIcon({ icon }), { + initialProps: { icon: 'wallet' }, + }); + + const initialResult = result.current; + rerender({ icon: 'wallet' }); + expect(result.current).toBe(initialResult); + }); + + it('should memoize the result for fundWallet', () => { + const { result, rerender } = renderHook(({ icon }) => useIcon({ icon }), { + initialProps: { icon: 'fundWallet' }, + }); + + const initialResult = result.current; + rerender({ icon: 'fundWallet' }); + expect(result.current).toBe(initialResult); + }); + + it('should memoize the result for custom icon', () => { + const customIcon = ; + const { result, rerender } = renderHook(({ icon }) => useIcon({ icon }), { + initialProps: { icon: customIcon }, + }); + + const initialResult = result.current; + rerender({ icon: customIcon }); + expect(result.current).toBe(initialResult); + }); +}); diff --git a/src/wallet/hooks/useIcon.tsx b/src/wallet/hooks/useIcon.tsx new file mode 100644 index 0000000000..468a5874e9 --- /dev/null +++ b/src/wallet/hooks/useIcon.tsx @@ -0,0 +1,20 @@ +import { isValidElement, useMemo } from 'react'; +import { fundWalletSvg } from '../../internal/svg/fundWallet'; +import { walletSvg } from '../../internal/svg/walletSvg'; + +export const useIcon = ({ icon }: { icon?: React.ReactNode }) => { + return useMemo(() => { + if (icon === undefined) { + return null; + } + switch (icon) { + case 'wallet': + return walletSvg; + case 'fundWallet': + return fundWalletSvg; + } + if (isValidElement(icon)) { + return icon; + } + }, [icon]); +};