Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding avatar token to design system react #413

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
@@ -0,0 +1,142 @@
import type { Meta, StoryObj } from '@storybook/react';
import React from 'react';

import { AvatarToken } from './AvatarToken';
import { AvatarTokenSize } from '.';
import README from './README.mdx';

const meta: Meta<typeof AvatarToken> = {
title: 'React Components/AvatarToken',
component: AvatarToken,
parameters: {
docs: {
page: README,
},
},
argTypes: {
name: {
control: 'text',
description:
'Required name of the token. Used as alt text for image and first letter is used as fallback if no fallbackText provided',
},
src: {
control: 'text',
description:
'Optional URL for the token image. When provided, displays the image instead of fallback text',
},
imageProps: {
control: 'object',
description:
'Optional prop to pass to the underlying img element. Useful for overriding the default alt text',
},
size: {
control: 'select',
options: Object.keys(AvatarTokenSize),
mapping: AvatarTokenSize,
description:
'Optional prop to control the size of the avatar. Defaults to AvatarTokenSize.Md',
},
fallbackText: {
control: 'text',
description:
'Optional text to display when no image is provided. If not provided, first letter of name will be used',
},
fallbackTextProps: {
control: 'object',
description:
'Optional props to be passed to the Text component when rendering fallback text. Only used when src is not provided',
},
className: {
control: 'text',
description:
'Optional additional CSS classes to be applied to the component',
},
},
};

export default meta;
type Story = StoryObj<typeof AvatarToken>;

export const Default: Story = {
args: {
src: 'https://cryptologos.cc/logos/ethereum-eth-logo.png',
name: 'Ethereum',
fallbackText: 'ETH',
},
};

export const Src: Story = {
render: () => (
<div className="flex gap-2">
<AvatarToken
name="Ethereum"
fallbackText="ETH"
src="https://cryptologos.cc/logos/ethereum-eth-logo.png"
/>
<AvatarToken
name="Bitcoin"
fallbackText="BTC"
src="https://cryptologos.cc/logos/bitcoin-btc-logo.png"
/>
<AvatarToken
name="USDC"
fallbackText="USDC"
src="https://cryptologos.cc/logos/usd-coin-usdc-logo.png"
/>
</div>
),
};

export const Name: Story = {
render: () => (
<div className="flex gap-2">
<AvatarToken
name="Ethereum"
src="https://cryptologos.cc/logos/ethereum-eth-logo.png"
/>
<AvatarToken
name="Bitcoin"
src="https://cryptologos.cc/logos/bitcoin-btc-logo.png"
/>
<AvatarToken name="USDC" />
</div>
),
};

export const FallbackText: Story = {
render: () => (
<div className="flex gap-2">
<AvatarToken name="Ethereum" fallbackText="ETH" />
<AvatarToken name="Bitcoin" fallbackText="BTC" />
<AvatarToken name="USDC" fallbackText="USDC" />
</div>
),
};

