-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { fundWalletSvg } from '../../internal/svg/fundWallet'; | ||
import { walletSvg } from '../../internal/svg/walletSvg'; | ||
import { useIcon } from './useIcon'; | ||
|
||
describe('useIcon', () => { | ||
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 = <svg aria-label="custom-icon" />; | ||
const { result, rerender } = renderHook(({ icon }) => useIcon({ icon }), { | ||
initialProps: { icon: customIcon }, | ||
}); | ||
|
||
const initialResult = result.current; | ||
rerender({ icon: customIcon }); | ||
expect(result.current).toBe(initialResult); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}; |