Skip to content
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
1 change: 1 addition & 0 deletions src/components/__tests__/FollowButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';import { describe, it, expect, vi } from 'vitest';import FollowButton from '../common/FollowButton';describe('FollowButton', () => { it('renders Follow when not following', () => { render(<FollowButton creatorAddress="0x123" isFollowing={false} onFollow={vi.fn()} onUnfollow={vi.fn()} />); expect(screen.getByTestId('follow-button')).toHaveTextContent('Follow'); }); it('renders Following when following', () => { render(<FollowButton creatorAddress="0x123" isFollowing={true} onFollow={vi.fn()} onUnfollow={vi.fn()} />); expect(screen.getByTestId('follow-button')).toHaveTextContent('Following'); }); it('calls onFollow with correct address when not following', async () => { const onFollow = vi.fn().mockResolvedValue(undefined); render(<FollowButton creatorAddress="0x123" isFollowing={false} onFollow={onFollow} onUnfollow={vi.fn()} />); fireEvent.click(screen.getByTestId('follow-button')); await waitFor(() => expect(onFollow).toHaveBeenCalledWith('0x123')); }); it('calls onUnfollow when already following', async () => { const onUnfollow = vi.fn().mockResolvedValue(undefined); render(<FollowButton creatorAddress="0x123" isFollowing={true} onFollow={vi.fn()} onUnfollow={onUnfollow} />); fireEvent.click(screen.getByTestId('follow-button')); await waitFor(() => expect(onUnfollow).toHaveBeenCalled()); }); it('disables button and shows loading while pending', async () => { const onFollow = vi.fn().mockImplementation(() => new Promise(resolve => setTimeout(resolve, 100))); render(<FollowButton creatorAddress="0x123" isFollowing={false} onFollow={onFollow} onUnfollow={vi.fn()} />); fireEvent.click(screen.getByTestId('follow-button')); expect(screen.getByTestId('follow-button')).toBeDisabled(); expect(screen.getByTestId('follow-button')).toHaveTextContent('Loading...'); await waitFor(() => expect(onFollow).toHaveBeenCalled()); });});
Expand Down
42 changes: 42 additions & 0 deletions src/components/common/FollowButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useState } from 'react';
import { Button } from '@/components/ui/button';

interface FollowButtonProps {
creatorAddress: string;
isFollowing: boolean;
onFollow: (creatorAddress: string) => Promise<void>;
onUnfollow: (creatorAddress: string) => Promise<void>;
}

export default function FollowButton({
creatorAddress,
isFollowing,
onFollow,
onUnfollow,
}: FollowButtonProps) {
const [loading, setLoading] = useState(false);

const handleClick = async () => {
setLoading(true);
try {
if (isFollowing) {
await onUnfollow(creatorAddress);
} else {
await onFollow(creatorAddress);
}
} finally {
setLoading(false);
}
};

return (
<Button
onClick={handleClick}
disabled={loading}
variant={isFollowing ? 'outline' : 'default'}
data-testid="follow-button"
>
{loading ? 'Loading...' : isFollowing ? 'Following' : 'Follow'}
</Button>
);
}
Loading