export const Size: Story = {
render: () => (
<div className="flex gap-2 items-center">
<AvatarToken name="Ethereum" fallbackText="E" size={AvatarTokenSize.Xs} />
<AvatarToken
name="Ethereum"
fallbackText="ETH"
size={AvatarTokenSize.Sm}
/>
<AvatarToken
name="Ethereum"
fallbackText="ETH"
size={AvatarTokenSize.Md}
/>
<AvatarToken
name="Ethereum"
fallbackText="ETH"
size={AvatarTokenSize.Lg}
/>
<AvatarToken
name="Ethereum"
fallbackText="ETH"
size={AvatarTokenSize.Xl}
/>
</div>
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { TextColor } from '..';
import { AvatarToken } from './AvatarToken';
import { AvatarTokenSize } from '.';

describe('AvatarToken', () => {
it('renders image when src is provided', () => {
render(
<AvatarToken src="test-image.jpg" name="Ethereum" fallbackText="Eth" />,
);

const img = screen.getByRole('img');
expect(img).toBeInTheDocument();
expect(img).toHaveAttribute('src', 'test-image.jpg');
expect(img).toHaveAttribute('alt', 'Ethereum');
});

it('renders fallbackText when src is not provided', () => {
render(<AvatarToken name="Ethereum" fallbackText="Eth" />);
expect(screen.getByText('Eth')).toBeInTheDocument();
});

it('applies fallbackTextProps to Text component', () => {
render(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
fallbackTextProps={{
color: TextColor.TextAlternative,
className: 'test-class',
'data-testid': 'fallback-text',
}}
/>,
);

const text = screen.getByTestId('fallback-text');
expect(text).toHaveClass('text-alternative', 'test-class');
});

it('applies custom className to root element', () => {
render(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
className="custom-class"
data-testid="avatar"
/>,
);

const avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('custom-class');
});

it('passes through additional image props when src is provided', () => {
render(
<AvatarToken
src="test-image.jpg"
name="Ethereum"
fallbackText="Eth"
imageProps={{
loading: 'lazy',
}}
/>,
);

screen.debug();

const img = screen.getByRole('img');
expect(img).toHaveAttribute('loading', 'lazy');
});

it('applies size classes correctly', () => {
const { rerender } = render(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
size={AvatarTokenSize.Xs}
data-testid="avatar"
/>,
);

let avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-4 w-4');

rerender(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
size={AvatarTokenSize.Sm}
data-testid="avatar"
/>,
);
avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-6 w-6');

rerender(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
size={AvatarTokenSize.Md}
data-testid="avatar"
/>,
);
avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-8 w-8');

rerender(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
size={AvatarTokenSize.Lg}
data-testid="avatar"
/>,
);
avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-10 w-10');

rerender(
<AvatarToken
name="Ethereum"
fallbackText="Eth"
size={AvatarTokenSize.Xl}
data-testid="avatar"
/>,
);
avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-12 w-12');
});

it('uses medium size by default', () => {
render(<AvatarToken name="Ethereum" data-testid="avatar" />);
const avatar = screen.getByTestId('avatar');
expect(avatar).toHaveClass('h-8 w-8');
});

it('uses name as alt text when fallbackText is not provided', () => {
render(<AvatarToken src="test-image.jpg" name="Ethereum" />);

const img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'Ethereum');
});

it('uses first letter of name as fallback text when fallbackText is not provided', () => {
render(<AvatarToken name="Ethereum" />);
expect(screen.getByText('E')).toBeInTheDocument();
});

it('prioritizes fallbackText over name for both alt text and fallback display', () => {
const { rerender } = render(
<AvatarToken
src="test-image.jpg"
name="Ethereum"
fallbackText="ETH"
imageProps={{ alt: 'ETH' }}
/>,
);

let img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'ETH');

rerender(<AvatarToken name="Ethereum" fallbackText="ETH" />);

expect(screen.getByText('ETH')).toBeInTheDocument();
});
});

describe('text display and alt text logic', () => {
it('uses first letter of name when fallbackText is not provided', () => {
render(<AvatarToken name="Ethereum" />);
expect(screen.getByText('E')).toBeInTheDocument();
});

it('uses fallbackText for display when provided', () => {
render(<AvatarToken name="Ethereum" fallbackText="ETH" />);
expect(screen.getByText('ETH')).toBeInTheDocument();
});

it('uses name for alt text when src is provided', () => {
render(<AvatarToken name="Ethereum" src="test.jpg" />);
const img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'Ethereum');
});

it('uses name for alt text even when fallbackText is provided', () => {
render(<AvatarToken name="Ethereum" fallbackText="ETH" src="test.jpg" />);
const img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'Ethereum');
});

it('allows alt text override through imageProps', () => {
render(
<AvatarToken
name="Ethereum"
src="test.jpg"
imageProps={{ alt: 'Custom Alt' }}
/>,
);
const img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'Custom Alt');
});

it('uses empty string for display text when name is not provided', () => {
// @ts-expect-error testing invalid props
render(<AvatarToken data-testid="avatar" />);
const base = screen.getByTestId('avatar');
expect(base.querySelector('span')).toHaveTextContent('');
});

it('uses default "Token logo" for alt text when name is not provided', () => {
// @ts-expect-error testing invalid props
render(<AvatarToken src="test.jpg" />);
const img = screen.getByRole('img');
expect(img).toHaveAttribute('alt', 'Token logo');
});
});
Loading
Loading