From 4dd40e1329969713f12f3b7df6500514799244c3 Mon Sep 17 00:00:00 2001 From: sepehr sanaee Date: Wed, 27 Mar 2024 12:33:45 +0330 Subject: [PATCH] resolve round 2 --- .../assetDataListItemStructure.stories.tsx | 5 +- .../assetDataListItemStructure.test.tsx | 25 +++-- .../assetDataListItemStructure.tsx | 99 +++++++++++-------- 3 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.stories.tsx b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.stories.tsx index 18d333237..75d37c1ca 100644 --- a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.stories.tsx +++ b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.stories.tsx @@ -42,8 +42,9 @@ export const Default: Story = { */ export const Fallback: Story = { args: { - amount: 0, - priceChange: 0, + name: 'Ethereum', + amount: 420.69, + symbol: 'ETH', }, render: (props) => ( diff --git a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.test.tsx b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.test.tsx index e64e39247..cf7f0dff9 100644 --- a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.test.tsx +++ b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.test.tsx @@ -3,20 +3,28 @@ import { DataList } from '../../../../../core'; import { AssetDataListItemStructure, type IAssetDataListItemStructureProps } from './assetDataListItemStructure'; describe(' component', () => { - const createTestComponent = (props?: Partial) => { + const createTestComponent = (props: Partial = {}) => { + const completeProps: IAssetDataListItemStructureProps = { + name: 'Ethereum', + symbol: 'ETH', + amount: 420.69, + ...props, + }; + return ( - + ); }; - it('renders tokenName, symbol, and the logoSrc', () => { + it('renders tokenName and symbol', () => { const props = { name: 'Ethereum', symbol: 'ETH', + amount: 420.69, }; render(createTestComponent(props)); @@ -26,6 +34,8 @@ describe(' component', () => { it('renders amount, fiat price', async () => { const props = { + name: 'Ethereum', + symbol: 'ETH', amount: 420.69, fiatPrice: 3654.76, }; @@ -36,21 +46,20 @@ describe(' component', () => { expect(screen.getByText(props.amount)).toBeInTheDocument(); }); - it('handles zero priceChange as neutral', () => { + it('handles not passing fiat price', () => { const props = { + name: 'Ethereum', + symbol: 'ETH', amount: 0, priceChange: 0, }; render(createTestComponent(props)); - const elements = screen.queryAllByText('$0.00'); - expect(elements.length).toBeGreaterThan(0); // assuming both asset price and changed price amount are 0.00 - expect(screen.getByText('0%')).toBeInTheDocument(); // Assuming Tag component renders '0%' for zero priceChange + expect(screen.getByText('Unknown')).toBeInTheDocument(); // Assuming Tag component renders '0%' for zero priceChange }); it('handle not passing priceChange', async () => { const props = { - logoSrc: 'https://assets.coingecko.com/coins/images/279/standard/ethereum.png?1696501628', name: 'Ethereum', amount: 420.69, symbol: 'ETH', diff --git a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.tsx b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.tsx index 18ff12a95..216ed69db 100644 --- a/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.tsx +++ b/src/modules/components/asset/assetDataListItem/assetDataListItemStructure/assetDataListItemStructure.tsx @@ -4,36 +4,40 @@ import { useMemo } from 'react'; import { Avatar, DataList, NumberFormat, Tag, formatterUtils, type IDataListItemProps } from '../../../../../core'; export interface IAssetDataListItemStructureProps extends IDataListItemProps { - /** - * The logo source of the asset - */ - logoSrc?: string; /** * The name of the asset. */ - name?: string; + name: string; /** * The symbol of the asset. */ - symbol?: string; + symbol: string; /** * The amount of the asset. */ - amount?: number | string; + amount: number | string; + /** + * The logo source of the asset + */ + logoSrc?: string; /** * The fiat price of the asset. */ fiatPrice?: number | string; /** * the price change in percentage of the asset (E.g. in last 24h). + * @default 0 */ priceChange?: number; } export const AssetDataListItemStructure: React.FC = (props) => { - const { logoSrc, name = '-', amount = 0, symbol, fiatPrice = 0, priceChange = 0, ...otherProps } = props; + const { logoSrc, name, amount, symbol, fiatPrice, priceChange = 0, ...otherProps } = props; const usdAmountChanged = useMemo(() => { + if (!fiatPrice || !priceChange) { + return 0; + } const usdAmount = (amount ? Number(amount) : 0) * (fiatPrice ? Number(fiatPrice) : 0); const oldUsdAmount = (100 / (priceChange + 100)) * usdAmount; return usdAmount - oldUsdAmount; @@ -48,50 +52,61 @@ export const AssetDataListItemStructure: React.FC
- +
+ +
{name}

- - {formatterUtils.formatNumber(amount, { - format: NumberFormat.TOKEN_AMOUNT_SHORT, - fallback: '', - })}{' '} - + {`${formattedAmount}`} {symbol}

-
- - {formatterUtils.formatNumber( - (amount ? Number(amount) : 0) * (fiatPrice ? Number(fiatPrice) : 0), - { - format: NumberFormat.FIAT_TOTAL_SHORT, - fallback: '-', - }, - )} - -
- - {sign(usdAmountChanged)} - {formatterUtils.formatNumber(Math.abs(usdAmountChanged), { - format: NumberFormat.FIAT_TOTAL_SHORT, - })} - - 0 ? 'success' : priceChange < 0 ? 'critical' : 'neutral'} - /> -
+
+ {fiatPrice ? ( + <> + + {formattedPrice} + +
+ + {sign(usdAmountChanged)} + {formattedPriceChanged} + + 0 ? 'success' : priceChange < 0 ? 'critical' : 'neutral'} + /> +
+ + ) : ( + Unknown + )}