|
| 1 | +import { render, screen, fireEvent, act } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 3 | +import { ShareButtons } from './ShareButtons'; |
| 4 | +import type { Campaign } from '../types/campaign'; |
| 5 | + |
| 6 | +const mockCampaign: Campaign = { |
| 7 | + id: '42', |
| 8 | + title: 'Build a Solar Farm', |
| 9 | + description: 'A solar energy crowdfund.', |
| 10 | + creator: `G${'A'.repeat(55)}`, |
| 11 | + assetCode: 'XLM', |
| 12 | + acceptedTokens: ['XLM'], |
| 13 | + targetAmount: 5000, |
| 14 | + pledgedAmount: 1200, |
| 15 | + deadline: Math.floor(Date.now() / 1000) + 86400, |
| 16 | + createdAt: Math.floor(Date.now() / 1000), |
| 17 | + pledges: [], |
| 18 | + progress: { |
| 19 | + status: 'open', |
| 20 | + percentFunded: 24, |
| 21 | + remainingAmount: 3800, |
| 22 | + hoursLeft: 24, |
| 23 | + pledgeCount: 3, |
| 24 | + canPledge: true, |
| 25 | + canClaim: false, |
| 26 | + canRefund: false, |
| 27 | + }, |
| 28 | + metadata: {}, |
| 29 | +}; |
| 30 | + |
| 31 | +describe('ShareButtons', () => { |
| 32 | + const originalLocation = window.location; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + Object.defineProperty(window, 'location', { |
| 36 | + writable: true, |
| 37 | + value: { origin: 'https://example.com' }, |
| 38 | + }); |
| 39 | + |
| 40 | + vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + Object.defineProperty(window, 'location', { |
| 45 | + writable: true, |
| 46 | + value: originalLocation, |
| 47 | + }); |
| 48 | + vi.useRealTimers(); |
| 49 | + vi.restoreAllMocks(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders all four share controls', () => { |
| 53 | + render(<ShareButtons campaign={mockCampaign} />); |
| 54 | + |
| 55 | + expect(screen.getByRole('link', { name: /share.*twitter/i })).toBeInTheDocument(); |
| 56 | + expect(screen.getByRole('link', { name: /share.*farcaster/i })).toBeInTheDocument(); |
| 57 | + expect(screen.getByRole('link', { name: /share.*lens/i })).toBeInTheDocument(); |
| 58 | + expect(screen.getByRole('button', { name: /copy campaign link/i })).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Twitter link points to twitter.com intent with campaign title, goal, and URL', () => { |
| 62 | + render(<ShareButtons campaign={mockCampaign} />); |
| 63 | + |
| 64 | + const link = screen.getByRole('link', { name: /share.*twitter/i }) as HTMLAnchorElement; |
| 65 | + const url = new URL(link.href); |
| 66 | + |
| 67 | + expect(url.hostname).toBe('twitter.com'); |
| 68 | + expect(url.pathname).toBe('/intent/tweet'); |
| 69 | + |
| 70 | + const text = url.searchParams.get('text') ?? ''; |
| 71 | + expect(text).toContain('Build a Solar Farm'); |
| 72 | + expect(text).toContain('5000'); |
| 73 | + expect(text).toContain('XLM'); |
| 74 | + |
| 75 | + const tweetUrl = url.searchParams.get('url') ?? ''; |
| 76 | + expect(tweetUrl).toBe('https://example.com/campaigns/42'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('Farcaster link points to warpcast.com with campaign details', () => { |
| 80 | + render(<ShareButtons campaign={mockCampaign} />); |
| 81 | + |
| 82 | + const link = screen.getByRole('link', { name: /share.*farcaster/i }) as HTMLAnchorElement; |
| 83 | + const url = new URL(link.href); |
| 84 | + |
| 85 | + expect(url.hostname).toBe('warpcast.com'); |
| 86 | + |
| 87 | + const text = url.searchParams.get('text') ?? ''; |
| 88 | + expect(text).toContain('Build a Solar Farm'); |
| 89 | + expect(text).toContain('5000'); |
| 90 | + expect(text).toContain('XLM'); |
| 91 | + expect(text).toContain('https://example.com/campaigns/42'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Lens link points to hey.xyz with campaign details', () => { |
| 95 | + render(<ShareButtons campaign={mockCampaign} />); |
| 96 | + |
| 97 | + const link = screen.getByRole('link', { name: /share.*lens/i }) as HTMLAnchorElement; |
| 98 | + const url = new URL(link.href); |
| 99 | + |
| 100 | + expect(url.hostname).toBe('hey.xyz'); |
| 101 | + |
| 102 | + const content = url.searchParams.get('content') ?? ''; |
| 103 | + expect(content).toContain('Build a Solar Farm'); |
| 104 | + expect(content).toContain('5000'); |
| 105 | + expect(content).toContain('XLM'); |
| 106 | + expect(content).toContain('https://example.com/campaigns/42'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('external links open in a new tab with rel=noopener noreferrer', () => { |
| 110 | + render(<ShareButtons campaign={mockCampaign} />); |
| 111 | + |
| 112 | + for (const name of [/share.*twitter/i, /share.*farcaster/i, /share.*lens/i]) { |
| 113 | + const link = screen.getByRole('link', { name }) as HTMLAnchorElement; |
| 114 | + expect(link).toHaveAttribute('target', '_blank'); |
| 115 | + expect(link).toHaveAttribute('rel', 'noopener noreferrer'); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('copy button copies canonical campaign URL to clipboard', async () => { |
| 120 | + const writeText = vi.fn().mockResolvedValue(undefined); |
| 121 | + Object.defineProperty(navigator, 'clipboard', { |
| 122 | + value: { writeText }, |
| 123 | + writable: true, |
| 124 | + configurable: true, |
| 125 | + }); |
| 126 | + |
| 127 | + render(<ShareButtons campaign={mockCampaign} />); |
| 128 | + |
| 129 | + const btn = screen.getByRole('button', { name: /copy campaign link/i }); |
| 130 | + await act(async () => { |
| 131 | + fireEvent.click(btn); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(writeText).toHaveBeenCalledWith('https://example.com/campaigns/42'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('copy button shows "Copied" feedback after click, then reverts after 2 s', async () => { |
| 138 | + const writeText = vi.fn().mockResolvedValue(undefined); |
| 139 | + Object.defineProperty(navigator, 'clipboard', { |
| 140 | + value: { writeText }, |
| 141 | + writable: true, |
| 142 | + configurable: true, |
| 143 | + }); |
| 144 | + |
| 145 | + render(<ShareButtons campaign={mockCampaign} />); |
| 146 | + |
| 147 | + const btn = screen.getByRole('button', { name: /copy campaign link/i }); |
| 148 | + |
| 149 | + await act(async () => { |
| 150 | + fireEvent.click(btn); |
| 151 | + }); |
| 152 | + |
| 153 | + expect(btn).toHaveTextContent('Copied'); |
| 154 | + |
| 155 | + act(() => { |
| 156 | + vi.advanceTimersByTime(2000); |
| 157 | + }); |
| 158 | + |
| 159 | + expect(btn).toHaveTextContent('Copy link'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('copy button falls back to execCommand when clipboard API is unavailable', async () => { |
| 163 | + // Remove clipboard API to trigger the fallback |
| 164 | + Object.defineProperty(navigator, 'clipboard', { |
| 165 | + value: undefined, |
| 166 | + writable: true, |
| 167 | + configurable: true, |
| 168 | + }); |
| 169 | + |
| 170 | + // jsdom does not implement execCommand — stub it |
| 171 | + Object.defineProperty(document, 'execCommand', { |
| 172 | + value: vi.fn().mockReturnValue(true), |
| 173 | + writable: true, |
| 174 | + configurable: true, |
| 175 | + }); |
| 176 | + |
| 177 | + render(<ShareButtons campaign={mockCampaign} />); |
| 178 | + |
| 179 | + const btn = screen.getByRole('button', { name: /copy campaign link/i }); |
| 180 | + await act(async () => { |
| 181 | + fireEvent.click(btn); |
| 182 | + }); |
| 183 | + |
| 184 | + expect(document.execCommand).toHaveBeenCalledWith('copy'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('share group has an accessible group label', () => { |
| 188 | + render(<ShareButtons campaign={mockCampaign} />); |
| 189 | + expect(screen.getByRole('group', { name: /share campaign/i })).toBeInTheDocument(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('each button and link has a descriptive aria-label', () => { |
| 193 | + render(<ShareButtons campaign={mockCampaign} />); |
| 194 | + |
| 195 | + expect( |
| 196 | + screen.getByRole('link', { name: 'Share "Build a Solar Farm" on Twitter' }), |
| 197 | + ).toBeInTheDocument(); |
| 198 | + expect( |
| 199 | + screen.getByRole('link', { name: 'Share "Build a Solar Farm" on Farcaster' }), |
| 200 | + ).toBeInTheDocument(); |
| 201 | + expect( |
| 202 | + screen.getByRole('link', { name: 'Share "Build a Solar Farm" on Lens' }), |
| 203 | + ).toBeInTheDocument(); |
| 204 | + expect(screen.getByRole('button', { name: 'Copy campaign link' })).toBeInTheDocument(); |
| 205 | + }); |
| 206 | +}); |
0 commit comments